51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

java读取本地目录的文件转换为list

不在沉默中爆发,就在沉默中灭亡。------鲁迅

我读取了我的全部博客内容并转换成了一个List<String>

代码如下:

|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | hljs java import java.io.*; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; class Scratch { public static void main(String[] args) throws Exception { List<File> fileNames = getFileNames("D:/file/blog/backup/blog/source/_posts/"); List<String> collect = fileNames.parallelStream().map(Scratch::readFile).collect(Collectors.toList()); collect.forEach(System.out::println); } private static List<File> getFileNames(String filePath) { File file = new File(filePath); //判断文件或目录是否存在 if (!file.exists()) { System.err.println("【" + filePath + " not exists】"); } return Optional.ofNullable(file.listFiles()).map(Arrays::asList).orElseGet(Collections::emptyList); } /** * 将文本文件中的内容读入到buffer中 * * @param buffer buffer * @param file 文件路径 * @throws IOException 异常 * @author cn.outofmemory * @date 2013-1-7 */ public static void readToBuffer(StringBuffer buffer, File file) throws IOException { InputStream is = new FileInputStream(file); String line; // 用来保存每行读取的内容 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); line = reader.readLine(); // 读取第一行 while (line != null) { // 如果 line 为空说明读完了 buffer.append(line); // 将读到的内容添加到 buffer 中 buffer.append("\n"); // 添加换行符 line = reader.readLine(); // 读取下一行 } reader.close(); is.close(); } /** * 读取文本文件内容 * * @param file 文件所在路径 * @return 文本内容 * @throws IOException 异常 * @author cn.outofmemory * @date 2013-1-7 */ public static String readFile(File file) { StringBuffer sb = new StringBuffer(); try { readToBuffer(sb, file); } catch (IOException e) { e.printStackTrace(); } return sb.toString(); } } |

效果如下:

image-20210819214039510

赞(0)
未经允许不得转载:工具盒子 » java读取本地目录的文件转换为list