I’m trying to write a simple java program in Eclipse that prints these four lines into a file “hello.txt”. THe problem is, that nothing is happening, it doesn’t create a new file, and if i make a file called “hello.txt” the program doesn’t overwrite it. What am I doing wrong? Thanks for your answers. 🙂
import java.io.*;
public class output {
{
try{
PrintStream output = new PrintStream(new File("hello.txt"));
output.println("Hello World!");
output.println("this is ");
output.println("four lines of");
output.println("text.");
}catch(FileNotFoundException e){
System.out.println("Cannot write file!");
}
}
}
I just run your code on windows putting it in main method and it works (it creates the file). try with an absolute path, perhaps you are checking the wrong directory.
You also should call