修复警告:使用或覆盖 Java 中已弃用的 API
本文我们将了解为什么警告说使用或覆盖已弃用的 API,并演示如何修复此问题以完成任务。
修复警告说使用或覆盖 Java 中已弃用的 API
示例代码(包含警告):
//import libraries
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
//Main class
public class Main {
//main method
public static void main(String[] args){
//path of a text file
File filePath = new File("Files/TestFile.txt");
try {
//obtain input bytes from a file
FileInputStream fileInputStream = new FileInputStream(filePath);
//adds the functionality to another input stream
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//lets an app read primitive Java data types from the specified input stream
DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
if (dataInputStream.available() != 0) {
// Get a line.
String line = dataInputStream.readLine();
// Place words to an array which are split by a "space".
String[] stringParts = line.split(" ");
// Initialize the word's maximum length.
int maximumLength = 1;
// iterate over each stingPart, the next one is addressed as "stringX"
for (String stringX : stringParts) {
// If a document contains the word longer than.
if (maximumLength < stringX.length())
// Set the new value for the maximum length.
maximumLength = stringX.length();
}//end for-loop
// +1 because array index starts from "0".
int[] counter = new int[maximumLength + 1];
for (String str : stringParts) {
// Add one to the number of words that length has
counter[str.length()] ++;
}
// We are using this kind of loop because we require the "length".
for (int i = 1; i < counter.length; i++) {
System.out.println(i + " letter words: " + counter[i]);
}//end for-loop
}//end if statement
}//end try
catch (IOException ex){
ex.printStackTrace();
}//end catch
}//end main method
}//end Main class
在此代码中,我们访问一个 .txt 文件,逐行读取该文件,并将单词放入一个数组中,该数组根据单个空格进行分割。 然后,我们计算每个单词中的字符数并将其全部显示在程序输出中。
尽管该程序生成输出,但它也突出显示我们正在 String line = dataInputStream.readLine();
行使用或覆盖已弃用的 API。 请参阅以下内容。
此警告是使用 DataInputStream 类的 readLine()
方法生成的。 根据文档,该方法自 JDK 1.1 起已被弃用,因为它不能正确地将字节转换为字符。
尽管该方法已被弃用,但在某些情况下可能会按预期执行。 但我们不能保证它能够继续履行其职责。
因此,最好使用类似但一致的方法。
从 JDK 1.1 开始,读取文本行的首选方法是 BufferedReader 类中的 readLine()
函数。 我们不必从头开始更改所有代码,只需将 DataInputStream 转换为 BufferedReader 类即可。
替换这行代码:
DataInputStream dataInputStream = new DataInputStream(in);
替换为这行代码:
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
现在,完整的工作程序如下所示。
//import libraries
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//Main class
public class Main {
//main method
public static void main(String[] args){
//path of a text file
File filePath = new File("Files/TestFile.txt");
try {
//obtain input bytes from a file
FileInputStream fileInputStream = new FileInputStream(filePath);
//adds the functionality to another input stream
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//lets an app read primitive Java data types from the specified input stream
//DataInputStream dataInputStream = new DataInputStream(bufferedInputStream);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(bufferedInputStream));
String line="";
//get a line and check if it is not null
if ((line = bufferedReader. readLine()) != null) {
// Place words to an array which are split by a "space".
String[] stringParts = line.split(" ");
// Initialize the word's maximum length.
int maximumLength = 1;
// iterate over each stingPart, the next one is addressed as "stringX"
for (String stringX : stringParts) {
// If a document contains the word longer than.
if (maximumLength < stringX.length())
// Set the new value for the maximum length.
maximumLength = stringX.length();
}//end for-loop
// +1 because array index starts from "0".
int[] counter = new int[maximumLength + 1];
for (String str : stringParts) {
// Add one to the number of words that length has
counter[str.length()] ++;
}
// We are using this kind of loop because we require the "length".
for (int i = 1; i < counter.length; i++) {
System.out.println(i + " letter words: " + counter[i]);
}//end for-loop
}//end if statement
}//end try
catch (IOException ex){
ex.printStackTrace();
}//end catch
}//end main method
}//end Main class
此外,如果您还看到类似以下内容。
Recompile with -Xlint: deprecation for details
不用担心; 它只是告诉您在编译时使用的选项,以获取有关在何处使用已弃用的内容的更多详细信息。
相关文章
如何在 Java 中延迟几秒钟的时间
发布时间:2023/12/17 浏览次数:217 分类:Java
-
本篇文章主要介绍如何在 Java 中制造程序延迟。本教程介绍了如何在 Java 中制造程序延时,并列举了一些示例代码来了解它。
如何在 Java 中把 Hashmap 转换为 JSON 对象
发布时间:2023/12/17 浏览次数:187 分类:Java
-
它描述了允许我们将哈希图转换为简单的 JSON 对象的方法。本文介绍了在 Java 中把 Hashmap 转换为 JSON 对象的方法。我们将看到关于创建一个 hashmap,然后将其转换为 JSON 对象的详细例子。
如何在 Java 中按值排序 Map
发布时间:2023/12/17 浏览次数:171 分类:Java
-
本文介绍了如何在 Java 中按值对 Map 进行排序。本教程介绍了如何在 Java 中按值对 Map
进行排序,并列出了一些示例代码来理解它。
如何在 Java 中打印 HashMap
发布时间:2023/12/17 浏览次数:192 分类:Java
-
本帖介绍了如何在 Java 中打印 HashMap。本教程介绍了如何在 Java 中打印 HashMap 元素,还列举了一些示例代码来理解这个主题。
在 Java 中更新 Hashmap 的值
发布时间:2023/12/17 浏览次数:146 分类:Java
-
本文介绍了如何在 Java 中更新 HashMap 中的一个值。本文介绍了如何在 Java 中使用 HashMap 类中包含的两个方法-put() 和 replace() 更新 HashMap 中的值。
Java 中的 hashmap 和 map 之间的区别
发布时间:2023/12/17 浏览次数:79 分类:Java
-
本文介绍了 Java 中的 hashmap 和 map 接口之间的区别。本教程介绍了 Java 中 Map 和 HashMap 之间的主要区别。在 Java 中,Map 是用于以键值对存储数据的接口,
在 Java 中获取用户主目录
发布时间:2023/12/17 浏览次数:218 分类:Java
-
这篇文章向你展示了如何在 Java 中获取用户主目录。本教程介绍了如何在 Java 中获取用户主目录,并列出了一些示例代码以指导你完成该主题。
Java 中 size 和 length 的区别
发布时间:2023/12/17 浏览次数:179 分类:Java
-
这篇文章教你如何知道 Java 中大小和长度之间的区别。本教程介绍了 Java 中大小和长度之间的区别。我们还列出了一些示例代码以帮助你理解该主题。
Java 中的互斥锁
发布时间:2023/12/17 浏览次数:111 分类:Java
-
了解有关 Java 中互斥锁的一切,在计算机科学领域,互斥或互斥被称为并发控制的属性。每台计算机都使用称为线程的最小程序指令序列。有一次,计算机在一个线程上工作。为了更好地理解,