Android創建檔案并輸入內容
本代碼之適合一些低版本Android
- 在AndroidManifest.xml添加權限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"</uses-permission>
- 創建檔案并輸入內容
String content = "輸入的內容";
InputStream is =new ByteArrayInputStream(content.getBytes());//string轉輸入流
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/qqpassword2.txt");
//Log.d("TAG", "file.exists():" + file.exists() + " file.getAbsolutePath():"+ file.getPath());
file.createNewFile(); //創建檔案,一般來說可以對file.exists()進行判斷,如果檔案存在就可以跳過創建
FileOutputStream fos = null;
fos = new FileOutputStream(file);
int length = 0;
length = is.available();
byte[] temp = new byte[length];
int i = 0;
while (true) {
if (!((i = is.read(temp)) > 0)) break;
fos.write(temp, 0, i);
}
fos.close();
is.close();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/275817.html
標籤:其他
