• 字符流,字节流,缓存流详解


    package com.bjsxt.init;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;

    public class mFileTest05 {

    public static void main(String[] args) {

    File newFile = new File("e:\test\myText.txt");

    try {
    FileOutputStream fos = new FileOutputStream(newFile);//用字节流写入文件
    OutputStreamWriter osw = new OutputStreamWriter(fos);//字符流字节流转换
    BufferedWriter bw = new BufferedWriter(osw);//缓存流执行效率更快
    //fos.write("你好啊c");
    //fos.write(97);
    int ch = 20320;//你
    // fos.write(ch);
    char cbuf[] =new char[1];
    cbuf[0]=(char)ch;

    osw.write(cbuf);

    osw.write("张志勤");
    osw.write("c");
    osw.flush();
    // Long a=System.currentTimeMillis() ;
    // for(int i=0;i<10000;i++){
    // bw.write("窗前明月光 ");
    // bw.write("疑是地上霜 ");
    //// osw.write("窗前明月光 ");
    //// osw.write("疑是地上霜 ");
    // }
    //
    // Long a1=System.currentTimeMillis() ;
    // Long a2=a1-a;
    // System.out.println("用了多少秒:"+a2);

    // osw.flush();
    bw.close();
    osw.close();
    fos.close();

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }

    File file = new File("e:\test\myText.txt");


    try {
    FileInputStream fis = new FileInputStream(file);//读一个字节
    InputStreamReader isr = new InputStreamReader(fis);//是字节流与字符流之间的桥梁,能将字节流输出为字符流,
    BufferedReader br = new BufferedReader(isr);
    // int a;
    // while((a = fis.read()) != -1){
    // System.out.print((char)a);
    // }
    // int c;
    // while((c = isr.read()) != -1){
    // System.out.println((char)c);
    // }


    String line;
    while((line = br.readLine()) != null){
    System.out.println(line);
    }


    br.close();
    isr.close();
    fis.close();

    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }



    }

    }

  • 相关阅读:
    ScrollView反弹效果的实现
    Unity 3D本地公布WebPlayer版时&quot;Failed to download data file&quot;解决方式
    win7休眠的开启与关闭方法命令行操作和图文结合的鼠标操作
    使用Javascript D3创建属于你的涂鸦作品
    android获取自己定义控件位置坐标,屏幕尺寸,标题栏,状态栏高度
    [Python]Use Flask-Admin with PostgreSQL
    [LeetCode] Best Time to Buy and Sell Stock
    spring实战五之Bean的自动检测
    FireBug使用总结
    javascript的window.onload()方法和jQuery的$(document).ready()的对比
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/9096782.html
Copyright © 2020-2023  润新知