Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Writing to a file in a specific directory

Posted on 2006-11-10
2
Medium Priority
?
260 Views
Last Modified: 2010-03-31
Hello, i'm using BufferedFileWriter and BufferedWriter to create and write to files.  Right now the files are just written to the directory where the source code is.  I know how to create a directory, but how do I get the files to be written into that new directory?

Thanks,
0
Comment
Question by:twibblejaway
2 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 2000 total points
ID: 17917942
Mention the path in the name. For instance:

BufferedWriter bw=new BufferedWriter(new FileWriter("c:\\dir1\\file.txt"));

will put the in dir1 under the root. A relative file name should also work. The possible uncertainty there is in knowing what the program considers to be the current directory.
0
 

Expert Comment

by:alexxf
ID: 17918096
In order to use a FileWriter to write to a file, you have to create a File
object. You pass this File object to the writer.

The location of the file is determined at the creation of the File object.
Check out http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html

So, to create a file named foo.out in /home/user/temp you need to create a File like this:
File f = new File("/home/user/temp/foo.out");

This instance of File is passed to a FileWriter like this:
FileWriter fw = new FileWriter(f);

This can be wrapped in a BufferedWriter like this
BufferedWriter bw = new BufferedWriter(fw);

You can wrap a PrintWriter around that like this:
PrintWriter out = new PrintWriter(bw);

Or in short (see
http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedWriter.html):
PrintWriter out
   = new PrintWriter(new BufferedWriter(new FileWriter("/home/user/temp/foo.out")));
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question