zoukankan      html  css  js  c++  java
  • 字符流,字节流,缓存流详解

    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();
    }



    }

    }

  • 相关阅读:
    aws-rds for mysql 5.7.34搭建备库
    Redis 未授权访问漏洞利用总结(转)
    mongoexport/mongimport命令详解
    mongodump/mongorestore命令详解
    redis stream类型 常用命令
    system_time_zone参数值由来
    MySQL加密解密函数AES_ENCRYPT AES_DECRYPT
    MySQL开启SSL加密
    MDL锁获取顺序和优先先
    explicit_defaults_for_timestamp 参数说明
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/9096782.html
Copyright © 2011-2022 走看看