Java + PrintWriter class Question:
When you create a new notepad document using PrintWriter, you specify the name of the file that you're making in the PrintWriter's constructor like so: PrintWriter pVariable = new PrintWriter("newFile.txt"); When the file is run, a file called newFile.txt is created inside of the folder of the program that you made it in. Let's say my program is called test.java, if I go to my files, I can see the new notepad file newFile.txt in this path: C:\eclipse\javaProjects\test\newFile How can I make a notepad document save in a different location through the PrintWriter class? Like onto the desktop for instance.
You enter the relative or full path to that location: ``` new PrintWriter("C:\\Users\\Lyrae\\Desktop\\newFile.txt"); ``` or ``` new PrintWriter("..\\..\\relative\\path\\to\\newFile.txt"); ``` On Windows the path separator is a backslash '\' which require escaping and becomes \\. On *nix systems the path separator is a regular slash '/'.
Thanks for the reply! I'll test it out
Do you think it has to be through c drive? I guess it's ok, since desktop is in there too.
It worked, thanks a bunch Lyrae. Also, besides using the escape sequence \\, a single forward slash also works / on my system.
Great! Yw ^^
Join our real-time social learning platform and learn together with your friends!