当前位置: 首页 > news >正文

Java中文件的创建(三种方式),文件常用的方法

文件的创建

  1. 方式1: new File(String pathName) 根据路径构建一个File对象
  2. 方式2: new File(File parent,String child) 根据父目录文件+子路径构建
  3. 方式3: new File(String parent,String child) 根据父目录+子路径构建

代码:

//方式1 new File(String pathName) 根据路径构建一个File对象public void creat1() {String pathName = "d:\\creatFile\\file1.txt";File file1 = new File(pathName);try {file1.createNewFile();System.out.println("文件1创建成功");} catch (IOException e) {e.printStackTrace();}}//方式2 new File(File parent,String child) 根据父目录文件+子路径构建public void creat2(){File parent = new File("d:\\");String child = "file2.txt";File file2 = new File(parent, child);try {file2.createNewFile();System.out.println("文件2创建成功");} catch (IOException e) {e.printStackTrace();}}//方式3 new File(String parent,String child) 根据父目录+子路径构建public void creat3(){String parent = "d:\\";String child = "file3.txt";File file2 = new File(parent, child);try {file2.createNewFile();System.out.println("文件3创建成功");} catch (IOException e) {e.printStackTrace();}}

常用方法

  1. getName():获取文件名字。
  2. getAbsolutePath():获取文件的绝对路径。
  3. getParent():获取文件的父级目录。
  4. length():获取文件的大小(以字节为单位)。
  5. exists():判断文件是否存在。
  6. isFile():判断是否为一个文件。
  7. isDirectory():判断是否为一个目录。
  8. delete():删除文件
  9. mkdirs():创建多级目录
  10. mkdir():创建父目录(一级目录)

代码:

public void fileMethods(){//先创建文件对象File file = new File("d:\\file1.txt");//调用相应的方法,得到对应信息System.out.println("文件名字=" + file.getName());//getName、getAbsolutePath、getParent、length、exists、isFile、isDirectorySystem.out.println("文件绝对路径=" + file.getAbsolutePath());System.out.println("文件父级目录=" + file.getParent());System.out.println("文件大小(字节)=" + file.length());System.out.println("文件是否存在=" + file.exists());//TSystem.out.println("是不是一个文件=" + file.isFile());//TSystem.out.println("是不是一个目录=" + file.isDirectory());//F}
public void delete(){File file1 = new File("d:\\file3.txt");if(!file1.exists()){System.out.println("文件不存在");}else {if(file1.delete()){System.out.println("文件删除成功");}else {System.out.println("文件删除失败");}}}public void delete2(){File file1 = new File("d:\\file1");if(!file1.exists()){System.out.println("文件不存在");}else {if(file1.delete()){System.out.println("文件删除成功");}else {System.out.println("文件删除失败");}}}public void creat(){String directoryPath = "d:\\file1\\ret";File file = new File(directoryPath);if(file.exists()){System.out.println("文件存在");}else {if(file.mkdirs()){//mkdirs()创建多级目录,如:d:\\file1\\ret。mkdir()创建父一级目录,如d:\\file2System.out.println("文件创建成功");}else {System.out.println("文件创建失败");}}}
http://www.lryc.cn/news/145744.html

相关文章:

  • Spring boot中调用C/C++(dll)
  • 【Apollo学习笔记】——规划模块TASK之PATH_DECIDER
  • Lua学习(二)
  • 制作鲜花商城小程序的详细步骤
  • Ubuntu20以上高版本如何安装低版本GCC
  • context.WithCancel()的使用
  • vue3中引入百度地图
  • 【Linux-Day8- 进程替换和信号】
  • 日志文件之间关系和介绍及应用
  • mac电脑屏幕录制Berrycast Mac屏幕录制软件
  • 机器学习笔记之最优化理论与方法(一)最优化问题概述
  • 【ES5新特性一】 严格模式语法变化、全局的JSON对象、编码和解码的方法
  • Java【手撕滑动窗口】LeetCode 3. “无重复字符的最长子串“, 图文详解思路分析 + 代码
  • 学习哈哈哈哈
  • 05-基础例程5
  • 双基证券:预计未来还会有更多政策来吸引增量资金
  • 前端:html实现页面切换、顶部标签栏,类似于浏览器的顶部标签栏(完整版)
  • 强化自主可控,润开鸿发布基于RISC-V架构的开源鸿蒙终端新品
  • 软件设计师知识点·1
  • 修改Jupyter Notebook默认打开路径
  • 经典卷积网络
  • react+koa+vite前后端模拟jwt鉴权过程
  • VK1616是LED显示控制驱动电路/LED驱动IC、数显驱动芯片、数码管驱动芯片
  • 开箱报告,Simulink Toolbox库模块使用指南(五)——S-Fuction模块(C MEX S-Function)
  • 摄像头的调用和视频识别
  • 多通道分离与合并
  • JOJO的奇妙冒险
  • LeetCode56.合并区间
  • 【内推码:NTAMW6c】 MAXIEYE智驾科技2024校招启动啦
  • Python框架【模板继承 、继承模板实战、类视图 、类视图的好处 、类视图使用场景、基于调度方法的类视图】(四)