繁体   English   中英

为什么我收到“资源泄漏:”<unassigned Closeable value> 即使我在 Java 中使用 close() 方法也会出错?

[英]Why I get 'Resource leak: '<unassigned Closeable value>' ' error even when I use close() method in Java?

为什么即使我在 Java 中使用 close() 方法也会收到“资源泄漏:”“”错误?

我把整个代码和注释放在了关于我在哪里收到错误消息和我在哪里关闭 Scanner 方法的地方。

我使用 Eclipse,Java 13。

这是代码。

import java.util.*;
import java.io.*;
import java.lang.reflect.InvocationTargetException;

public class VehicleTest {
  public static void main(String[] args) {
      VehicleTest vtest = new VehicleTest();
    try {
      vtest.menuLoop();
    } catch(IOException e) {
      System.out.println("IO Exception!");
      System.exit(1);
    } catch(CloneNotSupportedException e) {
      System.out.println("CloneNotSupportedException");
      System.exit(1);
    }
  }

  private void menuLoop() throws IOException, CloneNotSupportedException {
    Scanner scan = new Scanner(System.in);
    ArrayList<Vehicle> arr=new ArrayList<Vehicle>();
    Vehicle vehicle;

/** Deleted the hard coded adds so it doesn't add same types of cars
 *  everytime
 */

    java.io.File file = new java.io.File("Vehicles.txt");
    Scanner in = new Scanner(file).useLocale(Locale.US);
    in.useDelimiter(",");    //** This is where I get the resource leak error message **//
    while (in.hasNext()) {
        try {
              String vehClass = in.next();                
              Class veh1 = Class.forName(vehClass);
            Vehicle veh = (Vehicle)veh1.
                    getDeclaredConstructor().newInstance();
            arr.add(veh);
            veh.readData(in);
        } catch (Exception e) { 
            System.out.print("Exception");
            System.exit(0);
            }
    }

    while(true) {
      System.out.println("1...................................New car");
      System.out.println("2...............................New bicycle");
      System.out.println("3......................Find vehicle by name");
      System.out.println("4..............Show data about all vehicles");
      System.out.println("5.......Change direction of a given vehicle");
      System.out.println("6.........................Test clone method");
      System.out.println("7..................Test driveable interface");
      System.out.println("8..............................Exit program");
      System.out.println("...............................Your choice?");
      int choice = scan.nextInt();
      String name;

      switch (choice) {
      case 1:
        arr.add(new Car());
        vehicle = arr.get(arr.size() - 1);
        try {
            vehicle.setAllFields();
          } catch(Exception e) {
            System.out.println("Wrong input!");
            System.exit(1);
          }
        break;
      case 2:
        arr.add(new Bicycle());
        vehicle = arr.get(arr.size() - 1);
        try {
            vehicle.setAllFields();
          } catch(Exception e) {
            System.out.println("Wrong input!");
            System.exit(1);
          }
        break;
      case 3:
        System.out.println("Name of vehicle: ");
        name = scan.next();
        for(Vehicle v : arr) {
            if(v.getName() != null && 
                    (v.getName().compareToIgnoreCase(name) == 0)) {
                        System.out.println(v);
            }
        }
        break;
      case 4:
        for(Vehicle v : arr) {
                System.out.println(v);
            }
        break;
      case 5:
        System.out.println("Name of vehicle: ");
        name = scan.next();
        char dir;
        int degree;
        for(Vehicle v : arr) {
            if(v.getName() != null && 
                    (v.getName().compareToIgnoreCase(name) == 0)) {
                System.out.print("\nDirection [R/L]: ");
                dir = scan.next().charAt(0);
                System.out.print("\nDegrees [0-360]: ");
                degree = scan.nextInt();
                if (dir == 'L') { v.turnLeft(degree); }
                if (dir == 'R') {v.turnRight(degree); }
            }
        }
        break;
      case 6:
          Car car1 = new Car("A bad car","red",100000,1999,"A111",350,0);
          Car car2 = (Car)car1.clone();
          car2.getBuyingDate().set(2003, 1, 2);
          car2.getProductionDate().set(2003, 5, 5);
          System.out.println("Date objects are separate, deep copy");
          System.out.printf("%n %tF", car1.getBuyingDate());
          System.out.printf("%n %tF %n", car2.getBuyingDate());
          System.out.printf("Production date car2 edited: "
                + "%n %tF %n", car2.getProductionDate());
          break;
      case 7:
          Car carTest = new Car("Test car","red",100000,1999,
                  "A111",350,0);
          Bicycle bicycleTest = 
                  new Bicycle("Test bicycle","red",100000,
                          1999,"A111",15,0);
          carTest.accelerate(3);
          carTest.accelerate(250);
          bicycleTest.accelerate(5);
          bicycleTest.accelerate(200);
          carTest.breaks(100);
          bicycleTest.breaks(50);
          carTest.stop();
          bicycleTest.stop();
          break;
      case 8:
        java.io.PrintWriter output = new java.io.PrintWriter(file);
        for(Vehicle v : arr) {
            v.writeData(output);
        }       
        scan.close();
        in.close();  //* **in.close() method is here**
        output.close();
        System.exit(0);
      default:
        System.out.println("Wrong input!");
      }
    }
  }
}

即使我将 in.close() 放入 try 块中,它仍然显示相同的错误。

即使我在之后放入 in.close() ,它仍然显示相同的错误。

为什么会这样?

存在资源未关闭的代码路径。 要么使用 finally 块,要么使用try-with-resources 喜欢,

try (Scanner in = new Scanner(file).useLocale(Locale.US)) {
    in.useDelimiter(",");
    while (in.hasNext()) {
        try {
            String vehClass = in.next();                
            Class veh1 = Class.forName(vehClass);
            Vehicle veh = (Vehicle)veh1.
                    getDeclaredConstructor().newInstance();
            arr.add(veh);
            veh.readData(in);
        } catch (Exception e) { 
            System.out.print("Exception");
            System.exit(0);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM