Java进阶核心之Reader、 Writer字符流
Java字符输入流Reader
Reader是输入字符流的父类,它是一个抽象类,部分库不推荐使用Reader/Writer,所以简单了解即可
int read()
讲解:一个字符一个字符的读,只能用来操作文本(不能写图片音频视频)
int read(char cbuf[ ] )
讲解:从输入字符流中读取一定数量的字符,并将其存储在缓冲区数组cbuf中,返回实际读取的字符数,如果已经到达流未尾而没有可用的字节,则返回- 1
void close()throws IOException
讲解:关闭输入流并释放与该流关联的系统资源
public FileReader(String fileName)throws FileNotFoundException{
super (new FileInputStream (fileName));
}
public FileReader(File file)throws FileNotFoundException{
super (new FileInputStream (file));
}
package domee. chapter12;
import java. io. File;
import java. io. FileReader;
import java. io. IOException;
import java. io. Reader;
public class ReadTest {
public static void main ( String[ ] args) throws IOException {
test2 ( ) ;
}
public static void test1 ( ) throws IOException{
String dir = "C:\\Users\\123\\Desktop\\test" ;
String name = "1.txt" ;
File file = new File ( dir, name) ;
Reader input = new FileReader ( file) ;
int ch;
while ( ( ch = input. read ( ) ) != - 1 ) {
System. out. print ( ( char ) ch) ;
}
input. close ( ) ;
}
public static void test2 ( ) throws IOException{
String dir = "C:\\Users\\123\\Desktop\\test" ;
String name = "1.txt" ;
File file = new File ( dir, name) ;
Reader input = new FileReader ( file) ;
char c[ ] = new char [ 1024 ] ;
int lenght = input. read ( c) ;
System. out. println ( lenght) ;
System. out. print ( new String ( c, 0 , lenght) ) ;
input. close ( ) ;
}
}
Java字符输入流Writer
public void write(int c)throws IOException
讲解:直接将int 型数据作为参数的话,是不会写入数字的,而是现将数字按照asc11码表转换为相应的字符,然后写入
public void write(String str)throws IOException
讲解:要想实现数字和中文的写入,必须要用string为参数的
Write public abstract void write(char cbuf[ ] ,int off,int len)throws IOException;
讲解:将cbuf字符数组的一部分写入到流中,但能不能写1 en个字符取决于cbuf中是否有那么多
void flush()throws IOException
讲解:write是写到缓冲区中,可以认为是内存中,当缓冲区满时系统会自动将缓冲区的内容写入文件,但是一般还有一部分有可能会留在内存这个缓冲区中,所以需要调用flush空缓冲区数据。对比BufferWriter需要实时查表,效率低,其实缓冲区IO的各个都有,只不过很小被忽略,OutputStream都有flush方法,看子类是否有重写
void close()throws IOException
讲解:关闭输入流并释放与该流关联的系统资源
public FileWriter(String fileName)throws IOException
讲解:如果文件不存在,这会自动创建。如果文件存在,则会覆盖
public Filewriter(File file)throws IOException
讲解:如果文件不存在,这会自动创建。如果文件存在,则会覆盖
public Filewriter(String fileName,boolean append)throws IOException
讲解:加入true 参数,会实现对文件的续写,使用false 则会实现对文件的覆盖
public Filewriter(File file,boolean append)throws IOException
讲解:加入true 参数,会实现对文件的续写,使用false 则会实现对文件的覆盖
package domee. chapter12;
import java. io. File;
import java. io. FileWriter;
import java. io. IOException;
import java. io. Writer;
public class WriterTest {
public static void main ( String[ ] args) throws IOException {
test1 ( ) ;
}
public static void test1 ( ) throws IOException{
String dir = "C:\\Users\\123\\Desktop\\test" ;
String name = "2.txt" ;
File file = new File ( dir, name) ;
Writer writer = new FileWriter ( file, true ) ;
writer. write ( 21704 ) ;
writer. write ( 21704 ) ;
writer. write ( 21704 ) ;
writer. write ( 26089 ) ;
writer. write ( 19978 ) ;
writer. write ( 22909 ) ;
writer. write ( 65281 ) ;
writer. write ( "56789" ) ;
writer. flush ( ) ;
writer. close ( ) ;
}
}