gpt4 book ai didi

java 使用或覆盖了已弃用的 API 错误

转载 作者:行者123 更新时间:2023-12-05 00:44:58 25 4
gpt4 key购买 nike

您好,这是我的代码,不知何故我仍然在下面遇到错误,我描述了错误,任何帮助将不胜感激。我不是很擅长导入这个,尤其是 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

您好,这是我的代码,当我尝试编译它时,我得到了
错误说:
Note: MyClass.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

感谢帮助 :)

最佳答案

这些不是错误,它们是警告,您的代码已编译。

要解释这些行:

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



您正在调用 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:


DataInputStream d = new DataInputStream(in);   

和:
BufferedReader d = new BufferedReader(new InputStreamReader(in));

至于第二行:

Note: Recompile with -Xlint:deprecation for details.



它只是告诉您在编译时使用的选项,以获取有关您在何处使用已弃用的东西的更多详细信息。

编辑:

根据您的评论,您的代码如下所示:
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

关于java 使用或覆盖了已弃用的 API 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31762417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com