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));
}
public class ReaderTest {
public static void main (String [] args) throws Exception {
test1();
test2();
}
public static void test1()throws Exception {
String dir = "C:\\Users\\79466\\Desktop\\test\\1.txt";
File file = new File(dir);
// 传入要读取的文件路径
Reader input = new FileReader(file);
int ch;
// 读取文件
while ( (ch = input.read())!=-1){
System.out.print((char)ch);
}
input.close();
}
public static void test2()throws Exception {
String dir = "C:\\Users\\79466\\Desktop\\test\\1.txt";
File file = new File(dir);
Reader input = new FileReader(file);
char c[] = new char[1024];
//一次性读取,读取到数组
int len = input.read(c);
// 打印读取到的内容,读取c数组中的数据,从0开始读
System.out.println("内容为:"+ new String(c,0,len));
input.close();
}
}
public void write(int c) throws IOException
直接将int型数据作为参数的话,是不会写?数字的,?是现将数字按照ascll码表转换为相应的字符,然后写?
public void write(String str) throws IOException
要想实现数字和中?的写?,必须要?String为参数的Write
public abstract void write(char cbuf[], int off, int len) throws IOException;
将cbuf字符数组的?部分写?到流中,但能不能写len个字符取决于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则会实现对?件的覆盖
public class WriterTest {
public static void main(String [] ags)throws Exception{
test1();
test2();
}
public static void test2() throws IOException {
String dir = "C:\\Users\\79466\\Desktop\\test\\2.txt";
// 传入参数,要写入文件的路径;传入参数true表示追加数据
FileWriter writer = new FileWriter(dir,true);
writer.write(23567);
writer.write(28404);
writer.write(35838);
writer.write(22530);
writer.write("23567");
writer.flush();
writer.close();
}
public static void test1() throws IOException {
String dir = "C:\\Users\\79466\\Desktop\\test\\2.txt";
Writer writer = new FileWriter(dir);
writer.write(23567);
writer.write(28404);
writer.write(35838);
writer.write(22530);
writer.write("23567");
writer.flush();
writer.close();
}
}
当BufferedReader在读取?本?件时,会先尽量从?件中读?字符数据并放满缓冲区,?之后若使?read()?法,会先从缓冲区中进?读取。如果缓冲区数据不?,才会再从?件中读取
# 构造函数
// 创建?个使?指定??输?缓冲区的缓冲字符输?流
BufferedReader(Reader in)
// 创建?个使?指定??输?缓冲区的缓冲字符输?流,指定大小
BufferedReader(Reader in, int sz)
boolean ready()
判断此流是否已准备好被读取,依赖其他流,所以?般需要做判断
int read()
读取单个字符
int read(char[] cbuf, int off, int len)
读取?部分字符到数组??,从数组下标off处放置length?度的字符
String readLine()
读取?整??本?,返回?整?字符串,如果读到?尾了就返回null,注意返回的??字符中不包含换?符
void close()
关闭流释放资源
public class BufferReaderTest {
public static void main(String [] args) throws Exception {
test1("C:\\Users\\79466\\Desktop\\test\\1.txt");
test2("C:\\Users\\79466\\Desktop\\test\\1.txt");
}
public static void test1(String path) throws Exception {
// 传入参数,要读取的文件路径
BufferedReader reader = new BufferedReader(new FileReader(path));
if(!reader.ready()){
System.out.println("文件流暂时无法读取");
return;
}
int size;
char[] cbuf = new char[1024];
// 将数据读取到数组中,读到cbuf数组,从0开始读
while ( (size = reader.read(cbuf,0,cbuf.length)) != -1){
System.out.println(new String(cbuf,0,size));
}
reader.close();
}
public static void test2(String path) throws Exception {
// 传入参数,要读取的文件路径
BufferedReader reader = new BufferedReader(new FileReader(path));
if(!reader.ready()){
System.out.println("文件流暂时无法读取");
return;
}
String str = "";
// 读取1行
while ( (str = reader.readLine()) !=null){
System.out.println(str);
}
reader.close();
}
}
写?的数据并不会先输出到?的地,?是先存储?缓冲区中。如果缓冲区中的数据满了,才会?次对?的地进?写出
# 构造函数
BufferedWriter(Writer out)
BufferedWriter(Writer out, int sz)
# 常用api
void write(int c)
写??个字符
void write(char[] cbuf, int off, int len)
写?字符数组的?部分,通过off和len控制。
void write(String s, int off, int len)
写?字符数组的?部分,通过off和len控制。
void newLine()
写如?个换?符合
void close()
关闭输?流并释放与该流关联的系统资源
void flush()
write是写到缓冲区中,可以认为是内存中,当缓冲区满时系统会?动将缓冲区的内容写??件,但是?般还有?部分有可能会留在内存这个缓冲区中, 所以需要调?flush空缓冲区数据
public class BufferedWriterTest {
public static void main(String [] args)throws Exception{
test1("C:\\Users\\79466\\Desktop\\test\\8.txt");
}
public static void test1(String path)throws Exception{
// 传入参数要写入的文件路径
BufferedWriter writer = new BufferedWriter(new FileWriter(path));
//写入一个字符
char ch = '小';
writer.write(ch);
//写入一个字符数组
String other = "aaaaxdclass.net";
writer.write(other.toCharArray(),0,other.length());
// 写入换行符
writer.newLine();
writer.close();
}
}
InputStreamReader(继承Reader)
# 将字节流转换为字符流, 字节流通向字符流的桥梁,如果不指定字符集编码,则解码过程将使?平台默认的字符编码,如:UTF-8
# ?本?件存储是A编码,然后如果以B编码进?读取则会乱码
# 构造函数
//使?系统默认编码集
public InputStreamReader(InputStream in)
//指定指定编码集创建对象
public InputStreamReader(InputStream in, String charsetName)
# api
int read()
读取单个字符
int read(char[] cbuf, int off, int len)
读取?部分字符到数组??,从数组下标off处放置length?度的字符
int read(char []cbuf)
将读取到的字符存到数组中,返回读取的字符数
void close()
关闭流释放资源
public class InputStreamReaderTest {
public static void main(String [] args)throws Exception{
test1("C:\\Users\\79466\\Desktop\\test\\8.txt");
}
public static void test1(String path) throws Exception{
//读取字节流
InputStream in = new FileInputStream(path);
// 将字节流转换为字符六
InputStreamReader isr = new InputStreamReader(in);
// 传入字符流
BufferedReader reader = new BufferedReader(isr);
String line;
// 读取1行
while ((line = reader.readLine())!= null){
System.out.println(line);
}
isr.close();
reader.close();
}
}
OutputStreamWriter(继承writer)
# 将字符流转换为字节流(看源码解释), 字符流通向字节流的桥梁,如果不指定字符集编码,则编码过程将使?平台默认的字符编码,如:GBK
# 构造函数
//使?系统默认编码集
public OutputStreamWriter(OutputStream out)
//指定指定编码集创建对象
public OutputStreamWriter(OutputStream out, String charsetName)
# api
void write(int c)
写??个字符
void write(char[] cbuf, int off, int len)
写?字符数组的?部分,通过off和len控制
void write(String s, int off, int len)
写?字符数组的?部分,通过off和len控制
void newLine()
写如?个换?符合
void close()
关闭输?流并释放与该流关联的系统资源
void flush()
write是写到缓冲区中,可以认为是内存中,当缓冲区满时系统会?动将缓冲区的内容写??件,但是?般还有?部分有可能会留在内存这个缓冲区中, 所以需要调?flush空缓冲区数据
public class OutputStreamWriterTest {
public static void main(String [] args)throws Exception{
test1("C:\\Users\\79466\\Desktop\\test\\9.txt");
}
public static void test1(String path)throws Exception{
OutputStream out = new FileOutputStream(path);
// 将字符流转换为字节流,并指定编码
OutputStreamWriter osr = new OutputStreamWriter(out,"GBK");
BufferedWriter bufw = new BufferedWriter(osr);
String str = "欢迎来到课堂xdclass.net";
bufw.write(str);
bufw.newLine();
bufw.write("学习java课程");
bufw.flush();
bufw.close();
}
}
public class TryCatchTest {
// 方式1
public static void main(String [] args){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
FileInputStream fis = new FileInputStream("C:\\Users\\79466\\Desktop\\test\\1.txt");
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("C:\\Users\\79466\\Desktop\\test\\1111.txt");
bos = new BufferedOutputStream(fos);
int size;
byte [] buf =new byte[1024];
while ((size = bis.read(buf))!= -1){
bos.write(buf,0,size);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(bis != null){
try {
bis.close();
}catch (Exception e){
e.printStackTrace();
}finally {
if(bos!=null){
try{
bos.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
}
// 方式二
public static void test1(){
try( FileInputStream fis = new FileInputStream("C:\\Users\\79466\\Desktop\\test\\1.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("C:\\Users\\79466\\Desktop\\test\\1111.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
){
int size;
byte [] buf =new byte[1024];
while ((size = bis.read(buf))!= -1){
bos.write(buf,0,size);
}
}catch (Exception e){
e.printStackTrace();
}
}
}