【问题标题】:Writing/Reading Files to/from Android phone's internal memory向/从 Android 手机的内部存储器写入/读取文件
【发布时间】:2026-02-18 06:20:03
【问题描述】:

我有一个名为“MyClass”的实用程序类。该类有两种方法可以将一些数据读/写到手机的内存中。我是android新手,请按照以下代码进行操作。

public class MyClass  {
  public void ConfWrite() {
    try {
      BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new 
                           File(getFilesDir()+File.separator+"MyFile.txt")));
      bufferedWriter.write("lalit poptani");
      bufferedWriter.close();
    } catch (Exception e1) {
        e1.printStackTrace();
    }
  }
}

执行ConfWrite方法时失败

请提供更好的解决方案来解决这个问题

提前致谢

【问题讨论】:

  • “它失败了”...你这是什么意思?
  • 两个错误信息,FileNotFoundException 和 /test.txt 只读文件系统

标签: android file


【解决方案1】:

你可以Read/ Write你的文件在data/data/package_name/files文件夹中,

写作

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new 
                            File(getFilesDir()+File.separator+"MyFile.txt")));
bufferedWriter.write("lalit poptani");
bufferedWriter.close();

阅读

 BufferedReader bufferedReader = new BufferedReader(new FileReader(new 
                           File(getFilesDir()+File.separator+"MyFile.txt")));
 String read;
 StringBuilder builder = new StringBuilder("");

 while((read = bufferedReader.readLine()) != null){
        builder.append(read);
      }
 Log.d("Output", builder.toString());
 bufferedReader.close();

【讨论】:

  • 在你的阅读代码中,我可以在不使用字符串生成器的情况下阅读一行文本
  • @krish yeh,使用 bufferedReader.readLine() 它会给你整行。
  • @Soni 是否可以用读写模式打开文件,即在不使用临时文件的情况下更新值
  • @krish 我认为 java 类 BufferedWriter 会默认以写入模式打开它......你想读取文件需要使用 BufferedReader,但我不明白你为什么要打开同一个文件两种模式..因为那么你将无法获得该 2 类的读/写 java 函数,顺便说一句,你可以在两种模式下打开单个文件(我认为)
  • @LalitPoptani 我使用您的代码进行了编辑,但执行时失败,在 getFilesDir() 中显示异常 NullPointerException。我执行如下 MyClass myclass= new MyClass(); myclass.ConfWrite();
【解决方案2】:

public static void WriteFile(String strWrite) {
        String strFileName = "Agilanbu.txt"; // file name
        File myFile = new File("sdcard/Agilanbu"); // file path 
        if (!myFile.exists()) { // directory is exist or not
            myFile.mkdirs();    // if not create new
            Log.e("DataStoreSD 0 ", myFile.toString());
        } else {
            myFile = new File("sdcard/Agilanbu");
            Log.e("DataStoreSD 1 ", myFile.toString());
        }

        try {
            File Notefile = new File(myFile, strFileName); 
            FileWriter writer = new FileWriter(Notefile); // set file path & name to write
            writer.append("\n" + strWrite + "\n"); // write string
            writer.flush();
            writer.close();
            Log.e("DataStoreSD 2 ", myFile.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String readfile(File myFile, String strFileName) {
        String line = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(new File(myFile + "/" + strFileName)); // set file path & name to read
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); // create input steam reader 
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            StringBuilder stringBuilder = new StringBuilder();
            while ((line = bufferedReader.readLine()) != null) { // read line by line
                stringBuilder.append(line + System.getProperty("line.separator")); // append the readed text line by line
            }
            fileInputStream.close();
            line = stringBuilder.toString(); // finially the whole date into an single string
            bufferedReader.close();
            Log.e("DataStoreSD 3.1 ", line);
        } catch (FileNotFoundException ex) {
            Log.e("DataStoreSD 3.2 ", ex.getMessage());
        } catch (IOException ex) {
            Log.e("DataStoreSD 3.3 ", ex.getMessage());
        }
        return line;
    }

use this code to write ---  WriteFile(json); // json is a string type
use this code to read  ---  File myFile = new File("sdcard/Agilanbu");
                            String strObj = readfile(myFile, "Agilanbu.txt");

// you can put it in seperate class and just call it where ever you need.(for that only its in static)
// happie coding :)

【讨论】: