在Android中读取/写入字符串到文件中

273

我希望通过从EditText获取输入的文本将文件保存到内部存储器。然后,我希望同一个文件以字符串形式返回输入的文本,并将其保存到另一个字符串中,以便稍后使用。

这是代码:

package com.omm.easybalancerecharge;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final EditText num = (EditText) findViewById(R.id.sNum);
        Button ch = (Button) findViewById(R.id.rButton);
        TelephonyManager operator = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        String opname = operator.getNetworkOperatorName();
        TextView status = (TextView) findViewById(R.id.setStatus);
        final EditText ID = (EditText) findViewById(R.id.IQID);
        Button save = (Button) findViewById(R.id.sButton);

        final String myID = ""; //When Reading The File Back, I Need To Store It In This String For Later Use

        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //Get Text From EditText "ID" And Save It To Internal Memory
            }
        });
        if (opname.contentEquals("zain SA")) {
            status.setText("Your Network Is: " + opname);
        } else {
            status.setText("No Network");
        }
        ch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                //Read From The Saved File Here And Append It To String "myID"


                String hash = Uri.encode("#");
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse("tel:*141*" + /*Use The String With Data Retrieved Here*/ num.getText()
                        + hash));
                startActivity(intent);
            }
        });
    }

我已经包含了注释来帮助你进一步分析我的观点,以确定我想要进行操作/使用变量的位置。


2
如何读取/写入文件? - Dmitri Gudkov
你是否考虑使用应用程序的偏好设置来存储字符串? - sdabet
5
顺便提一下,请确保在清单文件中设置权限,以便与存储进行操作... - Dmitri Gudkov
1
这是我半成品的应用程序,还有很多改进需要实现。我的想法是用户在第一次运行应用程序时只需输入ID一次。然后应用程序将引用存储的ID,无论用户运行应用程序多少次。所有权限都已添加到清单中。 - Major Aly
11个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
401

希望这对您有所帮助。

写文件:

private void writeToFile(String data,Context context) {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(data);
        outputStreamWriter.close();
    }
    catch (IOException e) {
        Log.e("Exception", "File write failed: " + e.toString());
    } 
}

读取文件:

private String readFromFile(Context context) {

    String ret = "";

    try {
        InputStream inputStream = context.openFileInput("config.txt");

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String receiveString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ( (receiveString = bufferedReader.readLine()) != null ) {
                stringBuilder.append("\n").append(receiveString);
            }

            inputStream.close();
            ret = stringBuilder.toString();
        }
    }
    catch (FileNotFoundException e) {
        Log.e("login activity", "File not found: " + e.toString());
    } catch (IOException e) {
        Log.e("login activity", "Can not read file: " + e.toString());
    }

    return ret;
}

