Solved

cannot get rid of the message Java Deprecated Class

Posted on 2007-03-23
9
376 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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:

707 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