Solved

cannot get rid of the message Java Deprecated Class

Posted on 2007-03-23
9
360 Views
Last Modified: 2010-03-31
I have simple class to read from text file.  the clas work ok, but the "deprecation" warrning is bugging me :(

C:\>javac FileInput.java -Xlint
FileInput.java:29: warning: [deprecation] readLine() in java.io.DataInputStream
has been deprecated
        System.out.println(dis.readLine());
                              ^
1 warning

C:\>javac FileInput.java
Note: FileInput.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

here is the class:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileInput {

  public static void main(String[] args) {
    File file = new File("C:\\que.sql");    
    try {
      fis = new File(file);
        
        BufferedReader in = new BufferedReader(new InputStreamReader(fis));
                
      while (in.available() != 0) {

     
        System.out.println(in.readLine());
      }

      fis.close();
     
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
0
Comment
Question by:salasadi
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 18779580
that error looks like its coming from a different class that the one you posted.

To get rid of it use a BufferedReader instead of a DataInputStream (as you have done in the posted code)
0
 

Author Comment

by:salasadi
ID: 18780205
It was duplication .java file that confuse me.  I got it to work now, but some how the code loop after reach the null instead stopped:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.*;
import java.text.*;

public class FileInput {
  public static void main(String[] args) {
    File file = new File("C:\\enabled.sql");    
    try {
        BufferedReader in = new BufferedReader(new FileReader(file));
         String line = in.readLine();
            while(line !=null) {      
                  System.out.println(in.readLine());
            }      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
0
 
LVL 1

Expert Comment

by:alban4
ID: 18780476
Replace:
         String line = in.readLine();
            while(line !=null) {      
                  System.out.println(in.readLine());
            }      

With:
         String line;
            while((line = in.readLine()) !=null) {      
                  System.out.println(line);
            }      
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:salasadi
ID: 18780600
it works but the first line in the file is ignored!
0
 
LVL 1

Expert Comment

by:alban4
ID: 18780621
Make sure you're NOT declaring:
    String line = in.readLine();

It should be simply:
    String line;
0
 
LVL 20

Expert Comment

by:gatorvip
ID: 18781319
>>it works but the first line in the file is ignored!

Well, yes:

String line = in.readLine();   // read the first line
while(line !=null) {      
   System.out.println(in.readLine());  // read line, then print it
}

Nowhere do you actually print out the first line. You can either do what alban4 suggests, or make this modification :
String line = in.readLine();
System.out.println(line));  
while.....
0
 

Author Comment

by:salasadi
ID: 18781380
I modified the class as you suggested:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.*;
import java.text.*;
public class FileInput {
  public static void main(String[] args) {
    File file = new File("C:\\test.txt");    
    try {
        BufferedReader in = new BufferedReader(new FileReader(file));
            while(in.readLine() !=null) {  
                  
                  System.out.println(in.readLine());
            }      
            in.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

here is my text file:

the is 1st test for read line
this is 2nd test
this is third test
this is 4th test

here is the java output:

C:\>java FileInput
this is 2nd test
this is 4th test

somehow I am skipping lines!

0
 
LVL 20

Assisted Solution

by:gatorvip
gatorvip earned 200 total points
ID: 18781454
You are skipping lines because you read twice, but only print once...


while(in.readLine() !=null) {       // you read once here, but you don't print
  System.out.println(in.readLine());  // you read again here, and you do print
}  
0
 
LVL 1

Accepted Solution

by:
alban4 earned 300 total points
ID: 18781510
Modify the code as I suggested above:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.io.*;
import java.text.*;
public class FileInput {
  public static void main(String[] args) {
    File file = new File("C:\\test.txt");    
    try {
        BufferedReader in = new BufferedReader(new FileReader(file));
        String line;
        while((line = in.readLine()) !=null) {      
            System.out.println(line);
         }    
          in.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 57
Opening PDF on button click and fill new document 2 47
check java version using powershell 13 177
ejb wildfly example 2 25
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question