`

Android使用File创建临时文件,File.createTempFile

阅读更多

废话不多说,直接上代码

 

File files = new File("/mnt/sdcard/log.txt");
//如果这个文件不存在的话就开始创建临时文件
if (files.exists()==false) {
	try {
			   File tempFile=null;
			   tempFile = tempFile.createTempFile("users", "properties");
			   byte[] buffer = new byte[1024];
			   FileOutputStream writeFile = new FileOutputStream(tempFile);
			   InputStream inStream = getResources().getAssets().open("log.txt");
			   int length = inStream.read(buffer);
			   writeFile.write(buffer, 0, length);
			   writeFile.flush();
			   inStream.close();
			   writeFile.close();
			   files = tempFile;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

 

好了,现在files就是一个临时的File文件了,这个File文件和普通的没有区别。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics