Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / March 2005

Tip: Looking for answers? Try searching our database.

Java Help -- File I/O

Thread view: 
az - 10 Mar 2005 06:39 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.