【问题标题】:using txt file in eclipse & java在eclipse和java中使用文本文件
【发布时间】:2013-11-06 08:22:05
【问题描述】:

我是 eclipse 和 Java 的新手。 我有一个硬件作业,我需要编写一个读取 .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();   
            }
        }

    }   


}

我在网上搜索了这个错误,但没有找到有用的东西。 我感觉好像我缺少一些基本的东西

【问题讨论】:

  • 我确定该文件不在您的 C 盘中。检查它是否在那里。
  • 试试把txt文件直接放到你的项目目录中。
  • @SriHarshaChilakapati- 文件在那里,我确定
  • 如果文件不存在。在您的代码中,将抛出 java.io.FileNotFoundException:。检查文件是否存在。
  • @peeskillet- 我该怎么做?

标签: java eclipse


【解决方案1】:

路径中的反斜杠可能不是一个好主意。尝试使用“/”。

参考:file path Windows format to java format

否则,错误可能与扩展有关。参考这个:java.io.FileNotFoundException, file not being found

【讨论】:

  • Windows 使用反斜杠。
  • 但是 Java 并不在意 ;)
  • 但这不是准确的答案。异常清楚地表明该位置没有文件。
  • +1:很可能是“资源管理器中的隐藏扩展”的情况。我猜这个文件真的被命名为“in.txt.txt”。
  • @boaz:是的,隐藏文件扩展名的“功能”对普通用户来说可能不错,但对开发人员(或高级用户)来说却很烦人。我建议你关掉它。
【解决方案2】:

有时一个斜线是不够的,所以尝试提供 "c:\ \in.txt" 我认为它会正常工作。

【讨论】:

    【解决方案3】:

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

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

    另外,你有一个潜在的错误就行了

    double Flesch = 206.835 - 84.6*(verbs/words_num) -1015*(words_num/senteses);

    如果words_numsentenses 为零,则可以除以零。您应该检查这种情况并适当处理。

    【讨论】:

      【解决方案4】:

      试试 C:\\in.txt。对于字符串文字,\ 是转义字符。如果你想在代码中使用 \,你必须使用 \\。

      【讨论】:

        【解决方案5】:

        检查是否正确传递参数。

        java Flesch C:\in.txt 应该可以工作
        java Flesch "C:\in.txt" 也可以

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

        【讨论】:

          猜你喜欢
          • 2023-01-11
          • 1970-01-01
          • 1970-01-01
          • 2020-02-03
          • 1970-01-01
          • 2015-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多