一、按行读取txt并返回List集合
首先读取txt文本为文件字节流 FileInputStream ,然后转换为字符流,然后创建字符流缓冲区读取字符流,通过字符缓冲区 BufferdReader 的 readLine() 方法,按行读取。
关键代码:
File file = new File("F:\\IDEA project\\test.txt");
// 将字节流向字符流转换
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file),
"utf-8");
// 创建字符流缓冲区
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String string = null;
while ((string = bufferedReader.readLine()) != null) {
System.out.println(string);
}
txtUtil 工具类方法
/**
* 按行读取txt文件并返回一个List<String>集合
* @param path
* @return List<String>
*/
public static List<String> getFileContent(String path) {
List<String> strList = new ArrayList<>();
File file = new File(path);
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
// 将字节流向字符流转换
inputStreamReader = new InputStreamReader(new FileInputStream(file), "utf-8");
// 创建字符流缓冲区
bufferedReader = new BufferedReader(inputStreamReader);
String line;
// 按行读取
while ((line = bufferedReader.readLine()) != null) {
strList.add(line);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStreamReader != null) {
try {
inputStreamReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return strList;
}
二. 一次性读取txt文本所有内容,并返回String
可用两种方式实现,字节流读取或字符流读取。
使用字节流读取时,可以先定义一个字节数组 content[] ,然后使用 fileInputStream 的 read(content[]) 方法,读取为字节并放入到定义的字节数组中,最终返回这样一个字节数组。
关键代码:
byte[] fileContent = new byte[1024];
FileInputStream fileInputStream = new FileInputStream(new File("FilePath"));
fileInputStram.read(fileContent);
复制粘贴即可用的工具类方法:
/**
* 字节流方式 读取所有txt文本内容
* @param filePath
* @return String
*/
public static String getAllTxt(String filePath) {
File file = new File(filePath);
Long fileLengthLong = file.length();
byte[] fileContent = new byte[fileLengthLong.intValue()];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
fileInputStream.read(fileContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(fileContent);
}
也可以使用字符流读取方式读取所有txt文本并返回一个String
/**
* 字符流读取方式 读取所有文本内容并返回String
* @param filePath
* @return String
*/
public String getAllTxtByChar(String filePath) {
File file = new File(filePath);
Long fileLength = file.length();
char[] fileContent = new char[fileLength.intValue()];
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(new FileInputStream(file));
inputStreamReader.read(fileContent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return new String(fileContent);
}