您的位置:首页 > 编程语言 > Java开发

Java 字节流和字符流练习

2012-09-13 17:32 337 查看
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));

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: