更新:将文本文件读入字节数组

时间:2013-10-28 12:44:16

标签: 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

2 个答案:

答案 0 :(得分:1)

此代码是错误的来源

object.readByte(null, 0, 0);

参数nullFileInput的无效状态。它应该是文件名字符串。

答案 1 :(得分:1)

您正在将null参数从readByte()

传递给main()
object.readByte(null, 0, 0); 

并在readByte()

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

传递有效的文件名。

NullPointerException

public class NullPointerException
    extends RuntimeException

当应用程序在需要对象的情况下尝试使用null时抛出。其中包括:

  • 调用null对象的实例方法。
  • 访问或修改空对象的字段。
  • 将null的长度视为数组。
  • 访问或修改null的插槽,就像它是一个数组一样。
  • 抛出null,就好像它是一个Throwable值。