0

I want to know how to output the integers of an array into a text file separated by different lines. This is the pertinent code below, but every time I run the program, the file is created but no data is saved into the file itself.

public static void printToFile(double[] gravity)throws IOException
{
    PrintWriter outputFile = new PrintWriter (new FileWriter("gravitydata.txt", true));
    for(int a = 0; a < 9; a++)
    {
         outputFile.println(gravity[a]);
    }
}
2
  • 6
    you forget to call outputFile.close(); Commented Dec 26, 2014 at 20:20
  • 2
    Correct - Beat me to it! Commented Dec 26, 2014 at 20:21

3 Answers 3

2

You have to close() the file (closing the PrintWriter will close the FileWriter which will close the file). You could use a try-with-resources to do it for you

public static void printToFile(double[] gravity) throws IOException
{
    try (PrintWriter outputFile = new PrintWriter(
            new FileWriter("gravitydata.txt", true))) {
        for(int a = 0; a < gravity.length; a++){
            outputFile.println(gravity[a]);
        }
    }
}

or the older finally block and something like

public static void printToFile(double[] gravity) throws IOException
{
    PrintWriter outputFile = new PrintWriter(
            new FileWriter("gravitydata.txt", true))
    try {
        for(int a = 0; a < gravity.length; a++){
            outputFile.println(gravity[a]);
        }
    } finally {
        outputFile.close();
    }
}

in either case you should use the array length property instead of hard-coding 9.

Sign up to request clarification or add additional context in comments.

Comments

1
{
    PrintWriter outputFile = new PrintWriter (new FileWriter("gravitydata.txt", true));
    for(int a = 0; a < 9; a++)
    {
        outputFile.println(gravity[a]);
    }
    outputFile.close();

}

Comments

0

The following approach can be good if you wish to utilize the built-in functions from Java 8 (to avoid writing your own file handling and looping).

public static void printToFile(double[] gravity) throws IOException {
    // First, convert the double[] to a list of Strings
    final List<String> doublesAsStrings = Arrays.stream(gravity)
            .boxed() // Box it to Doubles
            .map(g -> String.valueOf(g)) // Convert each Double to a String
            .collect(Collectors.toList()); // Create a List<String>

    // Then, write the list to the file
    Files.write(new File("gravitydata.txt").toPath(), doublesAsStrings);
}

There are a few differences compared to the previous answers:

  • The Files class is used to write the data which is convenient since it handles try/catch etc
  • No external looping is required since the streaming API:s are used to create a sequence of strings that will be printed
  • It is short and compact ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.