Let's Format Css Documents

每次想参考一些好看网站的时候,打开css文档都是一行的,琢磨了下就自己写了块短短的代码,各路Java大神别笑我呀。^_^

复制粘贴控制台的输出就好了。(瞬间觉得跟上大神的脚步了←_←)

package sortingAlgorithm;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author xzh
 */
public class test {
    public static void main(String[] args){
        try {
            FileInputStream fis = new FileInputStream("./style.css");
            InputStreamReader insr = new InputStreamReader(fis,"utf-8");
            BufferedReader bufr = new BufferedReader(insr);
            String oneLine = "";
            String tmp;
            while((tmp=bufr.readLine())!=null){
                oneLine += tmp;
            }
            String result = "";
            for(int i=0;i<oneLine.length();i++){
                char ch = oneLine.charAt(i);
                switch (ch){
                    case '{':
                        result += "{
	";
                        break;
                    case '}':
                        result += "
}
";
                        break;
                    case ';':
                        if(oneLine.charAt(i+1)!='}'){
                            result += ";
	";
                        }else{
                            result += ";
";
                        }
                        break;
                    default :
                        result += ch;
                }
            }
            System.out.println(result);
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

  

原文地址:https://www.cnblogs.com/xzhang/p/3750397.html