为什么我在Java中使用close()方法时也会出现“资源泄漏:' ''错误?

问题描述 投票:0回答:1

即使我在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!");
      }
    }
  }
}

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

而且即使我紧接着放在.close()之后,它仍然显示相同的错误。

为什么呢?

java java.util.scanner resource-leak
1个回答
0
投票

有些代码路径没有关闭资源。使用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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.