Java-字符流练习。(新手)

 

 


参考手册:

 


 

关键字:

write()   写入存取

close()  结束

flush()   刷新缓冲区(缓冲区就是临时存放数据的区域。)

currentTimeMillis()  返回以毫秒为单位的当前时间

 


 

创建方法:

FileWriter fwr = new FileWriter("C:\\ja.txt");
创建对象,并且给对象指定路径。

实例:

//导入的包。
import java.io.*;
//创建的一个类。
public class ZFLlx {
    //公共静态的主方法。
    public static void main(String[] args)throws IOException{
        //调用方法。
        lx4();
    }
    public  static void lx4(){
        FileOutputStream fos = null;
        try {
            //指定盘符和文件。
            fos = new FileOutputStream("C:\\ja.txt");
            // 指定盘符内并且设置格式。(OutputStreamWriter字符输出流)
            OutputStreamWriter sow = new OutputStreamWriter(fos,"UTF-8");
            //添加。
            sow.write("您好");
            //结束释放资源。
            sow.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static void lx3() throws IOException {
        //指定盘符和文件。
        FileWriter fwr = new FileWriter("C:\\ja.txt");
        //输入添加的字符串。
        fwr.write("Qy97");
        fwr.write("威武");
        //创建字符数据类型,添加要输入的字符。
        char[] c = {'a','b','c','d','e'};
        //写入。
        fwr.write(c);
        //刷新缓冲区(缓冲区就是临时存放数据的区域。)
        fwr.flush();
        //结束释放资源。
        fwr.close();
    }

    private static void lx2() throws IOException {
        //计算开始的时间。返回的是以毫秒为单位的时间。(1秒为1000毫秒)
        long l = System.currentTimeMillis();
        //指定盘符和文件。
        FileReader frr = new FileReader("C:\\ja.txt");
        // 创建字符数组。char是字符类型,一次读1024个字符
        char[] c = new char[1024];
        //定义一个变量
        int len = 0;
        //创建while语句。
        while ((len = frr.read(c))!=-1){
            System.out.println(new String(c,0,len));
        }
        //结束释放资源。
        frr.close();
        //结束时间。
        long ll = System.currentTimeMillis();
        //结束时间减去开始时间就会得到运行时间。
        System.out.println(ll-l);
    }

    public static void lx1() {
        FileReader fir = null;
        FileWriter fwr = null;

        try {
            fir = new FileReader("C:\\ja.txt");
            fwr = new FileWriter("D:\\ja.txt");
            char[] c =  new char[2024*10];
            int len = 0;
            while ((len=fir.read(c))!=-1){
                fwr.write(c,0,len);
                //flush 用来刷新缓冲区的,只有字符流才需要刷新
                fwr.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fir!=null){
                try {
                    fir.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (fwr!=null){
                        try {
                            fwr.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

    }
}

 

执行结果

lx1:


 

lx2:


 

lx3:


 

lx4:

 

posted @ 2019-03-25 20:15  浪子。  阅读(294)  评论(0编辑  收藏  举报