我怎样写一个字节数组到Android的文件? [英] How do I write a byte array to a file in Android?

查看:142
本文介绍了我怎样写一个字节数组到Android的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code创建一个文件。

This is my code to create a file.

public void writeToFile(byte[] array) 
{ 
    try 
    { 
        String path = "/data/data/lalallalaa.txt"; 
        FileOutputStream stream = new FileOutputStream(path); 
        stream.write(array); 
    } catch (FileNotFoundException e1) 
    { 
        e1.printStackTrace(); 
    } 
} 

当我试图通过只调用的路径,我的文件发送到我的服务器字符串路径=/data/data/lalallalaa.txt;

When I try to send my file to my server by just calling the path String path = "/data/data/lalallalaa.txt";

我得到这个logcat的错误消息:

I get this logcat error message:

03-26 18:59:37.205: W/System.err(325): java.io.FileNotFoundException: /data/data/lalallalaa.txt

我不明白为什么它不能找到那就是所谓的已创建的文件。

I don't understand why it can't find a file that is "supposedly" created already.

推荐答案

您确定该文件已创建?

可以添加以下内容:

File file = new File(path);
if (!file.exists()) {
  file.createNewFile();
}

这篇关于我怎样写一个字节数组到Android的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