Java解析txt文件
package com.wb.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class TestTxt {
public static void Txt(String filepath){
String encoding = "gbk";//txt一般默认编码为gbk
File file = new File(filepath);
if(file.exists() && file.isFile()){
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String txtLine = "";
while((txtLine = bufferedReader.readLine()) != null){
System.out.println(txtLine);
}
read.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String filepath = "C:\\Users\\WINX\\Desktop\\1.txt";
Txt(filepath);
}
}
谢谢!!!有什么不明白欢迎联系