This discussion is archived
7 Replies Latest reply: Apr 8, 2008 6:28 AM by 807601 RSS

FileInputStream

807601 Newbie
Currently Being Moderated
I need a method that reads a text file as a bytestream and then returns it as a string. My assignment says specifically to use FileInputStream instead of a reader.
The method below works, but it leaves out the first character of the textfile it reads. Does anyone have a suggestion to what's wrong?
public static String readDataFromFile(File fileIn) throws IOException{
           
           FileInputStream streamIn   = new FileInputStream(fileIn);
           String msg = "";
              
         while (streamIn.read() != -1) {
 
              byte[] data = new byte[streamIn.available()];
              streamIn.read(data);
              msg = new String(data, "ISO-8859-1");
             }
 
         streamIn.close();
         return msg;
 
 }
  • 1. Re: FileInputStream
    752426 Newbie
    Currently Being Moderated
    Check this line. The value returned by read method is getting lost.
     while (streamIn.read() != -1) {
  • 2. Re: FileInputStream
    807601 Newbie
    Currently Being Moderated
    Opening the loop, you read (and discard) the first character:
    while (streamIn.read() != -1) {
    Perhaps do something with the character you read, or change the way the loop works...

    Good luck!
  • 3. Re: FileInputStream
    807601 Newbie
    Currently Being Moderated
    Any suggestion to how i can create a loop without the first character being discarded?
  • 4. Re: FileInputStream
    800308 Newbie
    Currently Being Moderated
    What does streamIn.read() do?

    Edited by: corlettk on 8/04/2008 13:25 - too slow
  • 5. Re: FileInputStream
    800308 Newbie
    Currently Being Moderated
    package forums;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;
    class FileReaderator
    {
      public static void main(String[] args) {
        try {
          System.out.println(read(new File("FileReaderator.java")));
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    
      public static String read(File file) throws IOException {
        String result = "";
        FileInputStream input = null;
        try {
          input = new FileInputStream(file);
          while ( input.available() > 0 ) {
            byte[] bytes = new byte[input.available()];
            input.read(bytes);
            result = new String(bytes, "ISO-8859-1");
          }
        } finally {
          if(input!=null)input.close();
        }
        return result;
      }
    
    }
    Edited by: corlettk on 8/04/2008 13:26 - note that this is VERY poor way to read a text-file... much better to read and process a line at time wherever possible... to minimise the amount of memory required... the above will fail on a 500 meg file, unless you blow-out the JVM's default memory settings to cater for it... but yuck!
  • 6. Re: FileInputStream
    807601 Newbie
    Currently Being Moderated
    Thanks for your help. It works now!
  • 7. Re: FileInputStream
    EJP Guru
    Currently Being Moderated
    while ( input.available() > 0 ) {
    This line is redundant.
    byte[] bytes = new byte[input.available()];
    This is a misuse of available(). Use File.length().