java - 更新: Reading text file into a byte array

标签 java arrays file-io

对于家庭作业,我需要创建一个可以从文件读取和写入字节数组的类。我已经成功创建了可以读写 CSV 和文本的类,但是当涉及到数组时我遇到了一些困难。下面的代码是我编写的类的特色。它主要基于我的 CSV 类,即 FileInput 类 http://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileInput.java )和 FileOutput 类( http://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileOutput.java )。

当运行程序读取文本文件时,我收到以下错误消息:

    "Exception in thread "main" java.lang.NullPointerException
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at java.io.FileReader.<init>(FileReader.java:58)
at com.gc01.FileManager.FileInput.<init>(FileInput.java:22)
at com.gc01.FileManager.ByteManager.readByte(ByteManager.java:28)
at com.gc01.FileManager.ByteManager.main(ByteManager.java:85)"

我的代码:

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;


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.nextByte();
    System.out.println("How many rows are in the file?");
    final int rows = sc.nextByte();
    return fileName;
    }



public void readByte(final String fileName, int columns,  int rows){
    FileInput in = new FileInput(fileName);
    int [] [] data = new int[rows] [columns];   
    String [] line;

    for (int i = 0; i < rows; i++){
        line = in.readString().split("\t");
        for (int j = 0; j < columns; i++){

            data [i][j] = Byte.parseByte(line[j]);
        }
    }
    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();
    System.out.println("How many columns are in the file?");
    final int columnsOut = sc.nextByte();
    System.out.println("How many rows are in the file?");
    final int rowsOut = sc.nextByte();
    return fileNameOUT;
    }

public void writeByte(final String fileNameOUT,  int columnsOut, int rowsOut){
    FileOutput createData = new FileOutput (fileNameOUT);
    int newData = 0;

    System.out.println("Enter data. To finish, enter 'TERMINATE_FILE'");

    while(!"TERMINATE_FILE".equals(newData)){
        Scanner input = new Scanner (System.in);
        int [] [] data = new int[rowsOut] [columnsOut]; 
        String [] line = null;
        for (int i = 0; i < rowsOut; i++){
            createData.writeInteger(newData = input.nextByte());
            System.out.println("\t");
            for (int j = 0; j < columnsOut; i++){
                data [i][j] = Byte.parseByte(line[j]);  
            } 
        }   
        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.getByteFile();
        object.readByte(null, 0, 0);
    } else if ("2".equals(choice)){
        object.chooseFileOutput();
        object.writeByte(null, 0, 0);
    } else{
        System.out.println("Goodbye!");
        System.exit(0);
    }
}

}

<小时/>

更新

感谢您在下面的评论和建议,我现在遇到了另一个我无法解决的问题。我重写了 readByte 方法。但是,当我现在运行它时,我不再收到编译器错误(感谢您的建议),但是我无法打印文件的内容。相反,控制台仅显示“文件已读”。我研究了各种资源但找不到解决方案。我确信这是某个地方的一个简单错误。我试图读取的文件的内容也在下面。

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);
            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*****");
}
<小时/>

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

<小时/>
123     6565
123     6564
123     6563
123     6562

最佳答案

此代码是错误的根源

object.readByte(null, 0, 0);

参数null对于FileInput来说是无效状态。它应该是一个文件名字符串。

关于java - 更新: Reading text file into a byte array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19635136/

相关文章:

c - 动态数组分配,按值传递,超出范围

java - 在Java中为条件声明不同类型的变量

java - 输入正确答案后无法让代码终止

JAVA-网络日志文件

c++ - 如何使数组具有用户输入的值的大小? (C++)

java - 尝试从 .data 文件获取值并将它们存储到 int[][] 数组中

java - jar从cmd写入文件但不双击

c - Errno 无法识别所有文件

Ruby 文件 IO 定界符?

c# - 返回一个新对象与修改作为参数传入的对象