Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:将byteArray写入文件_Java_Android_File - Fatal编程技术网

Java Android:将byteArray写入文件

Java Android:将byteArray写入文件,java,android,file,Java,Android,File,我有byte[],我想将这个byteArray保存到一个文件中,我为此编写了以下代码: File root = new File(App.getAppCacheDir(baseActivity) + "/user/"); if (!root.exists()) root.mkdirs(); File file = new File(root, "user.jpg"); writeBytesToFile(userImage,file); 首先我创建了patha,然后我为我的文件设置了一个名称,最

我有byte[],我想将这个byteArray保存到一个文件中,我为此编写了以下代码:

File root = new File(App.getAppCacheDir(baseActivity) + "/user/");
if (!root.exists()) root.mkdirs();
File file = new File(root, "user.jpg");
writeBytesToFile(userImage,file);
首先我创建了patha,然后我为我的文件设置了一个名称,最后我为我的方法设置了pass-away path和byteArray

这是我的方法:

    private void writeBytesToFile(byte[] bFile, File fileDest) {
    try {
        FileOutputStream fOut = new FileOutputStream(fileDest);
        fOut.write(bFile);
        fOut.close();
    }catch (IOException e) {
        e.printStackTrace();
    }
}
但是没有为我的地址创建文件?我的错误在哪里?

试试这个

File root = new File(App.getAppCacheDir(baseActivity) + "/user/");
if (!root.exists()) root.mkdirs();
File file = new File(root, "user.jpg");
if (!file.exists()) file.createNewFile();
writeBytesToFile(userImage,file);

可能会引发IOException,请尝试使用调试程序调试代码了吗?如果您真的得到了文件路径,会出现什么错误?我认为如果您获得路径,那么您将面临一些异常。没有异常
,但没有为我的地址创建文件?
。你怎么知道的?你在哪里找的?你用了什么工具?请告诉文件的确切完整路径。@greenapps此文件已创建,但我无法打开emulator。我正在使用并工作,没有任何问题。我的地址是:
/storage/emulated/0/Android/data/com.safa.notify/cache/user
没有任何变化。我认为这是emulator的一个错误:我认为这是emulator的一个错误:我正在使用API 25 Android 7.1.1。(Google API)如果您在活动中,请用getApplicationContext()替换新文件(App.getAppCacheDir(baseActivity)+“/user/”).getExternalFilesDir(“用户”)我认为有一个bug。现在我正试图使用这个文件,如果我没有得到错误。这是有趣的,那么这是谷歌的新政策,这不是一个错误!!
     String s = "Java Code Geeks - Java Examples";
    File file = new File("outputfile.txt"); 
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        // Writes bytes from the specified byte array to this file output stream

        fos.write(s.getBytes());
    }
    catch (FileNotFoundException e) {
        System.out.println("File not found" + e);
    }
    catch (IOException ioe) {
        System.out.println("Exception while writing file " + ioe);
    }
    finally {
        // close the streams using close method
        try {
            if (fos != null) {
                fos.close();
            }
        }
        catch (IOException ioe) {
            System.out.println("Error while closing stream: " + ioe);
        }
    }