java 已弃用的 readLine() 要更改什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13445451/
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
deprecated readLine() What to change?
提问by NestedCodeblocksFTW
I'm a Java beginner, trying to get this to work in Eclipse. However, the readLine
is struck through and a notice says it is deprecated. The code works, albeit not the while ((var2 = var5.readLine()) != null) {
bit ... So I'm wondering how to fix it.
我是一个 Java 初学者,试图让它在 Eclipse 中工作。但是,它readLine
被划掉了,并且通知说它已被弃用。代码有效,尽管不是一while ((var2 = var5.readLine()) != null) {
点点......所以我想知道如何修复它。
final class ScreenShotHelper implements Runnable
{
public void run()
{
try
{
String var1 = ScreenShotHelper.accesstry (InputStream is = connection.getInputStream()) {
BufferedReader lines = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while (true) {
String line = lines.readLine();
if (line == null)
break;
System.out.println("Server Response " + line);
...
}
}
0().getAbsolutePath();
String var2 = "";
HttpURLConnection var3 = null;
DataOutputStream var4 = null;
DataInputStream var5 = null;
String var6 = "\r\n";
String var7 = "--";
String var8 = "*****";
String var9 = "";
int var10 = 1048576;
String var11 = "";
var9 = Minecraft.getMinecraft().thePlayer.username;
String var12 = "http://localhost/screenupload/index.php?playername=" + var9;
try
{
FileInputStream var13 = new FileInputStream(new File(var1));
URL var14 = new URL(var12);
var3 = (HttpURLConnection)var14.openConnection();
var3.setDoInput(true);
var3.setDoOutput(true);
var3.setUseCaches(false);
var3.setRequestMethod("POST");
var3.setRequestProperty("Connection", "Keep-Alive");
var3.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + var8);
var4 = new DataOutputStream(var3.getOutputStream());
var4.writeBytes(var7 + var8 + var6);
var4.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + var1 + "\"" + var6);
var4.writeBytes(var6);
int var15 = var13.available();
int var16 = Math.min(var15, var10);
byte[] var17 = new byte[var16];
for (int var18 = var13.read(var17, 0, var16); var18 > 0; var18 = var13.read(var17, 0, var16))
{
var4.write(var17, 0, var16);
var15 = var13.available();
var16 = Math.min(var15, var10);
}
var4.writeBytes(var6);
var4.writeBytes(var7 + var8 + var7 + var6);
System.out.println("File is written");
var13.close();
var4.flush();
var4.close();
}
catch (MalformedURLException var20)
{
System.out.println("error1: " + var20.getMessage());
}
catch (IOException var21)
{
System.out.println("error2: " + var21.getMessage());
}
try
{
var5 = new DataInputStream(var3.getInputStream());
while ((var2 = var5.readLine()) != null) {
System.out.println("Server Response " + var2);
ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7aSuccessfully uploaded screenshot! Direct link:");
ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7a" + var2);
}
var5.close();
}
catch (IOException var19)
{
System.out.println("error3: " + var19.getMessage());
}
}
catch (Exception var22)
{
var22.printStackTrace();
ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a74failed to save");
}
}
}
回答by erickson
- Give your variables meaningful names.
- Declare your variables in the smallest possible scope.
- Don't assign dummy values (like
null
or""
) to variables. - Avoid side-effects in tests.
- Use automatic resource management to cleanly ensure that streams are closed.
- Determine and use the correct character encoding when converting bytes to characters.
- 给你的变量起有意义的名字。
- 在尽可能小的范围内声明变量。
- 不要将虚拟值(如
null
或""
)分配给变量。 - 避免测试中的副作用。
- 使用自动资源管理干净地确保流被关闭。
- 将字节转换为字符时,确定并使用正确的字符编码。
Here is an example of your code that applies these points:
以下是应用这些要点的代码示例:
BufferedReader var5 = null;
try
{
var5 = new BufferedReader(new InputStreamReader(var3.getInputStream()));
// or perhaps
// new BufferedReader(new InputStreamReader(var3.getInputStream(), "UTF-8"));
while ((var2 = var5.readLine()) != null) {
System.out.println("Server Response " + var2);
ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7aSuccessfully uploaded screenshot! Direct link:");
ScreenShotHelper.mc.thePlayer.addChatMessage("\u00a7a" + var2);
}
} finally {
if (var5 != null) {
try { var5.close(); }
catch (IOException ignored) {}
}
}
回答by Ted Hopp
Use this code instead:
请改用此代码:
##代码##The reason for this (and the suggestion) can be found in the docs for DataInputStream.readLine()
:
原因(和建议)可以在以下文档中找到DataInputStream.readLine()
:
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.
此方法不能正确地将字节转换为字符。从 JDK 1.1 开始,读取文本行的首选方法是通过
BufferedReader.readLine()
方法。
(If you're using Java 7, you can dispense with the finally
clause by using a try-with-resourcesconstruct.)
(如果您使用的是 Java 7,则可以finally
使用try-with-resources构造来省略该子句。)