Solved

Writing to a file in a specific directory

Posted on 2006-11-10
2
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

751 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