new File()
- package com.hspedu;
-
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
-
- /**
- * @author: guorui fu
- * @versiion: 1.0
- */
- public class Homework01 {
- public static void main(String[] args) throws IOException {
- /*
- 在判断e盘下是否有件夹mytemp,如果没有就创建
- 在上面创建的文件夹下创建文件hello.txt
- 如果文件已存在,提示已存在,就不用重复创建了
- */
-
- File file = new File("e:\\mytemp");
- if (file.exists()) {
- System.out.println("文件已存在");
- } else {
- file.mkdir();
- }
- String filePath = "e:\\mytemp\\hello.txt";
-
- File file1 = new File(filePath);
- if (!file1.exists()) {
- file1.createNewFile();
- BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath));
- bufferedWriter.write("hello,world");
- System.out.println("已写入");
- bufferedWriter.close();
- }
- if (file1.exists()) {
- System.out.println("已存在,不用在创建");
- }else{
- System.out.println("文件已存在");
- }
- }
- }