Solved

cannot get rid of the message Java Deprecated Class

Posted on 2007-03-23
9
349 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

816 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now