49
如果该类没有继承自Activity,则使用 "openFileInput()" 方法应按如下方式进行:context.openFileInput()。 - Behzad
14
注:以上代码可以正常使用,但生成的字符串将不包含文件中的任何换行符。如果要再次添加换行符,请将“stringBuilder.append(receiveString);”改为“stringBuilder.append(receiveString).append(”\ n“);”。如果您期望在最终字符串中包含其他换行符字符(例如Windows文本文件将具有\r等),则需要做一些适当的调整。 - treesAreEverywhere
33
这个配置文件在真实设备中保存在哪里?我找不到它来进行检查 :( - Mahdi
5
我认为,关闭流应该在 final 代码块中进行,就像 @SharkAlley 的答案中所示。 - Yurii K
7
@Kenji 文件保存在应用程序文件目录中(即/data/data/<package_name>/files/config.txt)。应用程序进程可以访问它,但不是操作系统中的所有进程。具体实现可能因设备运行的安卓版本而异。您可以在线查看AOSP的实现。例如,对于安卓8.1_r5: https://android.googlesource.com/platform/frameworks/base/+/android-cts-8.1_r5/core/java/android/app/ContextImpl.java#531 - vhamon
显示剩余9条评论

230

对于那些寻求读写字符串到文件的通用策略的人:

首先,获取文件对象

您需要存储路径。对于内部存储,请使用:

File path = context.getFilesDir();

对于外部存储 (SD 卡),请使用:

File path = context.getExternalFilesDir(null);

然后创建您的文件对象:

File file = new File(path, "my-file-name.txt");

将字符串写入文件

FileOutputStream stream = new FileOutputStream(file);
try {
    stream.write("text-to-write".getBytes());
} finally {
    stream.close();
}

或者使用Google Guava

String contents = Files.toString(file, StandardCharsets.UTF_8);

将文件读取为字符串

int length = (int) file.length();

byte[] bytes = new byte[length];

FileInputStream in = new FileInputStream(file);
try {
    in.read(bytes);
} finally {
    in.close();
}

String contents = new String(bytes);   

或者如果你正在使用Google Guava

String contents = Files.toString(file,"UTF-8");

为了完整起见,我会提到

String contents = new Scanner(file).useDelimiter("\\A").next();

这种方法不需要使用任何库,但在我的Nexus 5上的各种测试中,速度比其他选项慢了50%到400%。

对于这些策略中的每一个,您都将被要求捕获IOException异常。

Android的默认字符编码是UTF-8。

如果您正在使用外部存储,您需要在清单文件中添加以下内容之一:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
或者
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

写权限包含读权限,因此您不需要两者都拥有。


例如,我想让用户查看他的所有帖子,当他转到另一个屏幕并返回时,我需要重新绘制它吗?还是因为它已经被缓存了,所以只需从缓存中提取并显示即可?如果它只是从缓存中提取,那么我该如何添加一个if条件来告诉它不要查询我的服务器。 - Lion789
6
不要像这样使用new File(path + "/my-file-name.txt");,这与File的意义大相径庭。请改用new File(path, "my-file-name.txt"); - JimmyB
@HannoBinder Android始终运行在Linux之上,因此分隔符保证为“/”。在这种情况下使用new File(path,“my-file-name.txt”)的好处是什么?(如果有理由的话,我很乐意更新答案。) - SharkAlley
2
“File” 是有存在的意义的。在您的情况下,您可以跳过“File”,直接执行 new FileInputStream(path + "/my-file-name.txt");,但我不建议这样做。(例如,如果 path 包含尾随的 / 会怎么样呢?) - JimmyB
这个回答已经过时了,应该被移除。 - Bart Louwers

48
public static void writeStringAsFile(final String fileContents, String fileName) {
    Context context = App.instance.getApplicationContext();
    try {
        FileWriter out = new FileWriter(new File(context.getFilesDir(), fileName));
        out.write(fileContents);
        out.close();
    } catch (IOException e) {
        Logger.logError(TAG, e);
    }
}

public static String readFileAsString(String fileName) {
    Context context = App.instance.getApplicationContext();
    StringBuilder stringBuilder = new StringBuilder();
    String line;
    BufferedReader in = null;

    try {
        in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
        while ((line = in.readLine()) != null) stringBuilder.append(line);

    } catch (FileNotFoundException e) {
        Logger.logError(TAG, e);
    } catch (IOException e) {
        Logger.logError(TAG, e);
    } 

    return stringBuilder.toString();
}

9
App是什么?它应该是什么意思? - alap
@alap 是 @Eugene 静态检索应用程序上下文的工具。他需要它来获取 context.getFilesDir()。您可以将 new File(context.getFilesDir(), fileName) 的出现替换为传递给函数的 File 对象或 String,而不是 fileName - lorenzo-s
我在卸载应用程序后遇到了文件未找到异常。 - famfamfam

10

使用Kotlin的内置扩展函数来处理文件:

写入: yourFile.writeText(textFromEditText)
读取: yourFile.readText()


用户应该确保在Android平台下,如何正确地定义可读/可写,以便使用这些函数。 - Lucas Sousa

8

对读取文件中的字符串方法进行一些微小的修改,以提高性能

private String readFromFile(Context context, String fileName) {
    if (context == null) {
        return null;
    }

    String ret = "";

    try {
        InputStream inputStream = context.openFileInput(fileName);

        if ( inputStream != null ) {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);               

            int size = inputStream.available();
            char[] buffer = new char[size];

            inputStreamReader.read(buffer);

            inputStream.close();
            ret = new String(buffer);
        }
    }catch (Exception e) {
        e.printStackTrace();
    }

    return ret;
}

7

请查看下面的代码。

从文件系统中读取文件。

