Java常用方法 將檔案讀成String

1 · Hoxton · Dec. 26, 2023, 4:55 p.m.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 public static String readFileAsString(File file) { StringBuilder content = new StringBuilder(); try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { content.append(line).append("\n"); } } catch (IOException e) { e.printStackTrace(); // 可以根據需要進行錯誤處理 } return content.toString(); } ...