Solved

Writing to a file in a specific directory

Posted on 2006-11-10
2
216 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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
network + 7 72
create a gui in perl 3 42
JUnit 4 @Before and @BeforeClass differences 3 18
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
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:

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now