Javarevisited: How to Read File in One Line in JDK 7 or Java 8
▼ Tuesday, February 24, 2015 How to Read File in One Line in JDK 7 or Java 8 Reading a file in Java is not simple, it requires lots of boiler plate code, as we have seen in our earlier example of reading text files . Various things had to wrapped e.g. a FileInputStream inside a BufferedReader, loops with weird terminating conditions had to be specified and so forth. From JDK 7 onward, you can do a lot better. It provides lots of useful classes e.g. Files and Paths to deal with file and their actual path. In this article, we will see how we can read a file in just one line. Of course, your production code won't be like that, especially if you are reading a few gigabytes into memory and want to pay attention to the character set, if you don't specify, by platform's default character encoding will be used. In short, you will need a little more code, but for quick and dirty file reading this should do the trick. By the way, It wouldn't be a one-liner if it had exception handling.Read full article from Javarevisited: How to Read File in One Line in JDK 7 or Java 8
No comments:
Post a Comment