Android中文件的读写操作

作者: 菜鸟_一枚 | 来源:发表于2016-07-18 23:30 被阅读3193次

一、读取assets目录下的文件

try {
            InputStream is = getResources().getAssets().open("asset.txt");
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            String str_asset = "";
            while ((str_asset = br.readLine()) != null) {
                Log.d("MainActivity", str_asset);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

二、读取raw目录下的文件

 try {
            InputStream is = getResources().openRawResource(R.raw.raw);
            InputStreamReader isr = new InputStreamReader(is, "utf-8");
            BufferedReader br = new BufferedReader(isr);
            String str_raw = "";

            while ((str_raw = br.readLine()) != null) {
                Log.d("MainActivity", str_raw);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

三、读取手机存储文件(内置)

 try {
            FileInputStream fis = openFileInput(fileName);
            InputStreamReader isr = new InputStreamReader(fis, "utf-8");
            char input[] = new char[fis.available()];
            isr.read(input);
            isr.close();
            fis.close();
            String readed = new String(input);
            show_text.setText(readed);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

四、写入到手机存储(内置)

 try {
            FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
            osw.write(mText.getText().toString());
            osw.flush();
            fos.flush();
            osw.close();
            fos.close();
            Toast.makeText(MainActivity.this, "写入完成", Toast.LENGTH_SHORT).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

五、读取SDCARD存储文件

 File myfile = new File(sdcard, "This is my file.txt");
        if (myfile.exists()) {
            try {
                FileInputStream fis = new FileInputStream(myfile);
                InputStreamReader isr = new InputStreamReader(fis, "utf-8");
                char input[] = new char[fis.available()];
                isr.read(input);
                String str = new String(input);
                show_text_out.setText(str);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

六、写入SDCARD存储

 File myFile = new File(sdcard, "This is my file.txt");
        if (!sdcard.exists()) {
            Toast.makeText(MainActivity.this, "当前系统不具备SD卡目录", Toast.LENGTH_SHORT).show();
            return;
        }
        try {
            myFile.createNewFile();
            Toast.makeText(MainActivity.this, "文件创建完成", Toast.LENGTH_SHORT).show();
            FileOutputStream fos = new FileOutputStream(myFile);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
            osw.write(text_out.getText().toString());
            osw.flush();
            fos.flush();
            osw.close();
            fos.close();
            Toast.makeText(MainActivity.this, "文件已经写入完成", Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }

Github地址:https://github.com/wuyinlei/AndroidFileOperator

相关文章

  • 数据存储与访问之——文件存储读写

    1.Android文件的操作模式 在java中要想对文件做读写操作,只需创建 文件,读写数据即可,Android却...

  • android 读写文件

    title: android 读写文件 基本知识参照博客 Android - 文件读写操作 总结

  • Android 中的文件读写操作

    IO流(操作文件内容): 字节流 检查外部存储的状态 向外部存储写入字符串、图片 读取外部存储中的文本文件,图片文...

  • Android中文件读写操作

    Android开发中,离不开对文件的操作。本文首先介绍了使用java对文件进行基本的读写操作,而后介绍了A...

  • Android 中的文件操作

    Android 文件操作 概述 Android 中的文件操作主要涉及到两个部分,一个是内部存储的读写,一个是外部存...

  • Python 学习笔记6 2018-04-13

    文件操作: 1,文件的读写操作 2,文件的各种系统操作 3,存储对象 1,文件的读写操作 读写数据: ...

  • python文件相关操作

    一. 文件的读写操作基于字符read & write最基本的文件操作当然就是在文件中读写数据。打开一个文件的操作:...

  • Python之文件操作

    文件读写 文件读写是最基本的IO操作,在Python中内置了open函数来用于文件的读写操作,此函数创建一个文件对...

  • 2018-01-31

    java中对文件的读写操作

  • Python学习_IO文件操作

    在编程工作中,时常需要对各种文件进行操作。读写文件是最常见的IO编程,Python中内置了读写文件的函数。读写文件...

网友评论

  • Kuky_xs:楼主那个读取手机内存文件那个char input [ ] 是不是应该为char [ ] input
    Pober_Wong:@Kuky_xs Java里二者等价
  • MarcoHorse:建议加上nio和aio
  • 2eb56199844d:git是空的
    菜鸟_一枚:@fewwind 好的,应该是昨天提交的时候网络问题,今晚回去再次提交以下,谢谢提醒 :smiley:

本文标题:Android中文件的读写操作

本文链接:https://www.haomeiwen.com/subject/qrlmjttx.html