If the directory doesn't exist you need to create it. Java won't create it by itself since the File class is just a link to an entity that can also not exist at all.
As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see
FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
You should try creating a path for the folder it contains before:
File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();
PrintWriter printWriter = new PrintWriter(file);
Answer from Jack on Stack OverflowVideos
If the directory doesn't exist you need to create it. Java won't create it by itself since the File class is just a link to an entity that can also not exist at all.
As you stated the error is that the file cannot be created. If you read the documentation of PrintWriter constructor you can see
FileNotFoundException - If the given string does not denote an existing, writable regular file and a new regular file of that name cannot be created, or if some other error occurs while opening or creating the file
You should try creating a path for the folder it contains before:
File file = new File("C:/Users/Me/Desktop/directory/file.txt");
file.getParentFile().mkdirs();
PrintWriter printWriter = new PrintWriter(file);
import java.io.PrintWriter;
import java.io.File;
public class Testing {
public static void main(String[] args) throws IOException {
File file = new File ("C:/Users/Me/Desktop/directory/file.txt");
PrintWriter printWriter = new PrintWriter ("file.txt");
printWriter.println ("hello");
printWriter.close ();
}
}
throw an exception for the file.
According to coderanch.com, if we combine the answers we get:
FileWriter is the character representation of IO. That means it can be used to write characters. Internally FileWriter would use the default character set of the underlying OS and convert the characters to bytes and write it to the disk.
PrintWriter & FileWriter.
Similarities
- Both extend from Writer.
- Both are character representation classes, that means they work with characters and convert them to bytes using default charset.
Differences
- FileWriter throws IOException in case of any IO failure, this is a checked exception.
- None of the PrintWriter methods throw IOExceptions, instead they set a boolean flag which can be obtained using checkError().
- PrintWriter has an optional constructor you may use to enable auto-flushing when specific methods are called. No such option exists in FileWriter.
- When writing to files, FileWriter has an optional constructor which allows it to append to the existing file when the "write()" method is called.
Difference between PrintStream and OutputStream: Similar to the explanation above, just replace character with byte.
PrintWriter has following methods :
close()
flush()
format()
printf()
print()
println()
write()
and constructors are :
File (as of Java 5)
String (as of Java 5)
OutputStream
Writer
while FileWriter having following methods :
close()
flush()
write()
and constructors are :
File
String
Link: http://www.coderanch.com/t/418148/java-programmer-SCJP/certification/Information-PrintWriter-FileWriter
Both of these use a FileOutputStream internally:
public PrintWriter(File file) throws FileNotFoundException {
this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
false);
}
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}
but the main difference is that PrintWriter offers special methods:
Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams.
Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println, printf, or format methods is invoked, rather than whenever a newline character happens to be output. These methods use the platform's own notion of line separator rather than the newline character.
So I made some code to test a theory, and it disproved my theory. Here it is:
import java.io.PrintWriter;
public class Main {
public static void main(String[] args) {
PrintWriter writer = new PrintWriter(System.out);
System.out.println("HI");
writer.println("HI");
writer.flush();
}
}Now here's the context for my question. I was taught that System.out is a stream object, and that the console grabs from the System.out stream and puts those contents on its stream. I was then taught that PrintWriter is an class that functions essentially identically to System.out, except that you can direct your destination stream. So I hypothesized, "If you made a PrintWriter object and sent the System.out stream in as its destination, would that be identical to just using System.out?
As it turns out, no, but I still don't fully understand why. What I do understand is that if I use the flush() method, it does essentially work the same. So my question is this:
When you use System.out.println() , does the method println() also tell the console to grab from the buffer so its not left in the System.out stream? And from the other perspective, when you use writer.println() (writer being the PrintWriter object in this context), does the println() method NOT tell the destination stream to grab from the buffer (until the flush() method is used)?
Any information helps, feel free to tell me that my interpretation is wrong or that my hypothesis is wrong. Or you can tell me anything really, tell me how your day is going. Any information helps, the more I know the better a programmer I can become :)