Show pageBacklinksBack to top This page is read only. You can view the source, but not change it. Ask your administrator if you think this is wrong. ====== File to String ====== ===== Read the Whole File ===== Change YOUR_FILE to your actual file. You might want to change to charset for your file. <code> class Scratch { static String readFile(String path, Charset encoding) throws IOException { byte[] encoded = Files.readAllBytes(Paths.get(path)); return new String(encoded, encoding); } public static void main(String[] args) { try { String s = readFile("404357 1 - SOP AF WT 2019.json", Charset.defaultCharset()); System.out.println(s); } catch (IOException e) { e.printStackTrace(); } } } </code> ===== Read it by Line ===== <code> class Scratch { public static void main(String[] args) { try { Stream<String> ss = Files.lines(Paths.get("YOUR_FILE"), Charset.defaultCharset()); Collection<String> sss = ss.collect(Collectors.toList()); for (String s : sss) { System.out.println(s); } } catch (IOException e) { e.printStackTrace(); } } } </code> java/file_to_string.txt Last modified: 2020/12/10 12:29by chongtin