2013-10-28 54 views
1

对于作业分配,我需要创建一个可以读取和写入字节数组到/从文件的类。我已经成功创建了可以读取和写入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); 
    } 
} 

}


UPDATE

谢谢你的意见和建议如下,我现在遇到了另一个我无法解决的问题。我已经重写了我的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 
+1

您正在将'null'传递给您的构造函数。你想要读取或写入什么文件? –

+0

谢谢!这解决了编译器的错误,但正如你将看到的,我现在正遇到另一个问题。你有什么想法? –

回答

1

此代码是错误

object.readByte(null, 0, 0); 

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

1

您从main()

object.readByte(null, 0, 0); 

传递null参数readByte()而在readByte()

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

通行证有效的文件名。

NullPointerException

public class NullPointerException 
    extends RuntimeException 

当应用程序试图在需要的对象的情况下,使用空抛出该异常。其中包括:

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

谢谢你的建议!我需要将getByteFile方法放入readByte构造函数中! –

+0

不客气。 –

+0

Aniket,你有什么想法,更新的代码出了什么问题? 再次感谢! –