0

Im new to java. I Created this code to get a better grasp and idea how files works. The code executes but the file never appears on my desktop, can someone tell me why and how i can correct this issue.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class CreateFile1{
    public static void main(String[] args) throws IOException{
    File f = new File("C:/Users/Solris/Desktop");
    

BufferedWriter output = new BufferedWriter(new FileWriter(f));
output.write("Testing");
output.close();
}
}
3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by javaAddict
0

You deliberately ignore any IOException, which is dumb, really dumb. Your file isn't getting written, and you chose not to display any error messages that Java may have about it? Get rid of the throws clause. Put your code in a try/catch block, and print out any exceptions that get thrown.
Exceptions are there to HELP you.
ps Desktop is a directory, not a file.

0

Because: "C:/Users/Solris/Desktop" is NOT a file. It is a Directory.
Shouldn't you do something like this: File f = new File("C:/Users/Solris/Desktop/file.txt");

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.