java
public static void main(String[] args) throws IOException {
String filePath = "E:\\mytemp";
File file = new File(filePath);
String filePath2 = "E:\\mytemp\\hello.txt";
File file1 = new File(filePath2);
if (!(file.exists() && file.isDirectory() && file1.exists())) {
System.out.println("目录不存在,正在创建...");
file.mkdir();
} else {
System.out.println("该文件已经存在,就不要再重复创建了");
}
//以下代码正在写入文本内容
{
System.out.println("文件、目录已存在,正在输入文本~");
String context = "hehdhdfh~";
BufferedWriter bw = new BufferedWriter(new FileWriter(filePath2));
bw.write(context);
bw.close();
}
}
java
public class Homework01 {
public static void main(String[] args) throws IOException {
String filePath = "E:\\mytemp";
File file = new File(filePath);
if(!file.exists()){
if(file.mkdir()){
System.out.println("目录创建成功");
}else {
System.out.println("创建目录失败");
}
}
String filePath2 = filePath+"\\hello.txt";
file = new File(filePath2);
if(!file.exists()){
//创建文件
if(file.createNewFile()){
System.out.println(filePath2+"文件创建成功~");
}else{
System.out.println(filePath2+"文件创建失败~");
}
}else{
System.out.println(filePath2+"文件已经存在,无需继续创建~");
}
//以下代码正在写入文本内容
{
System.out.println("正在输入文本~");
String context = "hello,worldl!~";
BufferedWriter bw = new BufferedWriter(new FileWriter(filePath2));
bw.write(context);
System.out.println("文本输入成功!");
bw.close();
}
}
}