JAVA 解析TXT文本

时间:2022-08-04 18:21:02

package file;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.util.Date;

public class ImportFile {

    public static void main(String[] args) {

       System.out.println(new Date());

       String path = "E:\\数据.txt";  //文件路径

       File file = new File(path);

       if(file.exists() && file.isFile()){

          try{

             InputStreamReader read = new InputStreamReader(new FileInputStream(file),"GBK");  //设置文字编码

            BufferedReader bufferRead = new BufferedReader(read);

            String lineText = null;

         while(bufferRead.readLine() != null){

              lineText = bufferRead.readLine();

              String data[] = lineText.split("\t"); //本文件中按制表符分割

              for(String x : data){

               System.out.println(x);

              }

            }

            read.close();

}catch(Exception e){

         e.printStackTrace();

      }

}

System.out.println(new Date());

}

}