读取txt文件:
/**
* 读取txt文件的整行
* @return
* @throws IOException
*/
public List<String> read(String fileName) throws IOException {
List<String> txtList = new ArrayList<String>();
BufferedReader in = new BufferedReader(new FileReader(fileName));
String line = null;
while ((line = in.readLine()) != null) {
txtList.add(line);
}
return txtList;
}
写入txt文件:
/**
* 将"Hello world!"写入txt文件("Hello world!"可以换为字符串)
* @param fileName txt文件路径
*/
public void writeTpTxt(String fileName) {
try {
File file = new File(
fileName);
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Hello world!");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}