`

Easiest way to read write files in java 8

 
阅读更多
Path path = Paths.get(getClass().getClassLoader()
      .getResource("fileTest.txt").toURI()); //relative to the class

String content = new String(Files.readAllBytes(path));
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
                stream.forEach(System.out::println);
            }


String text = "Text to save to file";
Files.write(Paths.get("./fileName.txt"), text.getBytes());

 

    public static String read(String fileName) throws IOException {
        InputStream resourceAsStream = FileUtility.class.getClassLoader().getResourceAsStream(fileName); // the filename is relative to the classpath root
        try (BufferedReader buffer = new BufferedReader(new InputStreamReader(resourceAsStream))) {
            return buffer.lines().collect(Collectors.joining("\n"));
        }
    }

 

 

分享到:
评论

相关推荐

    Sybex - Mastering Visual Basic .NET (VBL).pdf

    must be done in a certain way, and I present alternatives and try to connect new topics to those explained earlier in the book. In several chapters, I expand on applications developed in earlier chap-...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    You can significantly minimize the number of header files you need to include in your own header files by using forward declarations. For example, if your header file uses the File class in ways that ...

    drupal 6.12

    to create the files directory and grant the web server write privileges to it: mkdir sites/default/files chmod o+w sites/default/files The install script will attempt to write-protect the ...

    VB编程资源大全(英文控件)

    PicOpener.zip PicOpener is an ActiveX control that allows you to read image files in over 50 formats and convert them to BMP bitmaps&lt;END&gt;&lt;br&gt;20,PicConverter.zip Read over 50 and write 15 image ...

    数位板压力测试

    Applications must be able to get the services they need without getting in each others' way. Questions to ask when thinking about tablet sharing include: • Can tablet applications use the tablet ...

Global site tag (gtag.js) - Google Analytics