[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
File.createTempFile() ,由此联想到File.createNewFile()
方法,一时间不知道两者到底有什么区别,感觉都是创建新文件嘛,后来查看api文档介绍,并经过自己动手试验,终于有了一个较为清楚地认识。 1. File 的
createNewFile() 方法: createNewFile();返回值为 boolean;
方法介绍:当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。 使用: File file = new
File(“D:\test\1.txt”); boolean res = file.createNewFile();
if(!res)System.out.println(“创建失败!”);
如果D:/test 目录下没有 1.txt文件,则创建该文件;如果没有test目录,直接抛出异常,如果1.txt已经存在,那么文件创建失败。
可以得知,createNewFile() 方法,根据抽象路径创建一个新的空文件,当抽象路径制定的文件存在时,创建失败。2. File 的
createTempFile() 方法该方法有两种调用方式: createTempFile(String prefix, String suffix);
在默认临时文件目录中创建一个空文件,使用给定前缀和后缀生成其名称。 createTempFile(String prefix, String suffix,
File directory); 在指定目录中创建一个新的空文件,使用给定的前缀和后缀字符串生成其名称。File file2 = new
File(“D:\temp”);// D;/temp 为一个目录 File tempFile1= file2.createTempFile(“msg”,
“.tmp”,file2); File tempFile2 = file2.createTempFile(“msg”, “.tmp”);
System.out.println(tempFile2.getAbsolutePath());可以这么认为,createTempFile()
方法,在指定的目录下创建一个temp文件,directory 类型为File ,如果路径不存在,则创建失败。createTempFile(String
prefix, String suffix);方法默认的保存路径为:C:\Documents and Settings\Administrator\Local
Settings\Temp 。