java:file_to_string

File to String

Change YOUR_FILE to your actual file. You might want to change to charset for your file.

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();
        }
    }
}
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();
        }

    }
}
  • java/file_to_string.txt
  • Last modified: 2020/12/10 12:29
  • by chongtin