我是 eclipse 和 Java 的新手。我有一个硬件任务,我需要编写一个读取 .txt 文件的类。
在模拟之前,我在 C 目录中创建了一个名为“in.txt”的文件。在运行配置中,我写了“C:\in.txt”作为参数。
我收到此错误:
java.io.FileNotFoundException: C:\in.txt (系统找不到指定的文件)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream .(Unknown Source)
at java.io.FileReader.(Unknown Source)
at Flesch.main(Flesch.java:25)
我也试过写“C:\in.txt”并得到同样的错误。
这是我的代码:
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class Flesch {
public static void main(String[] args)
{
if (args.length != 1)
{
System.out.print("error");
return;
}
BufferedReader mybuffer = null;
try
{
String my_line;
int words_num=0;
int senteses=0;
int verbs=0;
int word_verbs = 0;
String word_delimtiters = " ()*&^%$#@!_-=[]{}?;:',.";
mybuffer = new BufferedReader(new FileReader(args[0]));
while((my_line = mybuffer.readLine())!= null)//reading the liens
{
//counting the number of words
StringTokenizer token = new StringTokenizer(my_line,word_delimtiters);
words_num += token.countTokens();
//counting the verbs
while (token.hasMoreElements())
{
String word = token.nextToken();
word = word.toLowerCase();
word_verbs = 0;
boolean last_char_is_verb = false;
for (int k=0;k < word.length(); k++ )
{
switch (word.charAt(k))
{
case 'a': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
case 'e': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
case 'i': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
case 'o': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
case 'u': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
case 'y': if (!last_char_is_verb) word_verbs++; last_char_is_verb = true; break;
default: last_char_is_verb = false ;
}
}
if (word_verbs == 0)
word_verbs = 1;
verbs+= word_verbs;
}
//counting the number of sentecses
for(int i=0;i<my_line.length(); i++)
{
if(my_line.charAt(i) == '.' || my_line.charAt(i) == ',' || my_line.charAt(i) == ';' || my_line.charAt(i) == '?' || my_line.charAt(i) == '!')
{
senteses++;
}
}
}
double Flesch = 206.835 - 84.6*(verbs/words_num) -1015*(words_num/senteses);
System.out.print(Flesch);
System.in.read();
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (mybuffer != null)
mybuffer.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
}
}
我在网上搜索了这个错误,但没有找到有用的东西。我感觉好像我缺少一些基本的东西