This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Working with Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Working with Files" Watch "Working with Files" New topic
Author

Working with Files

sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I need to create a path and a file if it does not exist

C:/test/test.txt

and write to it.

I have done this before, I just dont remember how I did it.

Thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Read the File API. It will have everything you need for getting a File, checking if it exists, and making the directories required if not. Writing to it is done via a FileOutputStream.


Steve
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
I did read the API. But when I do:

File f = new File("C:/test/test.rdf");

I get IO Exception The system cannot find the path specified.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1449
    
    6

I guess you are giving the path in a wrong way, it should be


Swastik
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

sonia pandit wrote:I did read the API. But when I do:

File f = new File("C:/test/test.rdf");

I get IO Exception The system cannot find the path specified.


That line of code can not throw an IOException because the File constructor is not defined to do so. The exception, therefore, must be coming from someplace else. Before you get to the line that actually causes the error you need to make sure the file exists. The File API provides the methods to do that. So read the REST of the File class' API. There are specific methods to
1) check if the file whose path you defined exists
2) create the file if it does not exist
3) Create the folders required (note that you should call these methods on the target File's parent, which should be a directory)
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137

I get java.io.FileNotFoundException C:\test\test.rdf (The system cannot find the path specified) and it looks like it created a file "test" in c:\
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Do you have more code than that that you are not showing.

When I make your code actually compilable and run it, here is what I get:

1) If the folder C:\test already exists, then the file will be created properly
2) If the folder C:\test does not already exist, then I get "java.io.IOException: The system cannot find the path specified" on the f.file.createNewFile().

This problem when C:\test does not exist occurs because createNewFile() creates the file, not the folder path. You need to get the file's parent file and make the directories from it.

What I can't seem to get is a FileNotFoundException, and the API for File doesn't seem to have any methods which throw one. So that exception must also be coming from code you aren't showing us.
sonia pandit
Ranch Hand

Joined: Apr 19, 2008
Posts: 137
It was because the directory c:\test did not exist.
I just changed the code to this:


Then I use the String to write to the file with a:
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Go back to the API documentation. Just below the mkdir() method is another method which would have been more practical than what you did there.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

sonia pandit wrote:It was because the directory c:\test did not exist.
I just changed the code to this:

Note that you must escape backslashes \ in string literals by doubling them: \\
Otherwise the backslash will be regarded as the beginning of an escape sequence. \t means the tab character.
So "C:\test" means C:<TAB>est and that is not what you want.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Working with Files
 
Similar Threads
Cant use object from another class
WA #1.....word association
Posting to bump up my count...
Text box value is not being completely filled with request attribute
The x900 effect ?