java读取txt文件,将数据存入list中
public static List<String> readData(File file) {
InputStreamReader in = null;
try {
in = new InputStreamReader(new FileInputStream(file), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader br = new BufferedReader(in);
String string = null;
ArrayList<String> arrayList = new ArrayList<String>();
try {
while ((string = br.readLine()) != null) {
arrayList.add(string.trim());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return arrayList;
}
java 将数据写入 txt文件中
public static String writeDate(String outPath, List<Number> numbers) {
File writeName = new File(outPath + File.separator + System.currentTimeMillis() + ".txt");
try {
writeName.createNewFile();
BufferedWriter out = new BufferedWriter(new FileWriter(writeName));
for (Number number : numbers) {
out.write(number.getNumber() + "," + number.getEncryptionNumber() + "\r\n");
// out.newLine();
}
out.flush(); // 把缓存区内容压入文件
out.close(); // 最后记得关闭文件
} catch (IOException e) {
e.printStackTrace();
}
return writeName.toString();
}