Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    5
    Rep Power
    0

    Default help with input reader

    ok so i have this input reader code which works fine if you want it to out print to a console but i wanted it to output the data it read to a jtextarea in a gui in another class ive tryed everything no look wondering if anyone could help me? heres my code for my text reader:



    Java Code:
    import java.io.*;
    
    public class FileInput {
        
        public void readFile(){
            File file = new File("num1.dat");
            FileInputStream fileInputStream = null;
            BufferedInputStream bufferedInputStream = null;
            DataInputStream dataInputStream = null;
            
            try {
                fileInputStream = new FileInputStream(file);
                
                bufferedInputStream = new BufferedInputStream(fileInputStream);
                dataInputStream = new DataInputStream(bufferedInputStream);
                
                // returns 0 when it has reached the end of the file
                while (dataInputStream.available() != 0) {
                       System.out.println(dataInputStream.readInt());
                    }              
                // closes the stream
                fileInputStream.close();
                bufferedInputStream.close();
                dataInputStream.close();
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
        
    }

    Thanks in advanced.

  2. #2
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    import java.io.*;

    public class FileInput {

    public void readFile(){
    File file = new File("num1.dat");
    FileInputStream fileInputStream = null;
    BufferedInputStream bufferedInputStream = null;
    DataInputStream dataInputStream = null;

    try {
    fileInputStream = new FileInputStream(file);

    bufferedInputStream = new BufferedInputStream(fileInputStream);
    dataInputStream = new DataInputStream(bufferedInputStream);

    // returns 0 when it has reached the end of the file
    String text="";
    while (dataInputStream.available() != 0) {
    text+=String.valueOf(dataInputStream.readInt());
    /*System.out.println(dataInputStream.readInt()); cut this off*/
    }
    // closes the stream
    fileInputStream.close();
    bufferedInputStream.close();
    dataInputStream.close();

    JTextArea ta=new JTextArea();
    ta.setText(text);
    /*That's it )))*/
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    }

    }
    Do you see the way you should work at? Report.

  3. #3
    Webuser is offline Senior Member
    Join Date
    Dec 2008
    Posts
    526
    Rep Power
    0

    Default

    By the way it is quite enough you to use only...

    DataInputStream is=new DataInputStream(new FileInputStream("num1.dat"));
    instead of all those...

    File file = new File("num1.dat");
    FileInputStream fileInputStream = null;
    BufferedInputStream bufferedInputStream = null;
    DataInputStream dataInputStream = null;
    )))

Similar Threads

  1. how do i get input from barcode reader?
    By anthrax in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 04-09-2009, 03:48 PM
  2. Selecting an input file for Reader
    By myazuid in forum New To Java
    Replies: 3
    Last Post: 11-25-2008, 01:21 AM
  3. Maze reader
    By bix in forum New To Java
    Replies: 1
    Last Post: 11-20-2008, 06:06 PM
  4. A simple DOM reader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 09:24 AM
  5. how to take input and verify input in Java programs
    By bilal_ali_java in forum Advanced Java
    Replies: 0
    Last Post: 07-21-2007, 08:46 AM

Posting Permissions

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