现在的位置: 首页 > 综合 > 正文

Java 字节流和字符流练习

2012年07月13日 ⁄ 综合 ⁄ 共 1157字 ⁄ 字号 评论关闭

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

    }
}

 

 

【上篇】
【下篇】

抱歉!评论已关闭.