向指定文件夹创建文件,并且写入信息

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();
        }
    }
}
相关推荐
用户69371750013841 天前
Google 正在“收紧侧加载”:陌生 APK 安装或需等待 24 小时
android·前端
蓝帆傲亦1 天前
Web 前端搜索文字高亮实现方法汇总
前端
用户69371750013841 天前
Room 3.0:这次不是升级,是重来
android·前端·google
似水明俊德1 天前
02-C#.Net-反射-面试题
开发语言·面试·职场和发展·c#·.net
Leinwin1 天前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
漫随流水1 天前
旅游推荐系统(view.py)
前端·数据库·python·旅游
薛定谔的悦1 天前
MQTT通信协议业务层实现的完整开发流程
java·后端·mqtt·struts
enjoy嚣士1 天前
springboot之Exel工具类
java·spring boot·后端·easyexcel·excel工具类
Thera7771 天前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
罗超驿1 天前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist