简体   繁体   English

如何扫描同一包内的txt?

[英]How to scan an a txt within the same package?

I am trying to use Java.util.scanner on a txt to parse it. 我正在尝试对txt使用Java.util.scanner进行解析。 I am having trouble trying to scan the file because the file isn't found. 由于找不到文件,我在尝试扫描文件时遇到了麻烦。

My class is within the same package of the txt file. 我的课程在txt文件的同一包中。 So can you please tell me how to scan a file within the same package of the class. 因此,能否请您告诉我如何在该类的同一包中扫描文件。

public class PhoneBook {
  private ArrayList<PhoneBookEntry>[] buckets;
  public void load() {
    try {
      Scanner scan = new Scanner(new File("phone.txt"));
    } catch (FileNotFoundException e) {
      System.out.println("File Not Found"); 
    } 
  } 
  public static void main(String[]args) {
    PhoneBook phone = new PhoneBook(); 
    phone.load(); 
  } 
} 

Here is the StackTrace of the error 这是错误的StackTrace

java.io.FileNotFoundException: phone.txt (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source) at java.util.Scanner.<init>(Unknown Source) at HashSet.PhoneBook.load(PhoneBook.java:13) at HashSet.PhoneBook.main(PhoneBook.java:23)

Put the file in the root of your class path (after compilation, your file should appear in the root folder where .class files are generated) and use the code syntax below: 将文件放在类路径的根中(编译后,文件应显示在生成.class文件的根文件夹中),并使用以下代码语法:

    InputStream is = getClass().getClassLoader().getResourceAsStream("phone.txt");
    Scanner fileScanner = new Scanner(is);
new File("phone.txt")

looks up the file in the current working directory. 在当前工作目录中查找文件。 This is the directory where you started the java virtual machine. 这是启动Java虚拟机的目录。

You say that it is located in the same package as the class, which is most likely a different directory (eg src/com/mypackage ). 您说它与该类位于同一包中,这很可能是一个不同的目录(例如src/com/mypackage )。

Try moving the file to the root directory of your project. 尝试将文件移动到项目的根目录。 You should then be able to load it. 然后,您应该能够加载它。

A different approach is shown in the answer from @Yogendra, which should work also when the file is located in the same package binary directory as your .class file. @Yogendra的答案中显示了一种不同的方法,当该文件与.class文件位于同一软件包二进制目录中时,该方法也应起作用。 But I am not sure if you want to load this kind of file as a resource. 但是我不确定是否要将这种文件作为资源加载。

As stated in one of the comments, you need to put the file where the .jar is generated. 如评论之一所述,您需要将文件放在生成.jar的位置。 If you're running from Netbeans/Eclipse, this will be the root folder of your project, if you're running from the compiled .jar, it will be the same directory as the .jar. 如果从Netbeans / Eclipse运行,则它将是项目的根文件夹;如果从已编译的.jar运行,则它将与.jar相同。 Packages are only folders used to organise and separate source files. 软件包只是用于组织和分隔源文件的文件夹。

But if you want to have the file in the same package anyway, you can always do something like (presuming you only have one package): 但是,如果您仍然希望将文件放在同一软件包中,则可以始终执行以下操作(假设您只有一个软件包):

new File("./src/myPackage/myFile.txt")

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

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