宿題のために、ファイルとの間でバイト配列を読み書きできるクラスを作成する必要があります。CSV とテキストを読み書きできるクラスを正常に作成できましたが、配列に関しては少し問題があります。
「readByte」メソッド (以下を参照) を実行すると、コンパイル エラーが発生せず、ファイルの内容を印刷することができません。代わりに、コンソールには「File Read」と表示されるだけで、ネストされた for ループが正常に処理されたことを示します。さまざまなリソースを調べましたが、解決策が見つかりません。どこかで単純な間違いだと確信しています。これを解決する方法についてのアドバイスは大歓迎です。
読み込もうとしているファイルの内容も以下です。
2 番目の質問 (両方の質問を 1 つの投稿に入れるのが最善だと思いました) は、私の 'writeByte' メソッド (ユーザーが 2D 配列に値を入力し、それが .txt ファイルに出力されます) で、2 つ入力することができます次のエラー メッセージが表示される前の値:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.gc01.FileManager.ByteManager.writeByte(ByteManager.java:93)
at com.gc01.FileManager.ByteManager.main(ByteManager.java:111)
for ループがユーザー入力を収集する方法に関係していると確信していますが、それを修正する方法がわかりません。理想的には、ファイルは「readByte」メソッドによって読み取られるファイルに似ているはずです。
私はこれが長い質問であることを理解していますが、私はレンガの壁に遭遇しました。
ファイルの内容 (タブ区切り)
1 10
2 11
3 12
4 13
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.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);
//data[i] [0] = Integer.parseInt(line[i]);
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*****");
}
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();
return fileNameOUT;
}
public void writeByte(final String fileNameOUT){
Scanner input = new Scanner (System.in);
FileOutput createData = new FileOutput (fileNameOUT);
System.out.println("How many rows?");
int rowsOut = input.nextInt();
System.out.println("How many columns?");
int columnsOut = input.nextInt();
System.out.println("Please enter data.");
int newDataR = 0;
int newDataC = 0;
int [] [] data = new int [rowsOut] [columnsOut];
for (int i = 0; i < rowsOut; i++){
createData.writeInteger(newDataR = input.nextInt());
System.out.print("\t");
for (int j = 0; j < columnsOut; i++){
createData.writeInteger(newDataC = input.nextInt());
data[i] [j] = data[newDataR] [newDataC];
System.out.print("\t");
}
}
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.readByte(object.getByteFile(), 0, 0);
} else if ("2".equals(choice)){
object.writeByte(object.chooseFileOutput());
} else{
System.out.println("Goodbye!");
System.exit(0);
}
}
}