Java文件流操作

一、文件创建和删除

   public static void main(String[] args) throws IOException {
   
        File file = new File("..\\hello-world.txt");//..表示在上机目录下创建hello-world.txt
        System.out.println(file.getPath());//返回当前相对路径
        System.out.println(file.getCanonicalFile());//返回绝对路径
        System.out.println(file.exists());//false
        //创建文件
        boolean re=file.createNewFile();
        System.out.println(re);
        System.out.println(file.exists());//true
       //删除文件
        System.out.println(file.exists());//true
        boolean se=file.delete();
        System.out.println(file.exists());//false
    }

二、文件读写分类(重点讨论)

文件大多数是二进制保存的,当然也有以保存字符的文本文件,当我们读写文件的时候,我们需要针对这两种文件类型有不同操作,针对二进制的文件我们叫字节流,也就是每次读写一个字节,针对文本文件我们读写操作叫字符流,每次读取多个字节,这里多个字节没有准确几个字节,GBK每个中文字符有两个字节,UTF8每个中文字符是三个字节

 三、测试环境

 描述:Test目录下创建了两个目录,aaa和bbb,这个两个目录下分别创建文件111和222

四、字符流操作

 1、读操作

··方法一 

public class Test_Read {
    public static void main(String[] args) throws IOException {
        Reader reader=new FileReader("e:/Test/aaa/111.txt");
        while (true){
            int x=reader.read();
            if(x==-1){
                //读取完毕
                break;
            }
            char y=(char)x;//一个中文字符一个中文字符读取
            System.out.print(y+" ");
        }
    }
}

描述:用read方法一个中文字符一个中文字符读取但是read方法返回的是一个整数,如果是-1说明为文件空读取结束,不为空传为字符输出。 

 ··方法二

public class Test_Read_Array {
    public static void main(String[] args) throws IOException {
        Reader reader=new FileReader("e:/Test/aaa/111.txt");
        while (true){
            char[] array=new char[2];
            int n=reader.read(array);//每次只读取两个中文字符,并且打印,多次读取
            if(n==-1){
                //读取完毕
                break;
            }
            System.out.println("n="+n);
            for (int i=0;i<n;i++) {
                System.out.print(array[i]);
            }
            System.out.println();
        }
    }
}

 文件资源泄露问题(重点)

你有没有发现上述代码存在致命问题!!!,在while循环里面我们频繁的下创建reader对象,这会填满文件资源表,文件资源表一旦填满那么我们在想操作该文件将会失败,所以我们应该及时关闭我们创建的对象,以读取对象为例修改:

public class Test_Read_Array {
    public static void main(String[] args) throws IOException {
        //try()中的资源会随着try结束而释放
       try ( Reader reader=new FileReader("e:/Test/aaa/111.txt");){
           while (true){
               char[] array=new char[2];
               int n=reader.read(array);//每次只读取两个中文字符,并且打印,多次读取
               if(n==-1){
                   //读取完毕
                   break;
               }
               System.out.println("n="+n);
               for (int i=0;i<n;i++) {
                   System.out.print(array[i]);
               }
               System.out.println();
           }
       }
    }
}

这只是一种简单写法,当然你可以在try后面用finally配合read.close(),同样可以 ,我们下面的代码将会采用这种简便的写法

2、写操作

public class Test_Write {
    public static void main(String[] args) {
        //文件路径后面不加true默认是false会覆盖原来文件内容,加了true是在原来文件内容后添加新内容
        try(Writer writer=new FileWriter("e:/Test/aaa/111.txt",true)){
            //写入数据
            writer.write("我看到了");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

五、字节流操作 

1、读操作

public class Test_Read {
    //字节流读操作
    public static void main(String[] args) throws IOException {
        try(InputStream inputStream=new FileInputStream("E:/Test/bbb/222.txt")){
            //开始读取文件
            Scanner scanner=new Scanner(inputStream);
            String s=scanner.next();
            System.out.println(s);
        }
    }
}

2、写操作 

public class Test_Write {
    public static void main(String[] args) throws FileNotFoundException {
        try(OutputStream outputStream=new FileOutputStream("E:/Test/bbb/222.txt")){
            PrintWriter writer=new  PrintWriter(outputStream);
            writer.printf("我知道了");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

 

啊?非但没有写入而且原来的数据害消失了!!! 

这是因为缓冲区问题,所谓缓冲区问题就是,我们这个写的操作实际上是先写入缓存区,缓存区的数据到达一定数量后在写进硬盘,所以我们现在要直接把缓冲区的内容写进硬盘就可以了

public class Test_Write {
    public static void main(String[] args) throws FileNotFoundException {
        try(OutputStream outputStream=new FileOutputStream("E:/Test/bbb/222.txt")){
            PrintWriter writer=new  PrintWriter(outputStream);
            writer.printf("我知道了",true);
            writer.flush();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值