- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用通过谷歌找到的类来解压缩 .zip 文件。.zip 包含文件和文件夹。问题是 FileOutputStream
throws
FileNotFoundException
.. 但是文件应该从 .zip 文件中获取,所以它以前怎么可能存在?
这是我在 AsyncTask
中使用的代码:
@Override
protected Boolean doInBackground(String... params) {
try {
String zipFile = Path + FileName;
FileInputStream fin = new FileInputStream(zipFile);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
if (ze.isDirectory()) {
dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(Path
+ ze.getName()); // <-- crashes here
while ((length = zin.read(buffer)) > 0) {
fout.write(buffer, 0, length);
publishProgress(length);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
} catch (Exception e) {
mProgressDialog.dismiss();
return false;
}
return true;
}
下载 .zip 的另一个 AsyncTask
:
@Override
protected Boolean doInBackground(String... params) {
try {
URL url = new URL(params[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
File f = new File(Path+FileName);
if(!f.exists())
{
f.mkdirs();
if(!f.createNewFile())
{
f.delete();
f.createNewFile();
}
}
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(Path+FileName);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
return true;
} catch (Exception e)
{
e.printStackTrace();
e.getCause();
return false;
}
我再次收到带有(是目录)消息错误的 FileNotFoundExcpetion
!
最佳答案
如果涉及的目录不存在,
FileOutputStream
将抛出 FileNotFoundException
。我在这里没有看到任何目录创建代码,甚至没有任何代码来检查 Path
是否存在,所以这可能就是正在发生的事情。
关于java - 解压缩时 FileOutputStream 抛出 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7353871/
我查看了 FileOutputStream 的 java 文档 http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.ht
在我的 Java 应用程序中,我创建了线程,其中使用包含在 BufferedInputStream 和 BufferedOutputStream 中的 FileOutputStream 和 FileI
我希望这是一个直接的设计问题。 上下文: 我可能正在通过套接字连接下载一个到多个文件。当从套接字读取字节[]时,我正在传递它们。我还知道将这些字节写入哪个文件。我将这些字节附加到文件中 FileOut
我正在尝试编写一个简单的测试方法来将文件从 Asset 文件夹复制到 sd 卡。当我尝试打开 SD 卡上的文件时,它崩溃了。 代码 try { // POp
目标是对硬编码的 log4j Appender 进行功能测试,该 Appender 扩展了 RollingFileAppender,其中我可以使用 MyAppender 重现写入同一文件的 log4j
这里是我如何将字节写入文件。我正在使用 FileOutputStream private final Handler handler = new Handler(){
如何解决这个问题? 我收到以下错误:关闭此“FileOutputStream”。但我已经在finally block 中关闭了它 public void initHistoryForOldFile(F
我过去曾遇到过此错误,但从未完全理解它。关闭 OutputStream 后,无论 java 文件的位置或其调用方式如何,所有顺序运行或写入另一个文件的尝试都会完全搞砸,即使使用不同的写入文件的方法也是
我正在尝试创建跨 JVM 锁。为了做到这一点,我想在远程 Linux 服务器中的某个位置创建一个 java.io.FileOutputStream,如下所示: some_remote_server.m
我正在制作一个包含文件复制的应用程序,但是当我浏览一个大目录(1000+)文件并将它们复制到另一个文件夹时,它使用 290+ MB 的 RAM。 那么,有没有办法在不创建 FileOutoutStre
我在 Java 中使用 FileOutputStream 和 BufferedWriter 时遇到了问题。 如果我的磁盘空间已满并且我正在尝试写入,它将抛出IOException(这是正确的),但是当
我有一个 FileOutputStream 和一个像这样的 cicle: PrintStream output = new PrintStream(new FileOutputStream("XXXX
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我有一个FileOutputStream。我想“保存”此文件并为其指定一个保存在资源文件夹中的自己的图标。 代码如下: ObjectOutputStream oout = new Ob
这个问题已经有答案了: How to write data with FileOutputStream without losing old data? (2 个回答) 已关闭 7 年前。 您好,我正
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
目前我的代码中遇到 FileOutputStream 问题,使用 FileOutputStream 在磁盘中创建文件。创建文件后,无法从其位置打开、删除或移动文件,已经收到错误消息被其他用户锁定当网络
我正在尝试编写一个程序,将正弦波的数据保存到 .txt 中,但是当我执行该程序时,它写的内容比应有的少(有时甚至什么也不写)。这是代码: public static void main(String[
我在使用以下代码时遇到问题。我正在尝试写入 .ppm 文件,并且得到 Red.java:6: unreported exception java.io.FileNotFoundException; m
我正在从网络下载数据库,该数据库大小在 100 KB 到 500 KB 之间。这是我的代码(删除了无用的代码): URLConnection uConnection = downloadUrl.ope
我是一名优秀的程序员,十分优秀!