Question

I am currently tring to store data in a file that is going to be called questions.list (a file object)However i am unable to this, i suspect there a few problems/missing parts in my code(as i have a very basic knowledge on objects). Could anyone please help me achieve this or help provide me with a better understanding of what i am doing wrong? thanks.

import java.util.Scanner;
import java.io.*;

public class Practice {
/**
*large amount of code
*/

String answer = alpha4;
System.out.println(answer);
}

/**
 * Method for saving information to a file 'questions.list'
 */
public static void printToFile(String userInformationToBePrinted) 
        throws IOException {
    PrintWriter fileWriter = new PrintWriter(
            new BufferedWriter(
                    new FileWriter("questions.list", true)));

    fileWriter.append(userInformationToBePrinted + "\n");

    fileWriter.close();

I am guessing i am missing some code. If the question is not clear please feel free to ask any questions.

Was it helpful?

Solution

Since you are saying that the progam runs without any error. Perhaps, your problem is that you can't find the file that you've just created.

By default, file created using PrintWriter / FileWriter will be saved in the directory where you run your program.

Note that you can specify where you want to save the file you created by adding folder path in your file name. for example, if you want to save the question.list in D:\ you can do this:

PrintWriter fileWriter = new PrintWriter(
                         new BufferedWriter(
                         new FileWriter("D:\questions.list", true)));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top