我正在尝试在 Eclipse 中将 java 文件作为命令行参数传递。但是,每一次,它都会抛出FileNotFoundException
并显示错误 - Product.java(系统找不到指定的文件)。我将此产品文件与我的主要 java 文件放在同一个包中。还要提一点,我正在使用FileReader
. BufferedReader
在我的程序中读取该文件。有什么我想念的吗?
包 com.assign6.keyword.count;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class CountKeywords {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] accessifiers = {"public", "protected", "private"};
// Put each keyword in map with key
Map<String, Integer> theKeyWordCount = new HashMap<String, Integer>();
for( String str : accessifiers ){
theKeyWordCount.put(str, 0);
// Where str is key and 0 is value of Map
}
FileReader fr;
BufferedReader br;
File file = new File(args[0]);
System.out.println(file);
// Open and read the file
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String line;
while( ( line = br.readLine() ) != null ){
if( line.length() != 0 ){
if( theKeyWordCount.containsKey(line) ){
theKeyWordCount.put(line, theKeyWordCount.get(line)+1 );
}
}
}
} catch (FileNotFoundException e) {
// File not found
e.printStackTrace();
} catch (IOException e) {
// Not able to read line
e.printStackTrace();
}
System.out.println(theKeyWordCount);
}// end main method
}