转载 https://www.cnblogs.com/vickylinj/p/15490194.html
将 List
写入文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public void writeList2File(List<Long> lines, String filePath) { File file = new File(filePath); if (!file.exists()) { file.createNewFile(); } BufferedWriter bw = new BufferedWriter(new FileWriter(file)); for (long lineL : lines) { bw.write(long2DateString(lineL) + "\r\n"); } bw.flush(); bw.close(); }
|
将文件读为 List
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public ArrayList<String> readFile2List() { File inputFile = new File("D:\\dir\\file.txt"); InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(inputFile)); BufferedReader br = new BufferedReader(inputStreamReader);
String line = null; ArrayList<String> lines = Lists.newArrayList(); while ((line = br.readLine()) != null) { lines.add(line); } return lines; }
|