Thread: Reading Files

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    13

    Reading Files


    Java has so many streams to use im lost


    I need to read a text file into a variable so far ive got this


    Code:
    
    InputStream fis = new FileInputStream(f); 
    
    while ((x = fis.read())!= -1) {
                System.out.print(x); 
                }

    This outputs binary data, how do i get acsii data into a Srting variable


    Is this the best method for reading text files on windows


    Mark
    100 trillion calculations per nanosecond
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Oct 2000
    Location
    Back in the real world.
    Posts
    5,966
    Rep Power
    190
    binary data==ascii data.

    Code:
    InputStream fis = new FileInputStream(f); 
    
    while ((x = fis.read())!= -1) {
                s=new String(x); 
                            if (s.equals("your-data-here")) {
                                     (do processing.....)
                            }
                }
    does this help?

    anyway, not sure 100% but there should be a StringStream() - is there? (i am not a java pro......) but i am sure there is a LineReader() class that you should have a look at
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2001
    Location
    On a screen near you
    Posts
    498
    Rep Power
    13
    Its not what i wanted


    Code:
    
    s = new String(x);

    This line causes an error because x is binary data


    I need the binary data converted to acsii


    Mark
    100 trillion calculations per nanosecond
  6. #4
  7. SwollenMember
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    the master control
    Posts
    264
    Rep Power
    15
    would using java.lang.Byte help?

    then you could just say:

    Byte.toString(x);
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2002
    Location
    India
    Posts
    7
    Rep Power
    0
    InputStream fis = new FileInputStream(f);

    String data = "";

    do{
    i = fis.read();
    if (i != -1) data = data + (char)i;
    }while(i != -1);

    System.out.println(data);

    this will append all values from the file to variable data...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Posts
    34
    Rep Power
    13
    Why not do this,

    BufferedReader buf = new BufferedReader(new FileReader(f));


    System.out.println("buf.ReadLine);

IMN logo majestic logo threadwatch logo seochat tools logo