Results 1 to 3 of 3
  1. #1
    Roy Gardiner is offline Member
    Join Date
    Oct 2008
    Location
    London, England
    Posts
    3
    Rep Power
    0

    Default newbie. Reading a file, deprecated method

    I read and (for the sake of this example) print a file:
    Java Code:
    File file = new File("fu.bar");
    FileInputStream fis = null;
    DataInputStream dis = null;
    try {
         fis = new FileInputStream(file);
         dis = new DataInputStream(fis);
    
         while (dis.available() != 0) {
            System.out.println(">" + dis.readLine()+ "<");
         }
    The Java documentation (can't post link) says
    readLine() Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
    DataInputStream d = new DataInputStream(in);
    with:
    BufferedReader d
    = new BufferedReader(new InputStreamReader(in));
    but if I do this the available() method isn't, ahem, available, and I can't see what the equivalent is. Help appreciated. Pointer to alternative method of reading file also appreciated.

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

    Default

    BufferedReader returns null when it reaches the end–of–file.
    Java Code:
    File file = new File("fu.bar");
    BufferedReader br = null;
    try {
         br = new BufferedReader(
              new InputStreamReader(
              new FileInputStream(file)));
         String line;
         while ( (line = br.readLine()) != null ) {
            System.out.println(">" + line + "<");
         }

  3. #3
    Roy Gardiner is offline Member
    Join Date
    Oct 2008
    Location
    London, England
    Posts
    3
    Rep Power
    0

    Default

    Oh dear, a real RTFM problem -- although I didn't see anything in the FM. Betcha I will when I go back :mad:.

    Thanks for such a full answer, I appreciate it.

Similar Threads

  1. getYear deprecated method
    By ravian in forum New To Java
    Replies: 5
    Last Post: 01-05-2011, 08:50 AM
  2. deprecated method.. help!
    By iuna in forum Java Applets
    Replies: 11
    Last Post: 09-08-2008, 08:38 PM
  3. Right use of file reading ?
    By jurka in forum New To Java
    Replies: 3
    Last Post: 08-27-2008, 08:16 PM
  4. method size is deprecated
    By oregon in forum New To Java
    Replies: 4
    Last Post: 08-05-2007, 05:59 PM
  5. Newbie help with a file parser
    By adlb1300 in forum New To Java
    Replies: 2
    Last Post: 07-24-2007, 04:31 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
  •