The book I'm using says it should look something like this
import java.io.*;
public class main
{
public static void main(String[] argv)
{
String fil = "CormeOn.bat";
FileWriter fw = null;
try {
fw = new FileWriter(fil);
}
catch(FileNotFoundException fnf)
{
System.out.println("Yea...."+fnf);
}
catch (IOException e)
{
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter utFil = new PrintWriter(bw);
utFil.print("@echo off");
utFil.println("ping google.com");
utFil.println();
utFil.print("echo woop-da-woop");
utFil.close();
}
}
1. Is this way outdated or is there a better way to write to/from files?
2. What does it mean when you write throws IOException after the main(...) here {...}?
The try-catch isn't needed then it seems.
3. If the file doesnt exist, it creates it. So how do you use FileNotFoundException..?
4. Why is it good to use a bufferedwriter and how does it work? I mean, you never directly use it?
Some website showed an example with BufferedWriterVariable.write