java - Why it says the file is empty?
问题描述
I have a method that writes integer array into a file. When I read it with a method that brings those ints as array it's working. But when I try to count how many ints it says it's empty.
The same code is written on other computer systems. and it's working for him, the SAME code!
I already put the "buffer" byte array into the read method. still not working
File file = new File("/home/arad/Desktop/intFile.bin");
int[] arr = {1,2,3,4,5,6};
//exampleWriteIntegerArray(arr, file);
reverseThisFile(file);
static void reverseThisFile(File file){
File newFile = new File("/home/arad/Desktop/newIntegerFile.bin");
InputStream inputStream = null;
OutputStream outputStream = null;
int counter = 0;
System.out.println(file.length());
try {
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[4];
int actuallyRead;
while((actuallyRead = inputStream.read()) != -1){
counter++;``
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println(counter);
}
解决方案
Is something wrong in lines (input and output files are same):
inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file); //comment this line
Name your variables better (outputFile, inputFile)... Also output stream outputStream is not closed... Try first just to print on console, ...
推荐阅读
- c# - 如何在 ApplicationUser 中设置数据?
- java - 如何在非接口非抽象类中使用 JDBI @Transaction 注解?
- javascript - 从函数中的异步调用返回响应
- ios - 未找到无法识别的模块映射文件 Release-iphonesimulator/YogaKit/YogaKit.modulemap'
- python - 如何使用 python 从 json 中提取内部字符串元素?
- javascript - Javascript API 提交错误
- r - 我可以使用 R dplyr 中的“summarise_all”函数计算所有列的标准误差吗
- three.js - 从 hitTest.face.normal 获取全局法线
- html - 我的页脚元素有问题:不会停留在页面底部
- php - 如何使用 .htaccess 在代码点火器中访问带有 .php 扩展名的控制器(Branding.php),例如 http://localhost/myproject/branding.php