基本Java IO,总是抛出异常

InS*_*ace 1 java io

我是Java的新手,我正在尝试编写一个有一个参数的程序,即文本文件的路径.程序将找到文本文件并将其打印到屏幕上.最终,我将构建它来格式化给定的文本文件,然后将其打印到outfile,但我稍后会到达.

无论如何我的程序总是抛出和IOException,我不知道为什么.给定参数C:\ JavaUtility\input.txt,我在运行时收到"错误,无法读取文件".我的代码位于下方.

import java.io.*;

public class utility {
    public static void main(String[] path){

        try{

        FileReader fr = new FileReader(path[0]);
        BufferedReader textReader = new BufferedReader(fr);

        String aLine;

        int numberOfLines = 0;
        while ((aLine = textReader.readLine()) != null) {
            numberOfLines++;
        }

        String[] textData = new String[numberOfLines];

        for (int i=0;i < numberOfLines; i++){
            textData[i] = textReader.readLine();
        }

        System.out.println(textData);

        return;
        }
        catch(IOException e){
            System.out.println("Error, could not read file");

        }       
    }       
}
Run Code Online (Sandbox Code Playgroud)

[编辑]感谢所有人的帮助!因此,考虑到我的最终目标,我认为找到行数并存储在有限数组中仍然是有用的.所以我最后写了两个班.第一个,ReadFile.java找到了我想要的数据并处理了大部分的阅读.第二个FileData.java调用ReadFile中的方法并打印出来.我已将它们发布在下面,以后有人发现它们很有用.

package textfiles;
import java.io.*;

public class ReadFile {

        private String path;

        public ReadFile(String file_path){
            path = file_path;
        }

        int readLines() throws IOException{

            FileReader file_to_read = new FileReader(path);
            BufferedReader bf = new BufferedReader(file_to_read);

            String aLine;
            int numberOfLines = 0;

            while ((aLine = bf.readLine()) != null){
                numberOfLines++;
            }
            bf.close();
            return numberOfLines;
        }

        public String[] OpenFile() throws IOException{

            FileReader fr = new FileReader(path);
            BufferedReader textReader = new BufferedReader(fr);

            int numberOfLines = readLines();
            String[] textData = new String[numberOfLines];

            for(int i=0; i < numberOfLines; i++){
                textData[i] = textReader.readLine();
            }

            textReader.close();
            return textData;
        }
}
Run Code Online (Sandbox Code Playgroud)

package textfiles;
import java.io.IOException;

public class FileData {

    public static void main(String[] args)throws IOException{

        String file_name = args[0];

        try{
            ReadFile file = new ReadFile(file_name);
            String[] aryLines = file.OpenFile();

            for(int i=0; i < aryLines.length; i++){
                System.out.println(aryLines[i]);
            }


        }

        catch (IOException e){
            System.out.println(e.getMessage());
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

Mik*_*iar 6

你在文件的最后.当您确定文件中的行数时,您已读到文件末尾,并且EOF Flag已设置.[编辑:正如下面的@EJP注释,BufferedReader在文件末尾返回null读取.然而,你的读者不在你想要的地方这一事实仍然是正确的.]在过去,我只是通过关闭并重新打开文件来解决这个问题.或者,看看Array Lists或简单Lists.它们是动态重新调整大小的,因此您不需要提前知道文件中的行数.