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();
}