Java第十八天(练习拷贝文件、高效缓冲字符流、字节流、高效缓冲字节流)

(3)练习拷贝文件

import s20190517.ReaderTest3;

import java.io.*;

public class CopyDemo {
    public static void main(String[] args) {
        //创建文件
        File file = new File("src\\s20190517\\ReaderTest3.java");
        File file1 = new File("ReaderTest3.java");
        Reader reader = null;
        Writer writer = null;
        try {
            reader = new FileReader(file);
            writer = new FileWriter(file1);
            int len = 1;
            char[] cs = new char[1024];
            while ((len = reader.read(cs)) != -1) {
                writer.write(cs, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

(4)高效输入流

6aed0f19a0ea836f6a075ca1ced1c893ad8.jpg

28e7852a512afcb5bfbe2d861c34b4894e1.jpg

fc914233b709374ea4ca331c2b729d658fc.jpg

package s20190520;

import java.io.*;

public class BufferedReaderDemo {
    public static void main(String[] args) {
        //创建一个BufferedReader对象
        BufferedReader reader=null;
        try {
//注意BufferedReader构造器传入的是Reader对象为参数
            reader=new BufferedReader(new FileReader("src\\s20190520\\CopyDemo.java"));
            /*//读取一个文本行,边界是null
            String s = reader.readLine();
            System.out.println(s);*/
            //使用循环读取整个文件
            String line=null;
            while ((line=reader.readLine())!=null){
                System.out.println(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //外层关闭的时候内层自动关闭
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

(5)高效输出流

e5dcbfdd26b1066916a58cad1b0c4b1f830.jpg

ea7308efe7a223e9a05f5bd231e9406ca4d.jpg

import java.io.*;

public class BufferedWriterDemo {
    public static void main(String[] args) {
        //创建一个BufferedWriter对象
        BufferedWriter writer=null;
        try {
            writer=new BufferedWriter(new FileWriter("a.txt"));
            writer.write("莫名我就喜欢你");
            writer.newLine();//换行,要写下一行的时候必须进行这个操作才会换行
            writer.flush();//刷新
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //外层关闭的时候内层自动关闭
            if(writer!=null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
import java.io.*;

public class BufferCopyDemo {
    public static void main(String[] args) {
        BufferedReader reader=null;
        BufferedWriter writer=null;
        try {
            reader=new BufferedReader(new FileReader("src\\s20190517\\ReaderTest3.java"));
            writer=new BufferedWriter(new FileWriter("ReaderTest3.java"));
            //定义一个读取和被写入的字符串
            String line=null;
            //给字符串赋值,null作为循环终止条件
            while ((line=reader.readLine())!=null){
                //每次写入一行换行并刷新
                writer.write(line);
                writer.newLine();
                writer.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(reader!=null){
                    reader.close();
                }
                if(writer!=null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

83.字节流

f9be5a84609bb9fd240a2b9ac6f00296497.jpg

1035009d7a083bb005f4ee9f8ba89ff86dd.jpg

import java.io.*;

public class OutputStreamDemo {
    public static void main(String[] args) {
        //创建一个输出字节流
        OutputStream out = null;
        try {
            byte[] bs={98,99,100,101,102};
            //可以new File或者直接写文件名
            out = new FileOutputStream(new File("aa.txt"));
            //写入单个字符
            out.write(98);
            //写入整个数组
            out.write(bs);
            //写入数组的指定部分
            out.write(bs,2,2);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if(out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

bd0bfff5e6cb3b5e8b9be67b5e517a303a8.jpg

字节流不需要flush

9c9dc3e9b49d196220e2a2db7b1915f09e0.jpg

a76062e1b4f2eba1fe597d07f71e1329b1c.jpg

import java.io.*;

public class InputStreamDemo {
    public static void main(String[] args) {
        InputStream in=null;
        try {
            in=new FileInputStream(new File("aa.txt"));
            //读到的内容也是以assci码显示
            int read = in.read();
            //转化为字节,读一个字节,如果是汉字或者其他国家文字一次读半个字符可能会出现乱码
            System.out.println((char)read);
            read = in.read();
            System.out.println((char)read);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

adb4136fb8be56468aca6e14bd774a48dff.jpg

import java.io.*;
import java.util.Arrays;

public class InputStreamDemo1 {
    public static void main(String[] args) {
        InputStream in=null;
        try {
            in=new FileInputStream("aa.txt");
            byte[] bs=new byte[6];
            int len = in.read(bs);
            System.out.println("读取的长度是"+len+"   读取的内容是"+Arrays.toString(bs) +"   转换成字符串打印"+new String(bs));
            len = in.read(bs);
            System.out.println("读取的长度是"+len+"   读取的内容是"+new String(bs) );
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

6d02fd79b89d7c988a2863ba9a6aab86228.jpg

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo2 {
    public static void main(String[] args) {
        InputStream in=null;
        try {
            in=new FileInputStream("aa.txt");
            //定义一个字符串,如果中间有汉字最好长度尽可能长包括
            byte[] bs=new byte[3];
            //定义读取的长度
            int len = 0;
            while((len=in.read(bs))!=-1){
                System.out.print(new String(bs,0,len));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

b00cc1bd297aaa85383c45c990043afe690.jpg

拷贝二进制文件(如压缩包、图片)

import java.io.*;

public class CopyDemo1 {
    public static void main(String[] args) {
        OutputStream out=null;
        InputStream in=null;
        try {
            //创建字节流输入对象,输入需要读取的对象参数
            in=new FileInputStream(new File("C:\\Users\\cheung\\Desktop\\DCIM\\P90122-184132.jpg"));
            //创建字节流输出对象
            out=new FileOutputStream("P90122-184132.jpg");
            //创建数组接收读取的内容
            byte[] bs=new byte[1024];
            int len=0;
            while((len=in.read(bs))!=-1){
                //将读取的内容写入输出
                out.write(bs,0,len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(in!=null){
                    in.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

84.高效缓冲字节流

5fb28a476b6c92f8675a8071de946f0f8e3.jpg

29e4cfda293a54f6de9b43353bcaa99581a.jpg

fce702336a51fef3379154294a933833033.jpg

900e50787a05acb146b0076792c76e192a4.jpg

adc6afcb0bf7f50e8a786f920fbc2ba648a.jpg

eb1fae8c4e377720f27c72a274849987f47.jpg

import java.io.*;

public class BufferedCopyDemo2 {
    public static void main(String[] args) {
        BufferedInputStream in=null;
        BufferedOutputStream out=null;
        try {
            in=new BufferedInputStream(new FileInputStream("C:\\Users\\cheung\\Desktop\\DCIM\\Selfie\\P80812-152904.jpg"));
            out=new BufferedOutputStream(new FileOutputStream("P80812-152904.jpg"));
            byte[] bs=new byte[1024];
            //定义一个读取长度标识
            int len=0;
            while((len=in.read(bs))!=-1){
                out.write(bs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(in!=null){
                    in.close();
                }
                if(out!=null){
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

转载于:https://my.oschina.net/u/4110331/blog/3052014

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值