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

    相关文章

      网友评论

      • 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