File中createNewFile()和createTempFile()区别

1、createTempFile():


<span style="font-size:18px;">File sampleDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/FMRecording");
mSampleFile = File.createTempFile("FMRecording", ".m4a", sampleDir);</span>
主要的区别就是生成的这个文件的命名格式,看到createTempFile()的第一个参数,查看源码可以发现第一个参数是文件的命名的前缀;第二个参数是生成一个随机数作为文件的命名的中间,后面第三个参数是文件的后缀。


2、createNewFile():

<span style="font-size:18px;">long lTime = System.currentTimeMillis();
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(lTime);
String sFileName  = String.format("FMR_%02d%02d%04d_%02d%02d%02d",
		calendar.get(Calendar.DAY_OF_MONTH),calendar.get(Calendar.MONTH) + 1,calendar.get(Calendar.YEAR),
		calendar.get(Calendar.HOUR_OF_DAY),
		calendar.get(Calendar.MINUTE), calendar.get(Calendar.SECOND));
mSampleFile = new File(sampleDir + File.separator + sFileName+".amr");
<span style="white-space:pre">	</span>try {
		mSampleFile.createNewFile();
	} catch (Exception e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	Log.i(TAG, "lsn mSampleFile = "+mSampleFile);
	Log.i(TAG, "lsn sampleDir = "+sampleDir);</span>


这里文件的命名就是用一个存储路径的绝对路径和日期格式命名:比如:FMR_26032015_205830.amr


这是我前几天遇到的一个问题后面了解了一下的,希望大家知道更全的可以补充。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值