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 += "{\n\t";
                        break;
                    case '}':
                        result += "\n}\n";
                        break;
                    case ';':
                        if(oneLine.charAt(i+1)!='}'){
                            result += ";\n\t";
                        }else{
                            result += ";\n";
                        }
                        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);
        }
    }
}

  

posted @ 2014-05-24 21:00  小振xzh  阅读(179)  评论(0编辑  收藏  举报