Android文件读写操作

本文介绍了如何在Android应用中进行文件的读写操作,包括从Assets和raw资源中读取文件,以及如何向外部存储(如SD卡)写入文件,并再次读取这些文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取Assets中的文件数据

InputStream is = getResources().getAssets()
                            .open("读取的文件名");
                    InputStreamReader isr = new InputStreamReader(is,"utf-8");
                    BufferedReader bf = new BufferedReader(isr);
                    String string="";
                    while ((string = bf.readLine()) != null) {
                        System.out.println(string);
                    }

读取raw中的文件数据

InputStream is = getResources().openRawResource(R.raw.读取的文件名);
                    InputStreamReader isr = new InputStreamReader(is,"utf-8");
                    BufferedReader bf = new BufferedReader(isr);
                    String string="";
                    while ((string = bf.readLine()) != null) {
                        System.out.println(string);
                    }

写入外部存储文件

//获取SD卡根路径
                File sdPath = Environment.getExternalStorageDirectory();
                File file = new File(sdPath, "我的文件.txt");
                //如果不存在SD卡,则不写入
                if(!sdPath.exists()){
                    Toast.makeText(MainActivity.this, "无SD卡", Toast.LENGTH_SHORT).show();
                }else{
                    try {
                        //如果文件夹不存在则创建
                        if(!file.exists()){
                            file.createNewFile();
                            FileOutputStream fos = new FileOutputStream(file);
                            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
                            osw.write("我是内容");
                            osw.flush();
                            fos.flush();
                            osw.close();
                            fos.close();
                            Toast.makeText(MainActivity.this, "已创建", Toast.LENGTH_SHORT).show();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

读取外部存储文件

//获取SD卡根路径
                File sdPath = Environment.getExternalStorageDirectory();
                File file = new File(sdPath, "我的文件.txt");
                try {
                    //如果文件存在则读取
                    if(file.exists()){
                        FileInputStream fis = new FileInputStream(file);
                        InputStreamReader isr = new InputStreamReader(fis, "utf-8");
                        char[] buf = new char[fis.available()];
                        isr.read(buf);
                        isr.close();
                        fis.close();
                        String string = new String(buf);
                        Toast.makeText(MainActivity.this, string, Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值