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!
kamal - 10 Mar 2005 07:45 GMT
1). Just use the path to the file as;
FileInputStream a = new FileInputStream("C:\\test\\data.txt"); as \ is
a escape character in Java.
2).
String dir = "C:\\test"
File dir = new File(dir);
File[] files = dir.listFiles();
This gives an array of files. Hope you can use the array instead of a
vector. If a vector is required just put your array elements into the
vector.
Sameer - 11 Mar 2005 12:58 GMT
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
Fahd Shariff - 10 Mar 2005 10:07 GMT
(1) Did you try this:
FileInputStream in = new FileInputStream("C:\test\data.txt") ;
Did it work?
(2) Try:
File dir = new File("C:\test");
String[] fileNames = dir.list() ;
Returns an array of strings naming the files and directories in the
directory denoted by this abstract pathname
Also see listFiles() and consult the API!
--
Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking... "
Wannabee - 10 Mar 2005 10:49 GMT
"Fahd Shariff"
> FileInputStream in = new FileInputStream("C:\test\data.?txt") ;
FileInputStream in = new FileInputStream("C:\\test\\data.?txt") ;
> File dir = new File("C:\test");
File dir = new File("C:\\test");
Must be two backslashes "\\" in sourcecode if you want to get one backslash
inside a String.
sandy - 10 Mar 2005 10:22 GMT
try this
c:/test/data.txt
Sameer - 10 Mar 2005 14:07 GMT
It is better if you give it in the form of a URL:
A general URL for HTTP protocol is
http://groups-beta.google.com/group/comp.lang.java.programmer
Like this FILE is also a Protocol.
Why dont you try passing it like this...
file:///C|/test/data.txt
This is the language of web and is less dependant on your OS.
I think this will also do if you type it as (on a windows machine):
C:\\test\\data.txt
-Sameer Shinde