gpt4 book ai didi

java - 如何向后读取整数文件

转载 作者:行者123 更新时间:2023-11-30 10:00:45 24 4
gpt4 key购买 nike

<分区>

我的老师给我们布置了反转整数文件的作业。假设我有一个包含 1、2、3、4、5 的文件。我的作业是将其反转并将其写入文件,使其包含 5,4,3,2,1。我已经用 List 做到了。它奏效了。但是我的老师告诉我不要用List来做。有人可以帮我吗?

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();
}
}
}
}

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com