创建并读取txt。内存中的文件

时间:2017-03-23 19:38:12

标签: android readfile createfile

嘿伙计我想创建一个txt。我的appfolder中的内部目录中的文件... 我写了这段代码......但他没有工作......我希望有人能帮助我。

private void writeFile() {
    String message = "You have did it!";
    String file_name = "test_file";

    try{
        FileOutputStream fileOutputStream = openFileOutput(file_name, MODE_PRIVATE);
        fileOutputStream.write(message.getBytes());
        fileOutputStream.close();
        Toast.makeText(getApplicationContext(), "Message saved.", Toast.LENGTH_SHORT).show();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

private void readFile() {
    try{
        String message;
        FileInputStream fileInputStream = openFileInput("test_file");
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        StringBuffer stringBuffer = new StringBuffer();
        message = bufferedReader.readLine();
        usernameField.setText(message);
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

0 个答案:

没有答案