首页 > 解决方案 > 在 Java 中使用 Scanner 时如何检查文件是否存在

问题描述

所以,如果你继续我的createdChoice方法,你会看到我试图弄清楚如何User在文件中拥有一个类型并查看它是否存在。

这就是我到目前为止所拥有的。

import java.io.File;
import java.io.FileNotFoundException; 
import java.util.Scanner;

public class MadLibs {

  public static void main(String [] args) throws FileNotFoundException {
    Scanner console = new Scanner(System.in);
    File madLibFile = new File("/Users/adanvivero/IdeaProjects/Assignment 4/src/mymadlib.txt");
    Scanner madLib = new Scanner(System.in);

    String choice = menu(console);
    if(choice.equals("c")) {
      createdChoice(madLib);
    }
  }

  public static void createdChoice(Scanner madLib) throws FileNotFoundException {
    File madLibFile = new File("/Users/adanvivero/IdeaProjects/Assignment 4/src/mymadlib.txt");
    Scanner input = new Scanner(System.in);
    Scanner fileName = new Scanner(madLibFile);
    System.out.print("Input file name: ");
    String fileAccess = input.next();
    //fileName = madLibFile;
    if(fileAccess.equals(fileName)) {
        System.out.print("Well this file exists, but I don't know how to make it into a scanner");
    }
  }
}

我希望这是有道理的。提供一些反馈

标签: javafileinput

解决方案


要检查提供的路径名表示的文件或目录是否存在,您可以使用exist()

public static void main(String[] args) {
  final File file  = new File("temp/foo.txt");
  if (file.exists()) {
    System.out.printf("File " + file.getName() + " exist !");
  }
}

不让我们看看我们的Scanner

public Scanner(File source) throws FileNotFoundException {}

如果您将文件传递给不存在的扫描仪 - 您将收到异常。

因此,为了确保在这种情况下文件存在,您可以:

public static void main(String[] args) {
  // let's say there is no such file
  final File file  = new File("temp/foo.txt");
  try {
    Scanner scanner = new Scanner(file); // this will fail if file does not exist
    // otherwise it exist and you can do what you want
  } catch (FileNotFoundException e) {
    // and here you can handle the case when file not exist
  }
}

如果您的意思是如何检查您的扫描仪是否可以获取某些内容,那么扫描仪有很多hasNext...方法。比如hasNext(), hasNextByte(),hasNextLine()等。

在你这样做之前,scanner.next()你应该检查!例如:

if (scanner.hasNext()) {
  String fileAccess = scanner.next();  
}

您的代码中的这些行是错误的,因为您尝试检查 2 种不同类型的 String 和 Scanner 是否是equals(). 他们从来都不是。

// fileAccess is String and fileName is Scanner according to your code
if(fileAccess.equals(fileName)) {
  System.out.print("Well this file exists, but I don't know how to make it into a scanner");
}

我将如何重写您的代码(但不确定我是否理解它应该做什么),因此仅作为示例:

public static void createdChoice() throws FileNotFoundException {
  File madLibFile = new File("/Users/adanvivero/IdeaProjects/Assignment 4/src/mymadlib.txt");
  if (madLibFile.exist()) {
    Scanner inScanner = new Scanner(System.in);
    Scanner fileScanner = new Scanner(madLibFile);
    System.out.println("Input file name: " + madLibFile.getName());
    if (inScanner.hasNext()) {
      String fileAccess = inScanner.next();
      // do whatever you need e.g
      if(fileAccess.equals(madLibFile.getName())) {
        // something here
      }
    } else {
      System.out.println("No more tokens to read !");  
    }
  } else {
    System.out.println("File mymadlib.txt does not exist !");
  }
}

希望这会有所帮助。

快乐的黑客:)


推荐阅读