• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Working with Files

 
sonia pandit
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
sonia pandit
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1603
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you are giving the path in a wrong way, it should be
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20998
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic