筆者將 Android 12 內部存儲空間(Internal Storage)的讀寫檔案程式,在內部存儲空間讀寫和以往的程式差不多,所以困難度不高,可作參考作用。
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 內部存儲空間讀取程式 |
沒有留言:
張貼留言