adam bien's blog

Reading A File Into A String - With JDK 1.7 📎


import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileReader {

    public static void main(String[] args) throws IOException {
        Path file = Paths.get("./readme.txt");
        BufferedReader reader = Files.newBufferedReader(file, Charset.defaultCharset());
        StringBuilder content = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            content.append(line).append("/n");
        }
        System.out.println("Content: " + content.toString());
    }
}

NetBeans 7.0.1 not only runs and uses JDK 1.7 - it also provides hints and refactorings to transform your legacy JDK 1.6 code into JDK 1.7 :-).