Java Help -- File I/O

A

az

I have two questions regarding file I/O:

(1) If I want to open data.txt in C:\test directory, how to pass
directory name to FileInputStream?

I know how to open data.txt in current directory:
FileInputStream in = new FileInputStream("data.txt")

How to pass C:\test to FileInputStream?

Is the following code right?
FileInputStream in = new FileInputStream("C:\test\data.txt")


(2) How to get a list of file names under C:\test directorr? I want to
get a vector of file names under C:\test directory. How to do this?

Thanks for your help!
 
J

Jouko Johansson

Hi,

Have you checked the java.io.File -class?

File file = new File("filename.txt");

FileInputStream fis = new FileInputStream(file);

works also with the directories.

File dir = new File("C:\\test");

and the files in the directory :

File dir = new File("C:\\test");

String files [] = dir.list();

and then convert String-array to Vector.

If you use File-class you can test the file existence and if the given
filename is a directory etc...

I don't know why you want to open directory for reading, I usually open file
for reading / writing?

Hopefully this helps.

- Jouko
 
S

Sameer

How to create a File object for a directory contained in the current
directory?
We usually place the resources like fonts, gif images in a RSC direcory
in the current directory.
I dont want to include full path name of the folder as a parameter.
I tried the following but it wont work:

File file= new File("./RSC");

File file= new File("RSC");

File file= new File(".\RSC");

Please give OS independant representation for the same.
-Sameer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top