Java解析文件

解析文件的原理很簡單,從txt文件讀出文件,而後再經過一系列規則獲得想要的文件再送回一個新的文件。此次的解析我只完成了一半,因此記錄下來的大佬的代碼java

package com.zjhcsoft;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.zhuhao
 * @date 2018/11/12 16:34
 */
public class test {

    public static void main(String[] args) throws Exception {

        File pathFile = new File("D:\\解析04.txt");
        if (pathFile.isFile() && pathFile.exists()) {
            FileInputStream is = new FileInputStream(pathFile);//建立文件字節讀取流文件,明確與之關聯的數據源
            InputStreamReader isReader = new InputStreamReader(is, "utf-8");//將字節流包裝成字符流
            BufferedReader br = new BufferedReader(isReader);//提供緩衝式文本讀本
            File writeFl = new File("D:\\c.txt");
            if (!writeFl.exists()) {
                writeFl.createNewFile();
            }
            FileOutputStream fs = new FileOutputStream(writeFl);
            OutputStreamWriter ow = new OutputStreamWriter(fs, "utf-8");
            BufferedWriter bw = new BufferedWriter(ow);
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {//按行讀取
                StringBuffer sb = new StringBuffer();  
                sb.append(lineTxt.replace("\\t",""));  //將行中的空格去掉
                bw.write(sb.toString()+"\r\n"); //將內容讀入新的文檔
            }
            isReader.close();
            br.close();
            bw.flush();
            bw.close();
        } else {
            throw new Exception("文件不存在");
        }
    }
}
package com.zjhcsoft;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.zhuhao
 * @date 2018/11/12 19:26
 */
public class test1 {

    public static void main(String[] args) throws Exception {

        File pathFile = new File("E:\\學習文檔及截圖\\Java開發\\同步大數據清單sql\\out10.txt");
        if (pathFile.isFile() && pathFile.exists()) {
            FileInputStream is = new FileInputStream(pathFile);
            InputStreamReader isReader = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isReader);
            File writeFl = new File("D:\\f.txt");
            if (!writeFl.exists()) {
                writeFl.createNewFile();
            }
            FileOutputStream fs = new FileOutputStream(writeFl);
            OutputStreamWriter ow = new OutputStreamWriter(fs, "utf-8");
            BufferedWriter bw = new BufferedWriter(ow);
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                StringBuffer sb = new StringBuffer();
                String[] line = lineTxt.split(",");//根據,將文件隔開
                if (line.length <9) {
                  sb.append(line[0]+","+line[1]+","+line[2]+","+line[3]+","+line[4]+","+line[5]+","+line[6]+","+"\"\""+","+line[7]);
                }else {
                    sb.append(line[0]+","+line[1]+","+line[2]+","+line[3]+","+line[4]+","+line[5]+","+line[6]+","+line[7]+","+line[8]);
                }
                bw.write(sb.toString()+"\r\n");
            }
            isReader.close();
            br.close();
            bw.flush();
            bw.close();

        } else {
            throw new Exception("文件不存在");
        }
    }
}
package com.zjhcsoft;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Mr.zhuhao
 * @date 2018/11/12 19:27
 */
public class test2 {

    public static void main(String[] args) throws Exception {

        File pathFile = new File("D:\\b.txt");
        if (pathFile.isFile() && pathFile.exists()) {
            FileInputStream is = new FileInputStream(pathFile);
            InputStreamReader isReader = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isReader);
            File writeFl = new File("D:\\d.txt");
            if (!writeFl.exists()) {
                writeFl.createNewFile();
            }
            List<String> list = new ArrayList<>();
            list.add(null);
            FileOutputStream fs = new FileOutputStream(writeFl);
            OutputStreamWriter ow = new OutputStreamWriter(fs, "utf-8");
            BufferedWriter bw = new BufferedWriter(ow);
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                StringBuffer sb = new StringBuffer();
                list.add(lineTxt);
            }
            for(int i=1;i<list.size();i++){
                String l=list.get(i);
                if(!l.substring(l.length()-1,l.length()).equals(",")){
                    bw.write(list.get(i).toString()+"\r\n");//在文件中沒有逗號的地方換行
                }else {
                    bw.write(list.get(i).toString());//並將文件讀入文檔中
                }
            }
            isReader.close();
            br.close();
            bw.flush();
            bw.close();
        } else {
            throw new Exception("文件不存在");
        }
    }
}
相關文章
相關標籤/搜索