1

对于家庭作业,我需要创建一个可以从文件读取和写入字节数组的类。我已经成功创建了可以读写 CSV 和文本的类,但是在涉及数组时我遇到了一些困难。下面的代码是我编写的类的功能。它主要基于我的 CSV 类、FileInput 类http://www.devjavasoft.org/SecondEdition/SourceCode/Share/FileInput.java)和 FileOutput 类(http://www.devjavasoft.org/SecondEdition/SourceCode/共享/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
4

2 回答 2

1

此代码是错误的来源

object.readByte(null, 0, 0);

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

于 2013-10-28T12:49:31.990 回答
1

您正在将null参数传递给readByte()frommain()

object.readByte(null, 0, 0); 

而在readByte()

FileInput in = new FileInput(fileName); //here it throws NPE  

传递有效的文件名。

空指针异常

public class NullPointerException
    extends RuntimeException

当应用程序在需要对象的情况下尝试使用 null 时引发。这些包括:

  • 调用空对象的实例方法。
  • 访问或修改空对象的字段。
  • 将 null 的长度视为一个数组。
  • 访问或修改 null 的槽,就像它是一个数组一样。
  • 把 null 当作一个 Throwable 值来抛出。
于 2013-10-28T12:49:45.743 回答