Solved

Writing to a file in a specific directory

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

617 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