2013-10-28 56 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,你有什麼想法,更新的代碼出了什麼問題? 再次感謝! –