Guava 函数 toByteArray() 返回空值?

Guava function toByteArray() returning null?

这是我的代码片段:

File file = new File("encryptedImageFileName");
byte[] encryptedBytes = null;
try
{
    encryptedBytes = Files.toByteArray(file);
}
catch (Exception e)
{}

这是我使用调试器后 encryptedBytesfile 两个变量的图像,

http://i.stack.imgur.com/5nJgs.png

我几乎可以肯定 file 下的所有内容意味着它实际上找到了该文件,但正如您所看到的 encryptedBytes 为空,所以 Files.toByteArray(file) 没有用...为什么会这样?

***************************************编辑************ **********************

这是我尝试编写字节数组的代码,它有什么问题吗?

FileOutputStream outputStream;
try
{
    outputStream = openFileOutput("encryptedImageFileName", Context.MODE_PRIVATE);
    outputStream.write(encodedByteArrayImage);
    outputStream.close();
}
catch (Exception e)
{
    e.printStackTrace();
}

这抛出

java.io.FileNotFoundException: encryptedImageFileName: open failed: ENOENT (No such file or directory) 

调试器说 encodedByteArrayImage 有一个(非空)值。

没有。您可以创建一个 new File,它实际上并不存在于文件系统中,并且无法正常打开,然后当您尝试从中读取时,您会得到一个 IOException,看起来像这里发生了什么。

尝试使用 e.printStackTrace() 找出抛出的异常及其原因。

我解决了,

这里是要写的代码:

try
    {
        File file = new File(this.getFilesDir().getPath() + "encryptedImageFileName.txt");
        ByteSink sink = Files.asByteSink(file);
        sink.write(encryptedBytes);
    }
catch (Exception e)
    {
        e.printStackTrace();
    }

这是要阅读的代码:

encryptedBytes = null;
try
    {
        File file = new File(this.getFilesDir().getPath() + "encryptedImageFileName.txt");
        ByteSource source = Files.asByteSource(file);
        encryptedBytes= source.read();
    } 
catch (Exception e)
    {
        e.printStackTrace();
    }

在使用 ByteSink 而不是我之前所做的写入之后,我得到了一个只读错误,因为我试图写入根目录而不是应用程序目录,我错过了这个:

this.getFilesDir().getPath() + 

在文件名前(还在文件名末尾添加了.txt)。