java - 在 Java 中创建从 .txt 文件读取和写入二维数组的程序时遇到困难

标签 java arrays file-io

对于家庭作业,我需要创建一个类,该类可以从文件读取字节数组或将字节数组写入文件。我已经成功创建了可以读写 CSV 和文本的类,但是在涉及数组时我遇到了一些困难。

当我运行“readByte”方法(见下文)时,我没有遇到编译器错误,相反,我无法获取要打印的文件内容。相反,控制台只显示“文件读取”,表明它已成功处理嵌套的 for 循环。我研究了各种资源,但找不到解决方案。我确信这是某个地方的一个简单错误,我们将不胜感激任何关于如何解决这个问题的建议。

我正在尝试读取的文件内容也在下面。

第二个问题(我认为最好将两个问题放在一个帖子中),在我的“writeByte”方法中(用户将值输入二维数组,然后打印在 .txt 文件中),允许我在收到以下错误消息之前输入两个值:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.gc01.FileManager.ByteManager.writeByte(ByteManager.java:93)
at com.gc01.FileManager.ByteManager.main(ByteManager.java:111)

我确信它与 for 循环如何收集用户输入有关,但我不知道如何更正它。理想情况下,该文件看起来类似于“readByte”方法正在读取的文件。

我知道这是一个很长的问题,但我遇到了困难,非常感谢所有帮助!


文件内容(以制表符分隔)


1     10
2     11
3     12
4     13

public class ByteManager {

public String getByteFile(){
    Scanner sc = new Scanner (System.in);
    System.out.println("Please enter the file directory of the chosen txt file?");
    System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
    ///Users/ReeceAkhtar/Desktop/FileName.txt
    final String fileName = sc.nextLine();
    System.out.println("How many columns are in the file?");
    final int columns = sc.nextInt();
    System.out.println("How many rows are in the file?");
    final int rows = sc.nextInt();
    return fileName;
    }



public void readByte(final String fileName, int rows,int columns){

BufferedReader br = null;

String[] line;
String splitBy =  "\t";
int [][] data = new int[rows] [columns];


try {
    br = new BufferedReader(new FileReader(fileName));  
        for (int i = 0; i < rows; i++){
            line = br.toString().split(splitBy);
            //data[i] [0] = Integer.parseInt(line[i]);
            for (int j = 0; j < columns; j++){
                data[i] [j] = Integer.parseInt(line[j]);
                System.out.println(data[i][j]);
            }
        }       
    } catch (FileNotFoundException e){
        e.printStackTrace();
    } catch (IOException e) {   
        e.printStackTrace();  
    } finally {  
        if (br != null) {  
        try {  
        br.close();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }
        }
    }
    System.out.println("*****File Read*****");
}


public String chooseFileOutput(){
    Scanner sc = new Scanner (System.in);
    System.out.println("Please enter the file directory for the output of the chosen file");
    System.out.println("For Example: /Users/UserName/Downloads/FileName.txt");
    ///Users/ReeceAkhtar/Desktop/GeoIPCountryWhois.csv
    final String fileNameOUT = sc.nextLine();
    return fileNameOUT;
    }

public void writeByte(final String fileNameOUT){
    Scanner input = new Scanner (System.in);
    FileOutput createData = new FileOutput (fileNameOUT);
    System.out.println("How many rows?");
    int rowsOut = input.nextInt();
    System.out.println("How many columns?");
    int columnsOut = input.nextInt();


    System.out.println("Please enter data.");

        int newDataR = 0;
        int newDataC = 0;
        int [] [] data = new int [rowsOut] [columnsOut];    


        for (int i = 0; i < rowsOut; i++){
            createData.writeInteger(newDataR = input.nextInt());
            System.out.print("\t");
            for (int j = 0; j < columnsOut; i++){
                createData.writeInteger(newDataC = input.nextInt());
                data[i] [j] = data[newDataR] [newDataC];
                System.out.print("\t");
            } 
        }   
        createData.close();

}

public static void main(String[] args) {
    Scanner in = new Scanner (System.in);
    final ByteManager object = new ByteManager ();

    System.out.println("1 for Read File, 2 for Write file");
    String choice = in.nextLine();
    if("1".equals(choice)){
        object.readByte(object.getByteFile(), 0, 0);
    } else if ("2".equals(choice)){
        object.writeByte(object.chooseFileOutput());
    } else{
        System.out.println("Goodbye!");
        System.exit(0);
    }
}
}

最佳答案

关于您的第一个问题(关于无法阅读):

main 方法中,以下行:

object.readByte(object.getByteFile(), 0, 0);

为要从文本文件中读取的行数和列数提供 0 和 0。 getByteFile() 方法确实会提示用户输入行数和列数,但它不会返回此数量,也不会对这些数字执行任何操作。您必须提示用户输入行数和列数,并将它们作为 mainreadByte 方法的第二个和第三个参数。

另外,总的来说,我想知道你为什么要在 readBytewriteByte 中创建对象 int[][] data方法。它们是 void 方法,因此您不会返回 int[][],也不会对其进行任何有意义的操作。您打算稍后使用此对象吗?

关于java - 在 Java 中创建从 .txt 文件读取和写入二维数组的程序时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19662210/

相关文章:

java - Spring DI 声明和实例化 Arraylist 的方式?

java --version 在命令行中不起作用

指向字符串和 append() 方法的指针的 C++ 数组

c - 在C中动态分配int数组

python - 将我从文件中读取的每一行保存为不同的文件

java - 增加 Spring Cloud Stream Azure 服务总线中的并发调用未定义行为

java - 在 Mac 上使用 CodeRunner IDE 运行 Java 项目

php - 如何在 smarty 模板中访问数组的键和值?

file-io - 在 Clojure 中拖尾文件?

c - 从文件中读取代码