For a homework assignment, I need to create a class that that can read and write Byte arrays to/from a file. I have successfully created classes that can read and write CSV and text, however I am having some difficulty, when it comes to arrays.
When I run the 'readByte' method (see below) I do not get compiler errors , instead, I can not get the contents of the file to print. Instead the console just displays "File Read", demonstrating that it has successfully processed the nested for loop. I have studied various resources yet I can not find the solution. I am sure it is a simple mistake somewhere, any advice on how I can resolve this will be greatly appreciated.
The contents of the file I am trying to read is also below.
A second question (I thought it would be best to put both questions in one post), in my 'writeByte' method (the user enters values into a 2D array which is then printed in a .txt file), allows me to enter two values before I get the follow error message:
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)
I am sure its something to do with how the for the loop collects the user input, but I can't work out how to correct it. Ideally the file would look similar to the file being read by the 'readByte' method.
I understand this a long question, but I have run into a brick wall and all help would be greatly appreciated!
File Contents (separated by tab)
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);
}
}
}