Results 1 to 4 of 4

Thread: FileInputStream

  1. #1
    pachufir is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default 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?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    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.
    }

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    10

    Default

    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());
            }        
        }
    }

  4. #4
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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

  1. Replies: 5
    Last Post: 02-02-2010, 10:06 AM
  2. NullPointerException in FileInputStream text file
    By cheskers11 in forum Eclipse
    Replies: 1
    Last Post: 11-19-2009, 02:38 PM
  3. FileOutputStream > int > FileInputStream
    By dudejonne in forum New To Java
    Replies: 11
    Last Post: 11-11-2009, 05:03 PM
  4. Empty FileInputStream..
    By dudejonne in forum New To Java
    Replies: 5
    Last Post: 11-08-2009, 09:21 PM
  5. Replies: 1
    Last Post: 08-03-2007, 06:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •