My teacher gave us homework to reverse a integer file. lets say I have a file that contains 1,2,3,4,5. My homework is to reverse it and write it into a file so it contains 5,4,3,2,1. I already did that with List. and it worked. But my teacher told me not to do it with List. Can someone help me please ?
static void getNumFiles(File file){
InputStream inputStream = null;
OutputStream outputStream = null;
List<Integer> list = new ArrayList<>();
int actuallyRead = 0;
byte[] buffer = new byte[4];
int[] arr = new int[list.size()];
int counter = list.size();
int x = 0;
try {
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file);
while((actuallyRead = inputStream.read(buffer)) != -1){
x = ByteBuffer.wrap(buffer).getInt();
list.add(x);
}
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(list.size() - i) ;
}
exampleWriteIntegerArray(arr, file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}