实际文件为0字节时,java.io.File.length()大于0
什么:实际文件为0字节时,java.io.File.length()大于0,java,android,java-io,Java,Android,Java Io,什么: /// Where is the directory that we're compressing onto the USB stick? File dir = new File(Environment.getExternalStorageDirectory(), "logs"); if (!dir.exists() || !dir.isDirectory()) throw new IOException("Invalid directory."); /// Where on the
/// Where is the directory that we're compressing onto the USB stick?
File dir = new File(Environment.getExternalStorageDirectory(), "logs");
if (!dir.exists() || !dir.isDirectory()) throw new IOException("Invalid directory.");
/// Where on the USB stick is the logs.zip going to go?
File zip = new File("/storage/FD91-1317/logs.zip");
/// Delete the file if it already exists then recreate it
if (zip.exists() && !zip.delete()) throw new IOException("Failed to create zip file.");
zip.createNewFile();
/// Using try {} to manage our streams
try (FileOutputStream fileOutputStream = new FileOutputStream(zip); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
/// Get the list of files to enumerate through
File[] files = dir.listFiles();
// Enumerate through each file to be compressed
for (File file : files) {
/// If somehow a file was deleted throw an exception
if (!file.exists()) throw new FileNotFoundException(file.getPath());
/// Create the new zip entry
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
/// Copy the file into the zip
try (FileInputStream fileInputStream = new FileInputStream(file)) {
IOUtils.copy(fileInputStream, zipOutputStream);
}
/// Close the zip entry
zipOutputStream.closeEntry();
}
}
/// Validate that the zip has been created successfully
if (zip.length() == 0) throw new IOException("Zip failed to be created!!!!");
Log.v("logs", String.format("Logs collected: %s", zip.length());
我的Android应用程序正在根Android设备(API 25+)上压缩一个目录,并将zip写入一个可移动U盘
问题:
/// Where is the directory that we're compressing onto the USB stick?
File dir = new File(Environment.getExternalStorageDirectory(), "logs");
if (!dir.exists() || !dir.isDirectory()) throw new IOException("Invalid directory.");
/// Where on the USB stick is the logs.zip going to go?
File zip = new File("/storage/FD91-1317/logs.zip");
/// Delete the file if it already exists then recreate it
if (zip.exists() && !zip.delete()) throw new IOException("Failed to create zip file.");
zip.createNewFile();
/// Using try {} to manage our streams
try (FileOutputStream fileOutputStream = new FileOutputStream(zip); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
/// Get the list of files to enumerate through
File[] files = dir.listFiles();
// Enumerate through each file to be compressed
for (File file : files) {
/// If somehow a file was deleted throw an exception
if (!file.exists()) throw new FileNotFoundException(file.getPath());
/// Create the new zip entry
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
/// Copy the file into the zip
try (FileInputStream fileInputStream = new FileInputStream(file)) {
IOUtils.copy(fileInputStream, zipOutputStream);
}
/// Close the zip entry
zipOutputStream.closeEntry();
}
}
/// Validate that the zip has been created successfully
if (zip.length() == 0) throw new IOException("Zip failed to be created!!!!");
Log.v("logs", String.format("Logs collected: %s", zip.length());
Java告诉我这个文件是97993字节,但当我从Android设备中取出U盘并将其插入mac时,mac却说是0字节
代码:
/// Where is the directory that we're compressing onto the USB stick?
File dir = new File(Environment.getExternalStorageDirectory(), "logs");
if (!dir.exists() || !dir.isDirectory()) throw new IOException("Invalid directory.");
/// Where on the USB stick is the logs.zip going to go?
File zip = new File("/storage/FD91-1317/logs.zip");
/// Delete the file if it already exists then recreate it
if (zip.exists() && !zip.delete()) throw new IOException("Failed to create zip file.");
zip.createNewFile();
/// Using try {} to manage our streams
try (FileOutputStream fileOutputStream = new FileOutputStream(zip); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
/// Get the list of files to enumerate through
File[] files = dir.listFiles();
// Enumerate through each file to be compressed
for (File file : files) {
/// If somehow a file was deleted throw an exception
if (!file.exists()) throw new FileNotFoundException(file.getPath());
/// Create the new zip entry
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
/// Copy the file into the zip
try (FileInputStream fileInputStream = new FileInputStream(file)) {
IOUtils.copy(fileInputStream, zipOutputStream);
}
/// Close the zip entry
zipOutputStream.closeEntry();
}
}
/// Validate that the zip has been created successfully
if (zip.length() == 0) throw new IOException("Zip failed to be created!!!!");
Log.v("logs", String.format("Logs collected: %s", zip.length());
更多信息:
/// Where is the directory that we're compressing onto the USB stick?
File dir = new File(Environment.getExternalStorageDirectory(), "logs");
if (!dir.exists() || !dir.isDirectory()) throw new IOException("Invalid directory.");
/// Where on the USB stick is the logs.zip going to go?
File zip = new File("/storage/FD91-1317/logs.zip");
/// Delete the file if it already exists then recreate it
if (zip.exists() && !zip.delete()) throw new IOException("Failed to create zip file.");
zip.createNewFile();
/// Using try {} to manage our streams
try (FileOutputStream fileOutputStream = new FileOutputStream(zip); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
/// Get the list of files to enumerate through
File[] files = dir.listFiles();
// Enumerate through each file to be compressed
for (File file : files) {
/// If somehow a file was deleted throw an exception
if (!file.exists()) throw new FileNotFoundException(file.getPath());
/// Create the new zip entry
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
/// Copy the file into the zip
try (FileInputStream fileInputStream = new FileInputStream(file)) {
IOUtils.copy(fileInputStream, zipOutputStream);
}
/// Close the zip entry
zipOutputStream.closeEntry();
}
}
/// Validate that the zip has been created successfully
if (zip.length() == 0) throw new IOException("Zip failed to be created!!!!");
Log.v("logs", String.format("Logs collected: %s", zip.length());
- 我的应用程序作为系统应用程序运行在
目录中/system/priv-app/
- 我的应用程序具有android.permission.WRITE\u MEDIA\u STORAGE权限
- 如果我在调用
后的5-10秒内取出U盘,插入mac时U盘上的zip文件仅为0字节。如果我等待的时间超过10秒,它将总是有字节Log.v(…)
也在记录收集的日志:97993Log.v(…)
还返回一个长度为97993的字节数组,该字节数组100%包含数据,并且不是空的李>org.apache.commons.io.FileUtils.readFileToByteArray(zip)
ls-l/storage/FD91-1317也表示
是97993字节logs.zip
/// Where is the directory that we're compressing onto the USB stick?
File dir = new File(Environment.getExternalStorageDirectory(), "logs");
if (!dir.exists() || !dir.isDirectory()) throw new IOException("Invalid directory.");
/// Where on the USB stick is the logs.zip going to go?
File zip = new File("/storage/FD91-1317/logs.zip");
/// Delete the file if it already exists then recreate it
if (zip.exists() && !zip.delete()) throw new IOException("Failed to create zip file.");
zip.createNewFile();
/// Using try {} to manage our streams
try (FileOutputStream fileOutputStream = new FileOutputStream(zip); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream)) {
/// Get the list of files to enumerate through
File[] files = dir.listFiles();
// Enumerate through each file to be compressed
for (File file : files) {
/// If somehow a file was deleted throw an exception
if (!file.exists()) throw new FileNotFoundException(file.getPath());
/// Create the new zip entry
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
/// Copy the file into the zip
try (FileInputStream fileInputStream = new FileInputStream(file)) {
IOUtils.copy(fileInputStream, zipOutputStream);
}
/// Close the zip entry
zipOutputStream.closeEntry();
}
}
/// Validate that the zip has been created successfully
if (zip.length() == 0) throw new IOException("Zip failed to be created!!!!");
Log.v("logs", String.format("Logs collected: %s", zip.length());
我已经尝试了我能想到的一切,但仍然不明白为什么Java说zip包含数据,但当插入我的mac时,它说文件是0字节。Windows也在告诉我同样的事情。我所知道的唯一事实是,如果我在从Android设备上移除USB之前等待超过10秒,那么这个问题就不会发生,但我担心,根据拉链的大小,在所有情况下,这可能都不够时间。据我所知,一旦你写一个流并关闭它,它应该是100%完成 为了解决这个问题,我必须运行
fileOutputStream.getFD().sync()代码>
如果有人想知道为什么运行that会修复它,这里有一个来自Java文档的片段来解释什么是sync()代码>可以
有关更多详细信息,请参阅
非常感谢@commonware的帮助 在资源试用的底部,调用zipOutputStream.flush()
和fileOutputStream.getFD().sync()代码>。看看这是否有帮助。“据我所知,一旦你写入一个流并关闭它,它应该100%完成”——而不是使用写缓冲文件系统。这就是getFD().sync()
发挥作用的地方。在封面下,它执行POSIXfsync()
调用,以确保所有字节都写入磁盘。@commonware我尝试了flush()
,但没有getFD().sync()
。我马上就去试试!谢谢你的建议☺️