FileInputStream fis = null;
    try {

        fis = context.openFileInput(fileName);
        InputStreamReader isr = new InputStreamReader(fis);
        // READ STRING OF UNKNOWN LENGTH
        StringBuilder sb = new StringBuilder();
        char[] inputBuffer = new char[2048];
        int l;
        // FILL BUFFER WITH DATA
        while ((l = isr.read(inputBuffer)) != -1) {
            sb.append(inputBuffer, 0, l);
        }
        // CONVERT BYTES TO STRING
        String readString = sb.toString();
        fis.close();

    catch (Exception e) {

    } finally {
        if (fis != null) {
            fis = null;
        }
    }
以下代码是将文件写入内部文件系统的方法。
FileOutputStream fos = null;
    try {

        fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        fos.write(stringdatatobestoredinfile.getBytes());
        fos.flush();
        fos.close();

    } catch (Exception e) {

    } finally {
        if (fos != null) {
            fos = null;
        }
    }

我认为这会对您有所帮助。


5
我是一个初学者,今天在使用时遇到了一些困难。 以下是我最终得到的类。它能够正常工作,但我想知道我的解决方案有多么不完善。无论如何,我希望你们中更有经验的人能够查看我的IO类并给我一些建议。谢谢!
public class HighScore {
    File data = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator);
    File file = new File(data, "highscore.txt");
    private int highScore = 0;

    public int readHighScore() {
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            try {
                highScore = Integer.parseInt(br.readLine());
                br.close();
            } catch (NumberFormatException | IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            try {
                file.createNewFile();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
            e.printStackTrace();
        }
        return highScore;
    }

    public void writeHighScore(int highestScore) {
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));
            bw.write(String.valueOf(highestScore));
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

如果没有文件,您无需创建新文件。 - rml

4
我们需要的第一件事是在AndroidManifest.xml中获取权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在一个异步任务 Kotlin 类中,我们处理文件的创建

    import android.os.AsyncTask
    import android.os.Environment
    import android.util.Log
    import java.io.*
    class WriteFile: AsyncTask<String, Int, String>() {
        private val mFolder = "/MainFolder"
        lateinit var folder: File
        internal var writeThis = "string to cacheApp.txt"
        internal var cacheApptxt = "cacheApp.txt"
        override fun doInBackground(vararg writethis: String): String? {
            val received = writethis[0]
            if(received.isNotEmpty()){
                writeThis = received
            }
            folder = File(Environment.getExternalStorageDirectory(),"$mFolder/")
            if(!folder.exists()){
                folder.mkdir()
                val readME = File(folder, cacheApptxt)
                val file = File(readME.path)
                val out: BufferedWriter
                try {
                    out = BufferedWriter(FileWriter(file, true), 1024)
                    out.write(writeThis)
                    out.newLine()
                    out.close()
                    Log.d("Output_Success", folder.path)
                } catch (e: Exception) {
                    Log.d("Output_Exception", "$e")
                }
            }
            return folder.path

    }

        override fun onPostExecute(result: String) {
            super.onPostExecute(result)

            if(result.isNotEmpty()){
                //implement an interface or do something
                Log.d("onPostExecuteSuccess", result)
            }else{
                Log.d("onPostExecuteFailure", result)
            }
        }

    }

当然,如果您使用的是Android Api 23或更高版本,则必须处理请求以允许写入设备存储器。可以像这样进行处理:

    import android.Manifest
    import android.content.Context
    import android.content.pm.PackageManager
    import android.os.Build
    import androidx.appcompat.app.AppCompatActivity
    import androidx.core.app.ActivityCompat
    import androidx.core.content.ContextCompat

    class ReadandWrite {
        private val mREAD = 9
        private val mWRITE = 10
        private var readAndWrite: Boolean = false
        fun readAndwriteStorage(ctx: Context, atividade: AppCompatActivity): Boolean {
            if (Build.VERSION.SDK_INT < 23) {
                readAndWrite = true
            } else {
                val mRead = ContextCompat.checkSelfPermission(ctx, Manifest.permission.READ_EXTERNAL_STORAGE)
                val mWrite = ContextCompat.checkSelfPermission(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE)

                if (mRead != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(atividade, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), mREAD)
                } else {
                    readAndWrite = true
                }

                if (mWrite != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(atividade, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), mWRITE)
                } else {
                    readAndWrite = true
                }
            }
            return readAndWrite
        }
    }

然后在一个活动中执行该调用。

  var pathToFileCreated = ""
    val anRW = ReadandWrite().readAndwriteStorage(this,this)
    if(anRW){
        pathToFileCreated =  WriteFile().execute("onTaskComplete").get()
        Log.d("pathToFileCreated",pathToFileCreated)
    }

这些内部存储权限无效。 - Lerk

3

Kotlin

class FileReadWriteService {

    private var context:Context? = ContextHolder.instance.appContext

    fun writeFileOnInternalStorage(fileKey: String, sBody: String) {
        val file = File(context?.filesDir, "files")
        try {
            if (!file.exists()) {
                file.mkdir()
            }
            val fileToWrite = File(file, fileKey)
            val writer = FileWriter(fileToWrite)
            writer.append(sBody)
            writer.flush()
            writer.close()
        } catch (e: Exception) {
            Logger.e(classTag, e)
        }
    }

    fun readFileOnInternalStorage(fileKey: String): String {
        val file = File(context?.filesDir, "files")
        var ret = ""
        try {
            if (!file.exists()) {
                return ret
            }
            val fileToRead = File(file, fileKey)
            val reader = FileReader(fileToRead)
            ret = reader.readText()
            reader.close()
        } catch (e: Exception) {
            Logger.e(classTag, e)
        }
        return ret
    }
}

2
我们可以使用这段代码将字符串写入文件。
public static void writeTextToFile(final String filename, final String data) {
    File file = new File(filename);
    try {
        FileOutputStream stream = new FileOutputStream(file);
        stream.write(data.getBytes());
        stream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
然后在主代码中,我们使用这个,例如:
writeTextToFile(getExternalFilesDir("/").getAbsolutePath() + "/output.txt", "my-example-text");

之后,在Android/data/<package-name>/files目录下检查文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,