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



    }

    }

  • 相关阅读:
    laravel MethodNotAllowedHttpException错误一个原因
    laravel查看执行sql的
    二维,多维数组排序array_multisort()函数的使用
    REMOTE_ADDR,HTTP_CLIENT_IP,HTTP_X_FORWARDED_FOR获取客户端IP
    学习正则笔记
    关于apidoc文档生成不了的一个原因
    laravel 表单验证 Exists 规则的基本使用方法
    laravel 500错误的一个解决办法
    关于laravel 用paginate()取值取不到的问题
    C语言寒假大作战02
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/9096782.html
Copyright © 2011-2022 走看看