Android中檔案的讀寫操作

若蘭__明月發表於2018-01-03

####一、讀取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

相關文章