Solved

cannot get rid of the message Java Deprecated Class

Posted on 2007-03-23
9
331 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
 

Author Comment

by:salasadi
ID: 18780600
it works but the first line in the file is ignored!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Receive file in Servlet 1 36
powerN  challenge 3 47
array6 challenfge 6 63
topping1 challenge 7 51
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

705 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

14 Experts available now in Live!

Get 1:1 Help Now