Reputation: 105
To keep it short, I am trying to create a method which takes in as an argument a output file stream name, and then allows the user to write to that text, and append to it with a while loop. Here's what I wrote:
public void insertRowsToFile(OutputStream output) throws IOException {
Scanner keyboard = new Scanner(System.in);
//here is my mistake that i dont know how to solve (outputstream, boolean is undefined)
PrintWriter out = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(output, true)));
boolean answer = true;
while (answer == true) {
System.out.println("Enter the row:");
String entered = keyboard.nextLine();
out.println(entered);
System.out.println("Would you like to write more? (yes to continue)");
String answer2 = keyboard.next();
if (!answer2.equals("yes"))
answer = false;
}
}
}
Upvotes: 0
Views: 33
Reputation: 44428
Your output
variable should be File
instead of OutputStream
.
Remember, that the constructor of FileOutputStream
is legal only as the following ones:
new FileOutputStream(File, boolean)
new FileOutputStream(String, boolean)
Here is the correct method declaration:
public void insertRowsToFile(File output) throws IOException {
...
}
Edit, Untested: Or the better way would be keeping OutputStream
as the parameter but change the initialization of the out
variable:
public void insertRowsToFile(OutputStream output) throws IOException {
...
PrintWriter out = new PrintWriter(new OutputStreamWriter(output));
...
}
Edit 2: A difference between OutputStream
and File
classes:
OutputStream
: Is an abstract class that accepts output bytes and sends them to some sink.File
: Is an class is an abstract representation of file and directory pathnames.Source from: TutorialsPoint.com
Upvotes: 1