已弃用的 readLine() 要更改的内容



我是一个Java初学者,试图让它在Eclipse中工作。 但是,readLine已被删除,并且通知说它已被弃用。 代码有效,尽管不是while ((var2 = var5.readLine()) != null) {位...所以我想知道如何解决它。

final class ScreenShotHelper$1 implements Runnable
{
    public void run()
    {
        try
        {
            String var1 = ScreenShotHelper.access$000().getAbsolutePath();
            String var2 = "";
            HttpURLConnection var3 = null;
            DataOutputStream var4 = null;
            DataInputStream var5 = null;
            String var6 = "rn";
            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");
        }
    }
}
  • 为变量指定有意义的名称。
  • 在尽可能小的范围内声明变量。
  • 不要将虚拟值(如 null"" )分配给变量。
  • 避免测试中的副作用。
  • 使用自动资源管理来干净地确保流已关闭。
  • 在将字节转换为字符时确定并使用正确的字符编码。

下面是应用这些要点的代码示例:

try (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);
    ...
  }
}

请改用以下代码:

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) {}
    }
}

这样做的原因(和建议)可以在DataInputStream.readLine()文档中找到:

此方法不能正确地将字节转换为字符。从 JDK 1.1 开始,读取文本行的首选方法是通过 BufferedReader.readLine() 方法。

(如果您使用的是 Java 7,则可以通过使用 try-with-resources 构造来省去 finally 子句。

相关内容

  • 没有找到相关文章

最新更新