Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (woodrow73):

Java + PrintWriter class Question:

OpenStudy (woodrow73):

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.

OpenStudy (lyrae):

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 '/'.

OpenStudy (woodrow73):

Thanks for the reply! I'll test it out

OpenStudy (woodrow73):

Do you think it has to be through c drive? I guess it's ok, since desktop is in there too.

OpenStudy (woodrow73):

It worked, thanks a bunch Lyrae. Also, besides using the escape sequence \\, a single forward slash also works / on my system.

OpenStudy (lyrae):

Great! Yw ^^

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!