对于家庭作业,我需要创建一个可以从文件读取和写入字节数组的类。我已经成功创建了可以读写 CSV 和文本的类,但是在涉及数组时我遇到了一些困难。
当我运行“readByte”方法(见下文)时,我没有收到编译器错误,而是无法获取要打印的文件内容。相反,控制台只显示“文件读取”,表明它已成功处理嵌套的 for 循环。我研究了各种资源,但找不到解决方案。我确信这是某个地方的一个简单错误,任何关于我如何解决这个问题的建议将不胜感激。
我试图阅读的文件的内容也在下面。
第二个问题(我认为最好将两个问题放在一篇文章中),在我的“writeByte”方法中(用户将值输入到二维数组中,然后打印在 .txt 文件中),允许我输入两个在我收到以下错误消息之前的值:
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);
}
}
}