MI_report

package ReadTXT;

import java.io.*;

public class Filedo {
 static private String msgName;
 //逐行寫入文本
    public static void main(String[] args) throws IOException{
     String filePath = "D://Workspaces//eclipse//workspace//Test//data//MI Report";  //"Data//MI_Report"; //MI Report
     
     File file = new File(filePath);
     if(!file.isDirectory()){
      readFile(filePath);
     }else if (file.isDirectory()) {
      String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {
                    File readfile = new File(filePath + "//" + filelist[i]);
                    if (!readfile.isDirectory()) {
                            /*System.out.println("path=" + readfile.getPath());
                            System.out.println("absolutepath=" + readfile.getAbsolutePath());
                            System.out.println("name=" + readfile.getName());*/
                     readFile(filePath + "//" + readfile.getName());
                    } else if (readfile.isDirectory()) {
                            //readFile(filePath + "\\" + filelist[i]);
                    }
            }
     }
     
        //writeFile(filePath);
    }
   
    public static final void writeFile(String str) throws IOException {
     //String filePath = "Data//output.txt";
     String filePath = "D:\\Workspaces\\eclipse\\workspace\\Test\\data\\MI Report\\" + msgName + ".csv";
        FileWriter fw = null;
        try {
            fw = new FileWriter(filePath,true);
            //String c = "abs"+"\r\n";
            fw.write(str);
            fw.close();
            //fw.flush();
            //readF1(filePath);
        } catch (IOException e1) {
            e1.printStackTrace();
            System.out.println("寫入失敗");
            System.exit(-1);
        }
    }
   
    //逐行讀取寫入文本
    public static final void readFile(String filePath) throws IOException {
             BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));        
             for (String line = br.readLine(); line != null; line = br.readLine()) {
                  //System.out.println(line);
                  if((!line.startsWith("--"))&&(!line.startsWith("DNS"))){
                   if(line.startsWith("MSG_name"))
                   {
                    msgName = line.substring((line.lastIndexOf("=") + 3),line.length() - 2);
                          System.out.println("--------------------------------------------------");
                    System.out.println(msgName);
                    //writeFile(msgName + "\n"); //test
                    String titles = "institution_name," + "country_code," + "transation," + "counts,";
                          writeFile(titles + "\n");
                          System.out.println(titles);
                   }else if((!line.contains("=")) && (!line.contains("count"))){
                    line = line.substring(0, 28);
                    /*StringBuffer stdLine = new StringBuffer(line);
                    stdLine = stdLine.insert(5, ",");
                    stdLine = stdLine.insert(16, ",");
                    stdLine = stdLine.insert(22, ",");
                    stdLine = stdLine.insert(30, ",");
                    writeFile(stdLine + "\n");*/
                    String str1,str2,str3,str4,stdLine;
                    str1 = line.substring(0, 5) + ",";
                    str2 = line.substring(6, 15) + ",";
                    str3 = line.substring(16, 20) + ",";
                    str4 = line.substring(21, 27) + ",";
                    stdLine = str2 + str3 + str4 +str1;
                    writeFile(stdLine + "\n");
                    System.out.println(stdLine);
                   }
                  
                  }
             }
             br.close();
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章