首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使程序在编译时读取任意文件

如何使程序在编译时读取任意文件
EN

Stack Overflow用户
提问于 2014-03-12 11:05:20
回答 2查看 296关注 0票数 0

当我在终端中运行程序时,我需要程序读取任何文本文件,所以如果它看起来像这样:

01.txt中的java TextAnalysis,当我按回车键时。如果我有一个特定的文本文件被扫描仪读取,我知道一切都能正确编译。但是当我尝试用args替换该文件时,它停止了编译,并给出了一个错误:

代码语言:javascript
复制
Exception in thread "main" java.io.FileNotFoundException: in01.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:120)
    at java.util.Scanner.<init>(Scanner.java:636)
    at TextAnalysis.main(TextAnalysis.java:17)

来源:

代码语言:javascript
复制
import java.util.*;
import java.io.*;

public class TextAnalysis {

    public static void main (String [] args) throws IOException {
        String fileName = args[0];
        File file = new File(fileName);
        Scanner fileScanner = new Scanner(file);
        int MAX_WORDS = 10000;
        String[] words = new String[MAX_WORDS];
        int unique = 0;

        System.out.println("TEXT FILE STATISTICS");
        System.out.println("--------------------");
        System.out.println("Length of the longest word: " + longestWord(fileScanner));
        read(words, fileName);
        System.out.println("Number of words in file wordlist: " + wordList(words));
        System.out.println("Number of words in file: " + countWords(fileName));
        System.out.println("Word-frequency statistics");

    }


    public static String longestWord (Scanner s) {
        String longest = "";
        while (s.hasNext()) {
            String word = s.next();
            if (word.length() > longest.length()) {
                longest = word;
            }
        }
        return (longest.length() + " " + "(\"" + longest + "\")");
    }

    public static int countWords (String fileName) throws IOException {
        File file = new File(fileName);
        Scanner fileScanner = new Scanner(file); 
        int count = 0;

            while(fileScanner.hasNext()) {
                String word = fileScanner.next();
                    count++;
            }
        return count;
    }


    public static void read(String[] words, String fileName) throws IOException{
        File file = new File(fileName);
        Scanner s = new Scanner(file);
        while (s.hasNext()) {
        String word = s.next();
        int i;
        for ( i=0; i < words.length && words[i] != null; i++ ) {
            words[i]=words[i].toLowerCase();
            if (words[i].equals(word)) {
                break;
            }
        }
        words[i] = word;
    }
    }

    public static int wordList(String[] words) {
        int count = 0;
        while (words[count] != null) {
            count++;
        }
     return count; 
    }

}
EN

回答 2

Stack Overflow用户

发布于 2014-03-12 11:08:35

您的文件名中有一个空格,这使得java将您的参数解释为数组{TextAnalysis,in01}。解决此问题的简单方法是将文件名中的空格设置为下划线。

票数 0
EN

Stack Overflow用户

发布于 2014-03-12 11:14:46

由于您在01.txt中的文件不存在,因此当您尝试使用此文件中的扫描仪时,抛出异常。

确保在TextAnalysis.class所在的目录中有一个名为in01.txt的文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22341194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档