Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java NetBeans-线程中的异常;“主要”;java.io.FileNotFoundException:alice.txt(系统找不到指定的文件)_Java_File_Netbeans_Command Line - Fatal编程技术网

Java NetBeans-线程中的异常;“主要”;java.io.FileNotFoundException:alice.txt(系统找不到指定的文件)

Java NetBeans-线程中的异常;“主要”;java.io.FileNotFoundException:alice.txt(系统找不到指定的文件),java,file,netbeans,command-line,Java,File,Netbeans,Command Line,我正在为在命令行读取文件的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

我正在为在命令行读取文件的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);
       
    }

}
导入java.io.File;
导入java.util.array;
导入java.util.Comparator;
导入java.util.HashMap;
导入java.util.Map;
导入java.util.Scanner;
导入java.util.Set;
公共类词频{
@SuppressWarnings({“unchecked”})
公共静态void main(字符串[]args)引发异常{
字符串路径=新文件(“.”)。getAbsolutePath();
path=path.substring(0,path.length()-1);
path+=“alice.txt”;
文件=新文件(路径);
字符串filename=“alice.txt”;
映射频率=新HashMap();
Scanner sc=新扫描仪(新文件(文件名));
while(sc.hasNext()){
字符串字=sc.next();
if(频率。容器(字)){
//将该单词的频率计数增加1
频率.put(单词,频率.get(单词)+1);
}否则{
//在1开始频率计数
频率。put(单词,1);
}
}
系统输出打印项次(频率);
Set entries=frequencies.entrySet();
Map.Entry[]entryArray=entries.toArray(新的Map.Entry[0]);
sort(entryArray,newcomparator(){
公共整数比较(Map.Entry o1,Map.Entry o2){
返回o1.getKey().compareTo(o2.getKey());
}
});
for(Map.Entry:entryArray)System.out.println(Entry);
sort(entryArray,newcomparator(){
公共整数比较(Map.Entry o1,Map.Entry o2){
返回o1.getValue().compareTo(o2.getValue());
}
});
for(Map.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)

线程“main”java.io.FileNotFoundException:alice.txt中的异常(系统找不到指定的文件) 位于java.io.FileInputStream.open0(本机方法) 在java.io.FileInputStream.open(FileInputStream.java:195) 位于java.io.FileInputStream。(FileInputStream.java:138) 位于java.util.Scanner.(Scanner.java:611) 在fross_charles_pa.pkg9.WordFrequencies.main(WordFrequencies.java:30) C:\Users\lastname\AppData\Local\NetBeans\Cache\11.3\executor snippets\run.xml:111:执行此行时发生以下错误: C:\Users\fross\AppData\Local\NetBeans\Cache\11.3\executor snippets\run.xml:94:Java返回:1 生成失败(总时间:7秒)
感谢您的帮助

没有此类文件,将引发异常。 如果没有这样的文件,可以使用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();
        }
    }

发布引发异常的代码。“系统找不到指定的文件”=>并且您不确定是什么导致此错误?请发布整个代码。你发布的代码片段不可能抛出NPE。我假设该文件确实存在,我需要找到它。所以,这是没有帮助的。你说的“假设”是什么意思?您如何知道该文件存在?你怎么能肯定?您的代码中没有任何内容可以确保该文件存在(而且它不存在)。您有“文件未找到异常”,并且假设该文件存在?真正地什么是没有帮助的-是你的风格,抛出一般异常,甚至没有一次尝试处理可能的错误