Java常用的文件操作

网友投稿 619 2022-05-28

1. 文件

2. 文件流

2. 常用的文件操作

2.1 创建文件对象相关构造器和方法

2.2 获取文件相关信息的方法

2.4 目录的操作和文件删除

1. 文件

2. 文件流

2. 常用的文件操作

2.1 创建文件对象相关构造器和方法

2.2 获取文件相关信息的方法

2.4 目录的操作和文件删除

1. 文件

文件,对我们并不陌生,文件是保存数据的地方,比如经常使用的word文档,txt文件,excel文件…都是文件。它既可以保存一张图片,也可以保持视频,声音等。

2. 文件流

文件在程序中是以流的形式来操作的

流:数据在数据源(文件)和程序(内存)之间经历的路径

输入流:数据从数据源(文件)到程序(内存)的路径

输出流:数据从程序(内存)到数据源(文件)的路径

2. 常用的文件操作

2.1 创建文件对象相关构造器和方法

【Java】常用的文件操作

相关方法

new File(String pathname)//根据路径构建一个File对象 new File(File parent,String child)//根据父目录文件+子路径构建 new File(String parent,String child)//根据父目录+子路径构建

createNewFile创建新文件

案例演示

请在e盘下,创建文件 news1.txt、news2.txt、news3.txt,用三种不同的方式创建

public class FileCreate { public static void main(String[] args) { } //方式1 new File(String pathname) @Test public void create01() { String filePath = "e:\\news1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("文件创建成功"); } catch (IOException e) { e.printStackTrace(); } } //方式2 new File(File parent,String child) //根据父目录文件+子路径构建 //e:\\news2.txt @Test public void create02() { File parentFile = new File("e:\\"); String fileName = "news2.txt"; //这里的file对象,在java程序中,只是一个对象 //只有执行了createNewFile 方法,才会真正的,在磁盘创建该文件 File file = new File(parentFile, fileName); try { file.createNewFile(); System.out.println("创建成功~"); } catch (IOException e) { e.printStackTrace(); } } //方式3 new File(String parent,String child) //根据父目录+子路径构建 @Test public void create03() { String parentPath = "e:\\"; String fileName = "news4.txt"; File file = new File(parentPath, fileName); try { file.createNewFile(); System.out.println("创建成功~"); } catch (IOException e) { e.printStackTrace(); } } }

2.2 获取文件相关信息的方法

getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory

应用案例演示

如何获取到文件的大小,文件名,路径,父File,是文件还是目录(目录本质也是文件,一种特殊的文件)是否存在。

//获取文件信息 @Test public void info() { File file = new File("e:\\news1.txt"); //调用X响应方法,得到相应信息 System.out.println("文件名字=" + file.getName()); System.out.println("文件绝对路径=" + file.getAbsolutePath()); System.out.println("文件父级目录=" + file.getParent()); System.out.println("文件大小(字节)=" + file.length()); System.out.println("文件是否存在=" + file.exists()); System.out.println("是不是一个文件=" + file.isFile()); System.out.println("是不是一个目录=" + file.isDirectory()); }

2.4 目录的操作和文件删除

mkdir创建一级目录、mkdirs创建多级目录、delete删除空目录或文件

案例演示

判断d:\\news1.txt是否存在,如果存在就删除,否则提示不存在

@Test public void m1() { String filePath = "e:\\news1.txt"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "删除成功"); } else { System.out.println(filePath + "删除失败"); } } else { System.out.println("该文件不存在..."); } }

判断 d:\\demo02是否存在,存在就删除,否则提示不存在

//在java编程中,目录也被当做文件 @Test public void m2() { String filePath = "D:\\demo02"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "删除成功"); } else { System.out.println(filePath + "删除失败"); } } else { System.out.println("该目录不存在..."); } }

判断 d:\\demo\\a\\b\\c目录是否存在,如果存在就提示已经存在,否则就创建

@Test public void m3() { String directoryPath = "D:\\demo\\a\\b\\c"; File file = new File(directoryPath); if (file.exists()) { System.out.println(directoryPath + "存在.."); } else { if (file.mkdirs()) { //创建一级目录使用mkdir() ,创建多级目录使用mkdirs() System.out.println(directoryPath + "创建成功.."); } else { System.out.println(directoryPath + "创建失败..."); } } }

Java

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【Python编程创造营·第二期】最终积分排行榜和最终考核成绩公示!
下一篇:java随机数的陷阱
相关文章