字符流
1. FileReader和FileWriter
FileReader(String filePath);
FileReader(File fileObj);
FileWriter(String filePath);
FileWriter(String filePath, boolean append);
FileWriter(File fileObj);
例子:(以下例子是做备份,将autoexec.bat复制到autoexec.bak)
String file1="autoexec.bat";
String file2="autoexec.bak";
File inputfile=new File(file1);
File outputfile=new File(file2);
FileReader in=new FileReader(inputfile);
FileWriter out=new FileWriter(outputfile);
while ((c=in.read())!=-1) out.write((char) c);
2. CharArrayReader和CharArrayWriter
CharArrayReader(char array[]);
CharArrayReader(char array[], int start, int numChars);
CharArrayWriter(); //创建默认长度的缓冲器,
CharArrayWriter(int numChars); //创建numChars长度的缓冲器
例子:(以下例子过滤掉'!')
String stmt="we are the world! we are the future!"
byte b[]=stmt.getBytes();
CharArrayReader in=new CharArrayReader(b);
CharArrayWriter out=new CharArrayWriter();
while ((c=in.read())!=-1) {
if (c!='!') out.write(c);
}
byte c[]=new byte(out.count);
out.toCharArray(c);
System.out.println(c);
3. BufferedReader和BufferedWriter
BufferedReader(Reader inputStream);
BufferedReader(Reader inputStream, int bufSize);
BufferedWriter(Writer outputStream);
BufferedWriter(Writer outputStream, int bufSize);
例子:(将英文每句话的首字母改为大写,每句话结束标志是".!;?"符号之一)
String file1="chapter1.txt"
String file2="chapter2.txt"
FileReader inputfile=new FileReader(file1);
BufferedReader in=new BufferedReader(inputfile);
FileWriter outputfile=new FileWriter(file2);
BufferedWriter out=new BufferedWriter(outputfile);
boolean headflg=true; //headflg==true表示未读入的文本处于行首位置
while ((c=in.read())!=-1) {
if (headflg==true) {
if (c>='a' && c<='z') out.write((char)(c-32)); //变成大写字母并写入文件
else out.write(c);
}
else out.write((char)c);
if (c=='.' || c=='!' || c==';' || c=='?') headflg=true;
else if (c!=' ') headflg=false;
}
4. PushbackReader
参见PushbackInputStream,两者区别在于字节流与字符流
asp教程编程辅导汇总
[ASP]2016年12月2日JSP快速入门教程汇总
[JSP]2016年12月2日jsp基本用法和命令汇总
[JSP]2016年10月3日ASP编码教程:如何实现/使用缓存
[ASP]2015年4月15日ASP编码教程:asp缓存的分类
[ASP]2015年4月15日