Sunday, April 26, 2009

字符流

Reader & Writer
支持Unicode标准字符集(字节流只支持ISO-Latin-1 8-bit)。在处理数据时,会根据系统默认的字符编码来进行字符转换,Reader & Writer 是抽象类,在进行文本文件的字符读写时真正会使用其子类
例:BIG5 &ASCII
package 输入输出;
import java.io.*;
public class ReaderDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String arg="test.txt";
try{
PushbackInputStream pushbackInputStream=
new PushbackInputStream(new FileInputStream(arg));
byte[] array=new byte[2];
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(array);
//reader会从已读的位数组中取出数据
InputStreamReader reader=new InputStreamReader(byteArrayInputStream);

int tmp=0;
int count=0;

while((count=pushbackInputStream.read(array))!=-1){
//两个字节转换为整数
tmp=(short)((array[0]<<8)|(array[1] tmp="tmp&0xffff;">=0xA440 && tmp<0xffff){
System.out.println("BIG5:"+(char)reader.read());
//重置ArrayInputStream的读取光标
//下次reader才会重新读取数据
byteArrayInputStream.reset();
}
else{
//将第二个字节推回流
pushbackInputStream.unread(array, 1, 1);
//显示ASCII范围的字符
System.out.println("ASCII:"+(char)array[0]);
}
}
pushbackInputStream.close();
}
catch(IOException e){
e.printStackTrace();
}

}

}

InputStreamReader & OutputStreamWriter
分别为Reader和Writer的子类
对InputStream和OutputStream进行字符处理
例:文件复制
package 输入输出;
import java.io.*;
public class StreamReaderWriterDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String arg="test2.txt";
try{
InputStreamReader inputStreamReader=
new InputStreamReader(
new FileInputStream(arg));

OutputStreamWriter outputStreamWriter=
new OutputStreamWriter(
new FileOutputStream("backup_"+arg));

int ch=0;
while((ch=inputStreamReader.read())!=-1){
System.out.print((char)ch);
outputStreamWriter.write(ch);
}
System.out.println();

inputStreamReader.close();
outputStreamWriter.close();
}
catch(IOException e){
e.printStackTrace();
}

}

}


FileReader & FileWriter
存取的是文本文件
字符的转换会根据系统默认的编码
(若要指定编码,使用InputStreamReader & OutputStreamWriter)

package 输入输出;
import java.io.*;
public class FileReaderWriterDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String arg="test.txt";
try{
FileReader fileReader=new FileReader(arg);
FileWriter fileWriter=new FileWriter(arg+".txt");
int in=0;
char[] wlnChar={'\r','\n'};
while((in=fileReader.read())!=-1){
if(in=='\n'){
//写入"\r\n"
fileWriter.write(wlnChar);
}
else
fileWriter.write(in);
}

fileReader.close();
fileWriter.close();
}
catch(IOException e){
e.printStackTrace();
}

}

}

BufferedReader & BufferedWriter
个拥有8192字符的缓冲区
例:
package 输入输出;
import java.io.*;
public class BufferedReaderWriterDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String arg="test.txt";
try{
BufferedReader bufReader=new BufferedReader(
new InputStreamReader(System.in));
BufferedWriter bufWriter=new BufferedWriter(
new FileWriter(arg));

String input=null;

//每读一行进行写入动作
while(!(input=bufReader.readLine()).equals("quit")){
bufWriter.write(input);
//newLine()方法写入与操作系统相依的换行字符
bufWriter.newLine();
}

bufReader.close();
bufWriter.close();
}
catch(IOException e){
e.printStackTrace();
}

}

}

PrintWriter
例:
package 输入输出;
import java.io.*;
public class StreamWriterDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String arg="test.txt";
try{
//"简体中文"四个字的GB2312编码
byte[] sim={
(byte)0xbc,(byte)0xf2,
(byte)0xcc,(byte)0xe5,
(byte)0xd6,(byte)0xd0,
(byte)0xce,(byte)0xc4
};
//数组作为流来源
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(sim);
InputStreamReader inputStreamReader=new InputStreamReader(byteArrayInputStream,"GB2312");

//PrintWriter接受Writer实例作为变量
PrintWriter printWriter=
new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(arg),"GB2312"));

int in=0;

printWriter.print("PrintWriter:");
//写入数组内容
while((in=inputStreamReader.read())!=-1){
printWriter.print((char)in);
}
printWriter.println();

printWriter.close();
byteArrayInputStream.reset();
//PrintStream接受OutputStream实例作为变量
PrintStream printStream=new PrintStream(
new FileOutputStream(arg,true),true,"GB2312");

printStream.print("PrintStream: ");
//写入数组内容
while((in=inputStreamReader.read())!=-1){
printStream.print((char)in);
}
printStream.println();

inputStreamReader.close();
printStream.close();
}
catch(IOException e){
e.printStackTrace();
}

}

}


CharArrayReader & CharArrayWriter
将位数组作为流入来源和输出目的
例:
package 输入输出;
import java.io.*;
import java.util.*;
public class CharArrayReaderWriterDemo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String arg="test.txt";
try{
File file=new File(arg);
BufferedReader bufInputReader=
new BufferedReader(
new FileReader(file));
//将文件读入字符数组
CharArrayWriter charArrayWriter=new CharArrayWriter();
char[] array=new char[1];
while(bufInputReader.read(array)!=-1){
charArrayWriter.write(array);
}
charArrayWriter.close();
bufInputReader.close();

//显示字符数组内容
array=charArrayWriter.toCharArray();
for(int i=0;i< array.length;i++)
System.out.print(array[i]+" ");
System.out.println();

//让用户输入位置与修改内容
Scanner scanner=new Scanner(System.in);

System.out.print("输入修改位置:");
int pos=scanner.nextInt();
System.out.print("输入修改字符:");
char ch=scanner.next().charAt(0);
array[pos-1]=ch;
//将字符数组内容存回文件
CharArrayReader charArrayReader=new CharArrayReader(array);
BufferedWriter bufWriter=
new BufferedWriter(
new FileWriter(file));
char[] tmp=new char[1];
while(charArrayReader.read(tmp)!=-1){
bufWriter.write(tmp);
}

charArrayReader.close();
bufWriter.flush();
bufWriter.close();
}
catch(IOException e){
e.printStackTrace();
}

}

}

No comments:

Post a Comment