java 使用或覆盖已弃用的 API 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31762417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java uses or overrides a deprecated API error
提问by Anton9988
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
您好,这是我的代码,不知何故我仍然在那里出现错误,我描述了错误,任何帮助将不胜感激。我不是这个导入的专家,尤其是 API 本身
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 :)
感谢帮助 :)
采纳答案by Jean-Fran?ois Savard
Those are not errors, they are warning, your code compiled.
这些不是错误,而是警告,您的代码已编译。
To explain these lines :
解释这些行:
Note: MyClass.java uses or overrides a deprecated API.
注意:MyClass.java 使用或覆盖已弃用的 API。
You are doing a call of DataInputStream#readLine
which is deprecated since JDK 1.1 as per the documentation :
DataInputStream#readLine
根据文档,您正在调用自 JDK 1.1 以来已弃用的调用:
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:
已弃用。
此方法不能正确地将字节转换为字符。从 JDK 1.1 开始,读取文本行的首选方法是通过 BufferedReader.readLine() 方法。通过替换以下形式的代码,可以将使用 DataInputStream 类读取行的程序转换为使用 BufferedReader 类:
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.
注意:使用 -Xlint:deprecation 重新编译以获取详细信息。
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
回答by connorp
From the DataInputStream API documentation:
来自 DataInputStream API 文档:
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));
读行()
已弃用。
此方法不能正确地将字节转换为字符。从 JDK 1.1 开始,读取文本行的首选方法是通过 BufferedReader.readLine() 方法。通过替换以下形式的代码,可以将使用 DataInputStream 类读取行的程序转换为使用 BufferedReader 类:
DataInputStream d = new DataInputStream(in);
和:
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.
如您所见,在String s = dis.readLine()
您的代码中,您使用了不推荐使用的方法。这意味着有一个更好的选择来做你正在做的事情(如上面突出显示的)。尽管该方法已被弃用,但在某些情况下它可能会起作用。但它不再保证履行其合同,因此最好使用类似但一致的BufferedReader.readLine()
方法。