-
Help here!!
what's wrong in this code:
import java.io.*;
public class WriteFile{
public static void main(String[] args) throws IOException{
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
FileOutputStream fop = new FileOutputStream(f);
File f = new File("textfile1.txt");
System.out.print("Enter your name: ");
String winner1 = input.readLine();
System.out.print("Enter name1");
String winner2 = input.readLine();
System.out.print("Enter name2");
String winner3 = input.readLine();
System.out.print("Enter name3");
String winner4 = input.readLine();
System.out.print("Enter name4");
if(f.exists()){
String str= winner1;
String str2= winner2;
String str3= winner3;
String str4= winner4;
fop.write(str.getBytes());
fop.write(str2.getBytes());
fop.write(str3.getBytes());
fop.write(str4.getBytes());
fop.flush();
fop.close();
System.out.println("The data has been written");
}
else
System.out.println("This file is not exist");
}
}
-
Re: Help here!!
Welcome to the Forum, please read the welcome message in the forum announcements on how to ask for help.
Well, variable f seems to be used before it's defined.
once you fix that, you could use some polishing with the print statements, adding spaces where needed...
also, I don't think you'll need the else block, the file was created on my computer.