Java文件输入问题

时间:2014-03-15 16:12:19

标签: java netbeans file-io

我在让程序从文件中读取时遇到问题。我正在使用Netbeans 7.4,我已将文本文件包含在.java文件所在的源文件夹中。程序应该打开一个文本文件并返回行数。我认为问题与我处理文件的方式有关,因为对于这个程序,我使用一个类进行wordcounting,另一个类用于执行文件处理,另一个类用于测试我的类。

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class WordCountTest {
        public static void main(String[] args)
        {
            // Gets the file name from the user and creates a new FileHandle
            Scanner console = new Scanner(System.in);
            System.out.print("Input File: ");
            String inputFileName = console.next();
            FileHandle inputFileHandle = new FileHandle(inputFileName);

            try{
                // get the File object from the FileHandle
                File inputFile = inputFileHandle.getFile();
                // Create a new WordCount object with the input file
                WordCount fileCount = new WordCount(inputFile);
                // Print out the line numbers
                System.out.println(fileCount.getLineNumber());
            }
            catch(FileNotFoundException e) {
                System.out.println("Error");
            }
        }
    }

FileHandle类:

    import java.io.File;
    import java.io.FileNotFoundException;

    public class FileHandle {
        // Instance Variables
        private File inputFile;
        private String inputFileName;

        /**
         * Constructor for the FileHandle Class.
         * @param file the String representation of the file name
         */
        public FileHandle(String file)
        {
            this.inputFileName = file;
        }

        /**
         * Method for creating a file object based on the String name passed
         * @return File object
         * @throws FileNotFoundException 
         */
        public File getFile() throws FileNotFoundException
        {
            File inputFile = new File(this.inputFileName);
            return inputFile;
        }
    }

字数统计类:

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;

    public class WordCount {
        // Instance variabkes
        private File inputFile;
        private int lineNumber=0;

        /**
         * Constructor for the WordCount class
         * @param inputFile File object
         */
        public WordCount(File inputFile)
        {
            this.inputFile = inputFile;
        }

        /**
         * Method that counts the line numbers for a file
         * @return the number of line numbers
         * @throws FileNotFoundException 
         */
        public int getLineNumber() throws FileNotFoundException
        {
            // New Scanner to read from the file
            Scanner in = new Scanner(inputFile);

            // Increment the lineNumber counter if theres a next line
            while(in.hasNextLine())
            {
                lineNumber++;
            }

            return lineNumber;
        }
    }

这就是我的输出:

    run:
    Input File: mary.txt
    Error
    BUILD SUCCESSFUL (total time: 13 seconds)

2 个答案:

答案 0 :(得分:0)

如果文件位于项目的工作目录中(或者您运行ant目标的位置),则此方法有效。这是因为您提供了相对文件路径。给绝对文件路径应该有效。如果您尝试从类路径中读取文件,则应使用ClassLoader的getInputStream方法。

答案 1 :(得分:0)

而不是打印出来"错误"在拦截区。为什么不暂时使用e.printStackTrace()。它应该指向引起问题的功能。