Results 1 to 4 of 4
Thread: FileInputStream
- 12-11-2009, 02:34 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
FileInputStream
Hi, I'm trying to understand how the syntax of FileInputStream works... I'm supposed to get input from the user of a name, then search a text document, which has a name on each line, to see whether that name is in the directory. How can i achieve this with FileInputStream.. can someone show my some example code?
- 12-11-2009, 03:12 AM #2
Well, the FileInputStream itself is useful once we know a File object, or a String name of a file is a file that we want to read from, once it is created it works like a simple InputStream.
The File object has methods in it to test if the file exists, and is a valid file, or a directory.
for example,
Java Code:String theFileName="aFile.txt"; // this was read in from the user. File theFille = new File(theFileName); if (theFile.exists() ) { // the user has entered a name that is a valid file. if (theFile.isDirectory() ) { // the file is a directory. } else { // the file is not a directory, possibly a file. } } else { // the user has entered a name that is not a file or directory that exists. }
- 12-11-2009, 03:50 AM #3
trying to understand how the syntax of FileInputStream works...
can someone show my some example code?
Java Code:import java.io.*; public class Test { public static void main(String[] args) { String path = "Test.java"; try { File file = new File(path); BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(file))); String line; while((line = br.readLine()) != null) { System.out.println(line); } br.close(); } catch(IOException e) { System.out.println("read error: " + e.getMessage()); } } }
- 12-11-2009, 06:07 PM #4
You could also use a scanner attached to a FileInputStream.
Scanner in = new Scanner(new FileInputStream(new File("myFile.txt")));
Then traversing through the file is done with a while loop
while(in.hasNext()){
//push the name that's returned using in.nextLine or in.next
}Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Similar Threads
-
FileReader Vs FileInputStream and same goes to output
By unhurt in forum New To JavaReplies: 5Last Post: 02-02-2010, 10:06 AM -
NullPointerException in FileInputStream text file
By cheskers11 in forum EclipseReplies: 1Last Post: 11-19-2009, 02:38 PM -
FileOutputStream > int > FileInputStream
By dudejonne in forum New To JavaReplies: 11Last Post: 11-11-2009, 05:03 PM -
Empty FileInputStream..
By dudejonne in forum New To JavaReplies: 5Last Post: 11-08-2009, 09:21 PM -
There's not enough memory to process the command. at java.io.FileInputStream
By mary in forum New To JavaReplies: 1Last Post: 08-03-2007, 07:17 PM
Bookmarks