Android保存和读取文件

时间:2016-03-22 09:22:54

标签: android file

我试图写,然后读取文件:

private void writeToFile(String data,String filename) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(this.openFileOutput(filename, Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    } catch (IOException e) {
        //Log.e("Exception", "File write failed: " + e.toString());
        serviceNotifer("FILE WRITE", "ERROR", 687);
    }
}
 writeToFile("SOMETEXT!", "input.bin");

/* try {
    TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
    e.printStackTrace();
}*/

File file = new File("input.bin");
FileInputStream fis = null;

try {
    serviceNotifer("WriteFile", "Stop2", 922);
    fis = new FileInputStream(file);

    int bytesRead=0;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] b = new byte[1024];
    while ((bytesRead = fis.read(b)) != -1) {
        bos.write(b, 0, bytesRead);
    }
    byte[] bytes = bos.toByteArray();
    char[] hexArray = "0123456789ABCDEF".toCharArray();
    char[] hexChars = new char[bytes.length * 2];

    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }

    UpdateFileString=fis.toString();
    serviceNotifer("FILE C:", UpdateFileString, 922);
    int ByteNum=UpdateFileString.length();
    SentNewIndexVal(0xE001,ByteNum);
} catch (FileNotFoundException e) {
    System.out.println("File not found: " + e.toString());
    serviceNotifer("FILE", "ERROR1"+ e.toString(), 687);
} catch (IOException e) {
    System.out.println("Exception reading file: " + e.toString());
    serviceNotifer("FILE", "ERROR2", 687);
} finally {
    try {
        if (fis != null) fis.close();
    } catch (IOException ignored) {
    }
}

录制后,应用程序大于15千字节。但是我得到了oshmbku&#34; ERROR1&#34; - 文件未找到。我的错误在哪里?我认为它与文件路径有关...

0 个答案:

没有答案