Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java uses or overrides a deprecated API error

Hello so this is my code, somehow I still get error down there I described error, any help would be appreciated. I am not really expert with import of this and especially API itself

   import java.io.BufferedInputStream;
    import java.io.DataInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.IOException;

    public class MyClass {

        public static void main(String[] args) throws IOException {
            File file = new File("Sinatra.txt");
            FileInputStream fis = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(fis);
            DataInputStream dis = new DataInputStream(bis);

            if (dis.available() != 0) {
                // Get the line.
                String s = dis.readLine();
                // Put words to array.
                String[] sParts = s.split(" ");
                // Initialize word longest length.
                int longestLength = 1;
                for (String strx : sParts) { // Go through each sPart, the next one is called strx
                    // If the document has word longer than.
                    if (longestLength < strx.length())
                        // Set new value for longest length.
                        longestLength = strx.length();
                }
                // Because array index from "0".
                int[] counts = new int[longestLength + 1];
                for (String str : sParts) {
                    // Add one to the number of words that length has
                    counts[str.length()] += 1;
                }
                // We use this type of loop since we need the length.
                for (int i = 1; i < counts.length; i++) {
                    System.out.println(i + " letter words: " + counts[i]);
                }
            }
        }
    }

    // Result:
    //        1 letter words: 0
    //        2 letter words: 2
    //        3 letter words: 0
    //        4 letter words: 1
    //        5 letter words: 0
    //        6 letter words: 2
    //        7 letter words: 2
    //        8 letter words: 0
    //        9 letter words: 3

Hello this is my code and when I try to compile it I get error saying:

Note: MyClass.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

Thanks for help :)

like image 345
Anton9988 Avatar asked Aug 01 '15 13:08

Anton9988


People also ask

What is deprecated API in java?

A deprecated API is one that you are no longer recommended to use, due to changes in the API. While deprecated classes, methods, and fields are still implemented, they may be removed in future implementations, so you should not use them in new code, and if possible rewrite old code not to use them.

What is deprecated error in java?

Deprecation is a notification to library consumers that they should migrate code from a deprecated API. In the JDK, APIs have been deprecated for widely varying reasons, such as: The API is dangerous (for example, the Thread. stop method).

What is Xlint deprecation in java?

By "recompile with -Xlint", the compiler means to inform you that you need to recompile your program like this: javac -Xlint abc.java. If you do so, the compiler will tell you which methods are deprecated so you can remove your calls to them.


2 Answers

Those are not errors, they are warning, your code compiled.

To explain these lines :

Note: MyClass.java uses or overrides a deprecated API.

You are doing a call of DataInputStream#readLine which is deprecated since JDK 1.1 as per the documentation :

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));

As for the second line :

Note: Recompile with -Xlint:deprecation for details.

It simply tells you the option to use when compiling to get more details about where you are using deprecated stuff.

Edit :

As per your comment, here how your code would looks like :

import java.io.InputStreamReader;//Add these two imports
import java.io.BufferedReader;
...
BufferedReader br = new BufferedReader(new InputStreamReader(bis));//Use BufferedReader as suggested by the doc instead of DataInputStream
...
String s = br.readLine();//Read with the non-deprecated readLine of BufferedReader
like image 162
Jean-François Savard Avatar answered Oct 24 '22 18:10

Jean-François Savard


From the DataInputStream API documentation:

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));

So as you can see, in line String s = dis.readLine() in your code, you use a deprecated method. This means that there is a better option for doing what you're doing (as highlighted above). Even though the method is deprecated, it likely will work in some cases. But it isn't guaranteed to fulfill its contract anymore, and therefore it is better to use the similar but consistent BufferedReader.readLine() method.

like image 36
connorp Avatar answered Oct 24 '22 18:10

connorp