zoukankan      html  css  js  c++  java
  • Java 字节流和字符流练习

    Byte Stream --字节流

    package com.cmm.io4;
    
    import java.io.*;
    
    public class FileIo {
    
        /**
         * @param args
         * @author cmm
         * @Title: FileOutputStream & FileInputStream -- Byte Stream
         */
        public static void main(String[] args) throws Exception {
            // TODO Auto-generated method stub
    
            // OutputStream out = null;
            // out = new FileOutputStream(new File("F:\\cmm.txt"));
            
            /* FileOutputStream */
            File f = new File("F:\\cmm.txt");
            FileOutputStream out = new FileOutputStream(f);
            
            String str = "Cmm welcome to my board htx";
            out.write(str.getBytes("GB2312"));
            out.close();
            
            /* FileInputStream */
            FileInputStream in = new FileInputStream(f);
            byte b[] = new byte[500];
            int len = 0;
            len = in.read(b);
            in.close();
            System.out.println(new String(b, 0, len));
        }
    
    }

    Char Stream --字符流(用到了缓存)

    package com.cmm.io4;
    
    import java.io.*;
    
    public class RWTest {
    
        /**
         * @param args
         * @author cmm
         * @Tilte: Reader/Writer --Char Stream
         */
        public static void main(String[] args)  throws Exception
        {
            File f = new File("F:\\cmm.txt");
            /* Write Output char stream */
            // Writer out = null;
            FileWriter out = new FileWriter(f);
            String str = "So wat  are you do now? ";
            out.write(str);
            out.flush();
            out.close();
    
            /* Read Input char stream */
            // Reader in = null;
            FileReader in = new FileReader(f);
            char ch[] = new char[500];
            int len = 0;
            len = in.read(ch);
            in.close();
            System.out.println(new String(ch, 0, len));
    
        }
    }

     

  • 相关阅读:
    WPF 关于多语言 的实现 学习,利用反射中的特性
    WPF Textbox 中文输入崩溃 规避
    自定义控件.依赖项属性同步更新内部控件属性
    HexInput
    HashMap实现原理分析
    centos下nginx安装
    dos下mybatis自动生成代码
    设计模式之装饰器模式
    Java 中的悲观锁和乐观锁的实现
    redis分布式锁
  • 原文地址:https://www.cnblogs.com/bluestorm/p/2683734.html
Copyright © 2011-2022 走看看