1 import java.io.File; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 5 public class work { 6 7 public static void main(String[] args) { 8 //在电脑D盘下创建一个文件为HelloWorld.txt文件 9 File file=new File("D:","HelloWorld.txt"); 10 File file3=new File("E:","helloWorld.txt"); 11 try { 12 FileWriter w = new FileWriter(file); 13 w.write("hello word!"); 14 w.close(); 15 }catch (IOException e ){ 16 e.printStackTrace(); 17 } 18 //创建文件,返回一个布尔值 19 boolean isCreate; 20 try { 21 isCreate = file.createNewFile(); 22 if (isCreate) { 23 System.out.println("创建文件成功!"); 24 }else { 25 System.out.println("创建文件失败!文件已经存在"); 26 } 27 } catch (IOException e) { 28 System.out.println("创建文件失败!"); 29 } 30 31 if (file.renameTo(file3)) { 32 System.out.println("文件移动成功!"); 33 } else { 34 System.out.println("文件移动失败"); 35 } 36 System.out.println(file.exists()); 37 } 38 }