MI Report

package ReadTXT; //MI Report


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";
   
    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());
                    String content = readFile(readfile.getPath());
                    String newFilePath = filePath + "\\" + msgName + ".csv";
                    writeFile(content, newFilePath);
                    } else if (readfile.isDirectory()) {
                    readFile(filePath + "//" + filelist[i]);
                    }
            }
    }
    }
    
    public static final void writeFile(String str, String filePath) throws IOException {
        try {
        FileWriter fw = new FileWriter(filePath,true);
            String titles = "Institution_name,\t" + "Country_code,\t" + "Transation,\t" + "Counts,\t" + "\n";
            fw.write(titles + str);
            fw.close();
        } catch (IOException e1) {
            e1.printStackTrace();
            System.out.println("write failed");
            System.exit(-1);
        }
    }
    
    //逐行读取写入文本
    public static final String readFile(String filePath) throws IOException {
    String returnMSG = "";
             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.contains("--"))&&(!line.startsWith("DNS"))){
                 if(line.startsWith("MSG_name"))
                 {
                 msgName = line.substring((line.lastIndexOf("=") + 3),line.lastIndexOf("\""));
                          System.out.println("--------------------------------------------------");
                 System.out.println(msgName);
                 //writeFile(msgName + "\n"); //test
                 String titles = "institution_name,\t" + "country_code,\t" + "transation,\t" + "counts,\t";
                          //writeFile(titles + "\n");
                 //returnMSG = "titles + \n";
                          System.out.println(titles);
                 }else if((!line.contains("="))&&(!line.contains("count"))){
                 line = line.substring(0, line.length());
                 /*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 strCount,strTranc,strC_C,strINST,stdLine;
                 strCount = line.substring(0, 9) + ",";
                 strTranc = line.substring(10, 19) + ",";
                 strC_C = line.substring(20, 29) + ",";
                 strINST = line.substring(30, 39) + ",";
                 stdLine = strINST + strC_C + strTranc +strCount;
                 //writeFile(stdLine + "\n");
                 //returnMSG = stdLine + "\n";
                 returnMSG += stdLine + "\n";
                 System.out.println(stdLine);
                 } 
                 
                  }
             }
             br.close();
             System.out.println("===================================================");
             System.out.println(returnMSG);
             System.out.println("===================================================");
             return returnMSG;
    }


}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值