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

     

  • 相关阅读:
    markdown 书写文档的框架
    使用snap
    如何让pandas表格直接转换为markdown表格
    我们需要怎样去写博客
    jupyter notebook 远程访问
    安装tensorflowGPU版本
    Data Science Project
    使用python处理地理数据:Geopandas
    python移植环境
    Jupyter notbook& REVEAL.JS& nbconvert 使用jupyter notebook制作slides
  • 原文地址:https://www.cnblogs.com/bluestorm/p/2683734.html
Copyright © 2011-2022 走看看