在eclipse&;中使用txt文件;JAVA

在eclipse&;中使用txt文件;JAVA,java,eclipse,Java,Eclipse,我不熟悉eclipse和Java。 我有一个H.W作业,我需要编写一个读取.txt文件的类 在模拟之前,我在我的C目录中创建了一个名为“in.txt”的文件。 在运行配置中,我编写了“C:\In.txt”作为参数 我得到这个错误: java.io.FileNotFoundException:C:\in.txt(系统找不到 指定的文件) 在java.io.FileInputStream.open(本机方法) 位于java.io.FileInputStream。(未知源) 位于java.io.Fil

我不熟悉eclipse和Java。 我有一个H.W作业,我需要编写一个读取.txt文件的类

在模拟之前,我在我的C目录中创建了一个名为“in.txt”的文件。 在运行配置中,我编写了“C:\In.txt”作为参数

我得到这个错误:

java.io.FileNotFoundException:C:\in.txt(系统找不到 指定的文件)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(未知源)
位于java.io.FileInputStream。(未知源)
位于java.io.FileReader。(未知源)
在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();   
            }
        }

    }   


}
导入java.io.FileReader;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.util.StringTokenizer;
公共级弗莱希{
公共静态void main(字符串[]args)
{
如果(args.length!=1)
{
系统输出打印(“错误”);
返回;
}       
BufferedReader mybuffer=null;
尝试
{
系上我的线;
int words_num=0;
int senteses=0;
int动词=0;
int-word_动词=0;
字符串单词_delimiters=“()*&^%$#@!-=[]{}?:”,“;
mybuffer=newbufferedreader(newfilereader(args[0]);
while((my_line=mybuffer.readLine())!=null)//读取留置权
{
//计算字数
StringTokenizer token=新的StringTokenizer(我的行,单词);
words_num+=token.countTokens();
//数动词
while(token.hasMoreElements())
{
String word=token.nextToken();
word=word.toLowerCase();
单词动词=0;
布尔值last\u char\u是动词=false;
for(int k=0;k对于(int i=0;i路径中的反斜杠可能不是一个好主意。请尝试使用“/”

参考:


否则,错误可能与扩展名有关。请参阅:

有时一个斜杠不够,请尝试提供“c:\\in.txt”我认为它可以正常工作。

我尝试了你的代码,使用正斜杠和反斜杠对我都有效。也许这与该文件位置的权限有关。请尝试将其移动到用户空间中的目录

我在eclipse中设置了programs参数,因此如果您以前是从命令行执行此操作的话,不妨尝试一下。尽管从错误消息的外观来看,文件名是从命令行读取的,但OK

另外,您的线路上还有一个潜在的bug

double-Flesch=206.835-84.6*(动词/单词数量)-1015*(单词数量/句子);


如果
words\u num
sentenses
为零,则可以被零除。您应该检查此情况并进行适当处理。

请尝试C:\\in.txt。对于字符串文字,则为转义字符。如果需要,必须在代码中使用\\if.

检查是否正确传递参数

javaflesch C:\in.txt
应该可以工作
javaflesch“C:\in.txt”

将导致
FileNotFoundException
的参数:
java-Flesch“C:\in.txt”
(注意空格)
java-Flesch“C:\in.txt”
(注意空格)

java-Flesch“C:\in.txt”
(注意空格)

Windows使用反斜杠。但是java不太在意;)但这不是准确的答案。例外情况清楚地表明该位置没有文件。+1:最有可能是“资源管理器中隐藏的扩展名”。我猜该文件的真正名称是“in.txt.txt”@boaz:是的,隐藏文件扩展名的“功能”对普通用户来说可能很好,但对开发人员(或超级用户)来说却很烦人。我建议您将其关闭。我确定该文件不在您的C驱动器中。检查是否存在。尝试将txt文件直接放入您的项目目录。@SriHarshaChilakapati-该文件在那里,我确定该文件是否不在那里。在您的代码中,将抛出java.io.FileNotFoundException:。检查该文件是否存在。@peeskillet-how d我会那样做吗?