java文件操作

Java文件操作指南

一、 创建

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,不存在则返回false
System.out.println(b);

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

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

参考于:http://blog.youkuaiyun.com/qingdujun/article/details/41223841

四、读取文件

采用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值