方法一:new File (String pathname)
public void create01(){
String filePath = "e:\\news1.txt";
File file = new File(filePath);
try{
file.createNewFile();
System.out.println("文件创建成功");
}catch(IOException ex){
ex.printStackTrace();
}
}
方法二:new File(File parent,String child)//根据父目录文件+子路径构建
public void create02(){
File parentFile = new File("e:\\");
String fileName = "news2.txt";
File file = new File(parentFile , fileName);
}
try{
file.createNewFile();
System.out.println("文件创建成功");
}catch(IOException ex){
ex.printStackTrace();
}
方法三: new File (String parent , String child) //根据父目录+子路径构建
String parentPath = "e:\\";
String fileName = "news3.txt";
File file = new File(parentPath , fileName);
try{
file.createNewFile();
System.out.println("文件创建成功");
}catch(IOException ex){
ex.printStackTrace();
}
PS:File file = new file(String fileName) ------->这里创建的文件只是一个Java程序中的一个对象,只有执行了file.createNewFile()方法后,才会真正地在磁盘创建该文件(内存->硬盘)