2023年3月8日 星期三

Android Studio - Android 12 內部存儲空間讀寫檔案程式(一百零七)

Android Studio - Android 12 內部存儲空間讀寫檔案程式(一百零七):

筆者將 Android 12 內部存儲空間(Internal Storage)的讀寫檔案程式,在內部存儲空間讀寫和以往的程式差不多,所以困難度不高,可作參考作用。

Android 12 內部存儲空間讀寫程式
Android 12 內部存儲空間讀寫程式
操作系統:Windows 7 64-bit 版本
開發環境:Android Studio 4.0.1 版本
最低SDK版本:minSdkVersion 24
測試手機:Samsung Galaxy M33 5G
測試手機系統版本:Android 12(Snow Cone / 2021)
原程式:C:\Development\Development_Android\DIY-Android-190-15d Android12ReadWriteFile bugworkshop 20230304
程式:C:\Development\Development_Android\Android_Project\DIY-Android-190-15d Android12ReadWriteFile bugworkshop 20230304

MainActivity.java - Read Internal Storage File:

    public void read(){

        filecontent = "";

 

        try {

            FileInputStream fis = new FileInputStream(myFilename);

            DataInputStream in = new DataInputStream(fis);

            BufferedReader br = new BufferedReader(new InputStreamReader(in));

            String strLine;

            while ((strLine = br.readLine()) != null) {

                filecontent = filecontent + strLine;

            }

            in.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

 

        mtxt_filepath.setText(String.valueOf(myFilename));

        mtxt_filecontent.setText(filecontent);

    }


MainActivity.java - Write Internal Storage File:

   public void save(String myfilename, String fileContent, Boolean mode){

 

        myFilename = new File(getFilesDir(), filename);

        mtxt_filepath.setText(String.valueOf(myFilename));

        mtxt_filecontent.setText(fileContent);

 

        try {

            fos = new FileOutputStream(myFilename,true);

 

            if(myFilename.exists()){

                OutputStreamWriter writer = new OutputStreamWriter(fos);

                writer.append(fileContent);

                writer.close();

 

            } else {

                // File not Exist then do WRITE to the file

                fos.write(fileContent.getBytes());

            }

            // Close the stream

            fos.close();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }


MainActivity.java - Delete Internal Storage File:

    public void delete(){

 

        File file = new File(String.valueOf(myFilename));

        if(file.exists()) {

            file.delete();

        }

    }



Android 12 內部存儲空間寫入程式
Android 12 內部存儲空間讀取程式

2023年 3月 8日(Fri)天氣報告
氣溫:35.0°F / 2.0°C @ 07:00
風速:每小時 11公里
降雨機會:15%
相對濕度:百分之 85%
天氣:霧

沒有留言:

張貼留言