【问题标题】:Java NetBeans - Exception in thread "main" java.io.FileNotFoundException: alice.txt (The system cannot find the file specified)Java NetBeans - 线程“main”java.io.FileNotFoundException中的异常:alice.txt(系统找不到指定的文件)
【发布时间】:2020-10-19 11:42:34
【问题描述】:

我正在为在命令行读取文件的 HashMap 编写代码,但我不确定是什么导致了这个错误。是我不存在这个文件还是我使用错误的代码来搜索文件?

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;


public class WordFrequencies {

    @SuppressWarnings({"unchecked"})
    public static void main(String[] args) throws Exception {
        String path = new File(".").getAbsolutePath();
        path=path.substring(0, path.length() - 1);
        path+="alice.txt";
        File file = new File(path);
        String filename = "alice.txt";
        Map<String,Integer> frequencies = new HashMap<String,Integer>();
        Scanner sc = new Scanner(new File(filename));
        while (sc.hasNext()) {
            String word = sc.next();
            if (frequencies.containsKey(word)) {
                // increment the frequency count for this word by 1
                frequencies.put(word,frequencies.get(word)+1);
            } else {
                // start the frequency count at 1
                frequencies.put(word,1);
            }
        }
               
        System.out.println(frequencies);
       
        Set<Map.Entry<String,Integer>> entries = frequencies.entrySet();
       
        
        Map.Entry<String,Integer> [] entryArray = entries.toArray(new Map.Entry[0]);
               
        Arrays.sort(entryArray, new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
       
        for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
       
        Arrays.sort(entryArray, new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Map.Entry<String,Integer> o1, Map.Entry<String,Integer> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });
        for (Map.Entry<String,Integer> entry : entryArray) System.out.println(entry);
       
    }

}

这是它抛出的异常

Exception in thread "main" java.io.FileNotFoundException: alice.txt (The system cannot find the file specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.util.Scanner.<init>(Scanner.java:611)
    at fross_charles_pa.pkg9.WordFrequencies.main(WordFrequencies.java:30)
C:\Users\lastname\AppData\Local\NetBeans\Cache\11.3\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\fross\AppData\Local\NetBeans\Cache\11.3\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 7 seconds)

感谢您的帮助

【问题讨论】:

  • 贴出抛出异常的代码。
  • "系统找不到指定的文件" => 你不确定是什么导致了这个错误?
  • 请发布整个代码。您发布的代码 sn-p 不会抛出 NPE。

标签: java file netbeans command-line


【解决方案1】:

没有这样的文件,抛出异常。 如果没有这样的文件,您可以使用 file.createNewFile() 处理这种情况。 此外,在使用 java i/o 时 - 始终使用 try/catch 来处理可能的异常:

if (!file.exists()) {
        try {
            file.createNewFile();
            // populate file, print it or do whatever you want
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

【讨论】:

  • 我假设该文件确实存在,我需要找到它。所以,这没有帮助。
  • “假设”是什么意思?你怎么知道那个文件存在?你怎么能确定?您的代码中没有任何内容可以确保您确定该文件存在(并且它不存在)。您有“找不到文件异常”并且您假设该文件存在?真的吗?没有什么帮助 - 是你抛出一般异常的风格,甚至没有尝试处理可能的错误
猜你喜欢
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 2021-02-26
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
相关资源
最近更新 更多