Solved

Writing to a file in a specific directory

Posted on 2006-11-10
2
235 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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

777 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