This discussion is archived
6 Replies Latest reply: Nov 29, 2007 12:15 PM by 807603 RSS

Problem on reading and writing from from a *.txt file

807603 Newbie
Currently Being Moderated
I get Problem on reading and writing from from a *.txt file. The following is the read() method...
The software said the DataInputStream is depreciated. Can anyone help me please?
 public void read()
  {
    File file = new File("C://Documents and Settings//Charles//My Documents//Brunel//EE2065//Assignment and Lab//Assignment 4 and Lab 4//data.txt");
    FileInputStream in = null;
    String str = "";

    try
    {
      in = new BufferedReader(file);
      //in = new FileInputStream(file);
      for(;;)
      {
        str = new BufferedReader(in).readLine();
        //str = new DataInputStream(in).readLine();
        if(str == null)
        {
          break;
        }
        System.out.print(str);
      }
    in.close();
    }
    catch(IOException e)
    {
        System.err.println("execution error: " +e);
    }



  }
  • 1. Re: Problem on reading and writing from from a *.txt file
    796440 Guru
    Currently Being Moderated
    It's deprecated, not depreciated.

    No, DataInputStream is not deprecated, the method you're calling is. If you read the javadocs for that method, it will tell you what you should use instead. However, if you're just reading a text file, you shouldn't be using DataInputStream at all. You should just be using BufferedReader.
  • 2. Re: Problem on reading and writing from from a *.txt file
    807603 Newbie
    Currently Being Moderated
    Thank you for your reply. I have made some change. However, there is an incompetable type found error.
    in = new BufferedReader(new InputStreamReader(in));
    The following are all of the code.

     public void read()
      {
        File file = new File("C://Documents and Settings//Charles//My Documents//Brunel//EE2065//Assignment and Lab//Assignment 4 and Lab 4//data.txt");
        FileInputStream in = null;
        //BufferedReader in = null;
        String str = "";
    
        try
        {
          in = new BufferedReader(new InputStreamReader(in));
          //in = new FileInputStream(file);
          for(;;)
          {
            BufferedReader Bstr = new BufferedReader(new InputStreamReader(in));
            //str = new BufferedReader(in).readLine();
            //str = new DataInputStream(in).readLine();
            if(str == null)
            {
              break;
            }
            System.out.print(str);
          }
        in.close();
        }
        catch(IOException e)
        {
            System.err.println("execution error: " +e);
        }
    
    
    
      }
    
    
     
  • 3. Re: Problem on reading and writing from from a *.txt file
    796440 Guru
    Currently Being Moderated
    Read the error message closely. Look at your code closely. You do know that a variable's type has to be compatible with the object you assign to it, right? You can't do
    String str = new Date();
    for instance.
  • 4. Re: Problem on reading and writing from from a *.txt file
    807603 Newbie
    Currently Being Moderated
    Sorry for that, but I do not really understand what do you mean. Could you explain further please? ^.^
    String str = new Date();
  • 5. Re: Problem on reading and writing from from a *.txt file
    796440 Guru
    Currently Being Moderated
    Charles_Wong wrote:
    Sorry for that, but I do not really understand what do you mean. Could you explain further please? ^.^
    String str = new Date();
    This code is just an example. It has nothing to do with your code It will give you the same kind of error as you described above. Do you know why it will give you that error?
  • 6. Re: Problem on reading and writing from from a *.txt file
    807603 Newbie
    Currently Being Moderated
    I understand that the variable "str" is a String type and is not compatible if create a object using Date(). I will check my program later. If there is any problem, I will ask again. Thanks.