博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java文件操作
阅读量:6907 次
发布时间:2019-06-27

本文共 4917 字,大约阅读时间需要 16 分钟。

hot3.png

一、 创建

1. 创建文档:createNewFile()

File f1 = new File("/Users/**/Documents/dev/fileTest/a.txt");        try {            boolean b = f1.createNewFile();  //如果已经存在该文件,则返回false,不存在则返回true,成功创建            System.out.println(b);        } catch (IOException e) {            e.printStackTrace();        }

2. 创建文件夹:mkdir()和mkdirs()

File file = new File("/Users/**/Documents/dev/fileTest/a");boolean b = file.mkdir();//如果已经存在该文件夹,则返回false,不存在则返回true,成功创建.//boolean b = file.mkdirs();//创建多级目录System.out.println(b);

二、删除文件:delete()和deleteOnExit()

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");//      file.delete();  //手动删除文件        file.deleteOnExit();  //程序退出时自动删除文件

三、查询文件

1、是否存在:exists()

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");boolean b = file.exists();//如果已经存在,则返回true,不存在则返回falseSystem.out.println(b);

2、isDirectory():判断是否为文件夹;

3、isFile():判断是否为文件;等等...

参考于:

四、读取文件

采用Reader是非常方便的,比如FileReader,InputStreamReader和BufferedReader。其中最重要的类是InputStreamReader, 它是字节转换为字符的桥梁。你可以在构造器重指定编码的方式,如果不指定的话将采用底层操作系统的默认编码方式,例如GBK等

1. FileInputStream

一次单个字节读取

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");        InputStream in = null;        try {            in = new FileInputStream(file);//以字节为单位读取文件内容,一次读一个字节 换行也会自动换行            int tempbyte;            while ((tempbyte = in.read()) != -1) {                System.out.write(tempbyte);            }            in.close();        } catch (IOException e) {            e.printStackTrace();            return;        }

用 byte[] bt = new byte[100] 一次多个字节(自定义)读取

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");        byte[] bt = new byte[100];        InputStream in = null;        try {            in = new FileInputStream(file);//以字节为单位读取文件内容,一次读一个字节            int i;            while ((i = in.read(bt)) != -1) {                System.out.write(bt,0,i);            }            in.close();        } catch (IOException e) {            e.printStackTrace();            return;        }

2. InputStreamReader

一次一个char字符读取

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");        Reader in = null;        try {            in = new InputStreamReader(new FileInputStream(file),"utf8");            int i = 0;            while ((i = in.read()) != -1) {                System.out.write(i);            }            in.close();        } catch (IOException e) {            e.printStackTrace();            return;        }

一次多个char字符读取

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");        char[] tempchars = new char[30];        Reader in = null;        try {            in = new InputStreamReader(new FileInputStream(file),"utf8");            int i = 0;            while ((i = in.read(tempchars)) != -1) {                System.out.print(tempchars);            }            in.close();        } catch (IOException e) {            e.printStackTrace();            return;        }

3. BufferedReader:按正行读取

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");        BufferedReader reader = null;        try {            reader = new BufferedReader(new FileReader(file));            String str = "";            while ((str = reader.readLine()) != null) {                System.out.print("\n"+str);            }            reader.close();        } catch (IOException e) {            e.printStackTrace();            return;        }

4. FileReader

File file = new File("/Users/**/Documents/dev/fileTest/a.txt");        FileReader reader = null;        try {            reader = new FileReader(file);            int i = 0;            while ((i = reader.read()) != -1) {                System.out.write(i);            }            reader.close();        } catch (IOException e) {            e.printStackTrace();            return;        }

五、写入文档

1.InputStream转为OutputStream

File fileI = new File("/Users/bob/Documents/dev/fileTest/a.txt");        File fileO = new File("/Users/bob/Documents/dev/fileTest/b.txt");        try {            InputStream it = new FileInputStream(fileI);            OutputStream os = new FileOutputStream(fileO);            byte flush[]  = new byte[1024];            int len = 0;            while(0<=(len=it.read(flush))){                os.write(flush, 0, len);            }            //关闭流注意: 先打开的后关            os.close();            it.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }

2.写入一串字符

File fileO = new File("/Users/**/Documents/dev/fileTest/b.txt");        try {            PrintWriter w = new PrintWriter(new OutputStreamWriter(                    new FileOutputStream(fileO), "utf8"));            w.write("hello world 你好世界");            w.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }

 

 

转载于:https://my.oschina.net/u/3697586/blog/1601045

你可能感兴趣的文章
开源Xen是如何衰落的?
查看>>
Pivot Table系列之切片器 (Slicer)
查看>>
windows下安装mysql5.6及基本命令
查看>>
jsp的九个内置对象简介
查看>>
用户如何获得***服务---步骤与效果
查看>>
学习沟通技巧--- SOFTEN法则与SOLER法则
查看>>
用户密码重设对EFS的影响
查看>>
基于mdrill的大数据分析
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
gitlab web hooks 应用
查看>>
STM32的停机模式与唤醒
查看>>
安全运维之端口安全
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
【转载】什么是站点,Active Directory系列之十一
查看>>
代码设计中常见的pitfall-----翻译《An Introduction to Design Patterns in C++ with Qt4 》
查看>>
配置struts2.3.15+felix网站项目时遇到Template /osgi/admin/viewBundles.ftl not found解决方案...
查看>>
Red Hat Enterprise Liunx6 配置apache 全攻略
查看>>
CentOS 5.5下LVM的分区管理
查看>>