Java Ninja Chronicles By Norris Shelton

Things I learned in the pursuit of code

I was working on a piece of code and noticed that it was using an older method to write a file. A fileWriter was used to to write the file. That was wrapped with a bufferedWriter. Here is what the original code looked like.

        BufferedWriter outputWriter = null;
        outputWriter = new BufferedWriter(new FileWriter(filename));
        for (int i = 0; i < x.length; i++) {
            if (i == x.length -1) {
                outputWriter.write(Integer.toString(x[i]));
            } else {
                outputWriter.write(Integer.toString(x[i]) + ",");
            }

            outputWriter.newLine();
        }
        outputWriter.flush();
        outputWriter.close();

I changed it to a NIO Files implementation.

Files.write(Paths.get("/igs/rng/test/" + provider + ".csv"), stringBuilder.toString().getBytes());

It doesn’t get any easier than that.

January 23rd, 2015

Posted In: Java

Tags: , , , ,

Leave a Comment

LinkedIn Auto Publish Powered By : XYZScripts.com