Android中读写文件的N种方法

在android或者java中读写文件时,我们常常面临不知道该用哪种方法来实现,今天我们一起来总结一下N种读写文件的方法。

一、文件输入输出流读写文件

    public void writeFileString(File file, String str){
        if(file.canWrite()){
            try {
                FileOutputStream fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(str.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            Log.e(TAG, "No permission to write file.");
        }
    }



    public String readFileString(File file){
        String str = "";
        if(file.canRead()){
            try {
                FileInputStream fileInputStream = new FileInputStream(file);
                int length = fileInputStream.available();
                byte[] bytes = new byte[length];
                fileInputStream.read(bytes);
                str = new String(bytes);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return str;
    }

二、BufferWriter读写文件

    public void writeFileString(String file, String str){
        File file1 = new File(file);
        if(file1.canWrite()){
            try {
                BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file));
                bufferedWriter.write(str);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public String readFileString(String file){
        File file1 = new File(file);
        char[] buf = new char[512];
        StringBuilder stringBuilder = new StringBuilder();
        int length=0;
        if(file1.canWrite()){
            try {
                BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                while((length = bufferedReader.read(buf)) != -1){
                    stringBuilder.append(buf);
                }
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return stringBuilder.toString();
    }

BufferWriter和BufferReader实际上就是对FileOutputStream和FileInputStream的标准化封装

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Android Studio使用Java实现文件功能并在app界面展示,可以按照以下步骤进行: 第一步是在AndroidManifest.xml文件添加外部存储的权限: ``` <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ``` 第二步是在app的布局文件添加一个TextView用来展示文件内容: ```xml <TextView android:id="@+id/text_file_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:lineSpacingExtra="8dp" android:padding="16dp" android:textSize="16sp" /> ``` 第三步是在MainActivity文件方法,并将内容展示在TextView: ```java public class MainActivity extends AppCompatActivity { private TextView textFileContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textFileContent = findViewById(R.id.text_file_content); readFile(); } private void readFile() { String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/example.txt"; try { FileInputStream fis = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append("\n"); } textFileContent.setText(sb.toString()); br.close(); isr.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 最后,确保在app的build.gradle文件添加外部存储的权限: ``` android { ... defaultConfig { ... } compileOptions { ... } buildTypes { ... } // 添加以下代码 applicationVariants.all { variant -> variant.outputs.all { output -> def outputFile = output.outputFile output.outputFile = new File(outputFile.parent, outputFile.name.replaceAll(".apk", "-${variant.versionName}.apk")) } } } ``` 这样,当应用启动时,会自动取外部存储的example.txt文件的内容,并在TextView展示出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

静默安装9339

希望本文章对你有一点点帮助

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值