java笔记--IO流

IO流基础

表格输入流输出流
字节流InputStreamOutputStream
字符流ReaderWriter

字节流

所有都需要抛出异常

OutputStream:抽象类,表示输出字节的所有的超类。

FileOutputStream把内存中的数据写入到硬盘的文件当中。

 FileOutputStream(String name);//创建一个向具有名称的文件中写入数据的输出文件流
 FileOutputStream(File file);
 FileOutputStream(String name,boolean append);//true 追加数据
                                             //false 覆盖原文件
 FileOutputStream(File file,boolean append);//
  
 close();//关闭此输出流并释放
 flush();//刷新
 write(byte[] b);//将b.length字节从指定的字节数组写入此输出流
                //b数组中,第一个字节是正数,查询ASCII表
               //第一个字节是负数,第一个字节会和第二个字节组成一个中文显示,查询GBK
 write(byte[] b,int off,int len);//从指定的字节数组写入len字节,off开始
 write(int b);//将指定的字节输出流
 
 换行符号:
 	windows:\r\n
 	linux:/n
 	mac:/r

InputStream:抽象类,表示输入字节的所有的超类。

FileInputStream把硬盘文件中的数据,读到内存中使用。

FileInputStream(String name);//读取
FileInputStream(File file);

read();//从输入流中读取数据的一个字节并返回,读到文件的末尾返回-1
read(byte[] b);//从输入流中读取一定数量的字节,并将其存储在缓冲区数组b中
              //返回值int每次读取的有效字节个数
close();

BufferedOutputStream:字节缓冲输出流

BufferedOutputStream(OutputStream out);
BufferedOutputStream(OutputStream out,int size);

close();
flush();
write(byte[] b);//将b.length字节从指定的字节数组写入此输出流
write(byte[] b,int off, int len);
write(int b);

BufferedInputStream:字节缓冲输入流

BufferedInputStream(InputStream in);
BufferedInputStream(InputStream in,int size);

read();
read(byte[] b);
close();

字符流

FileReader:文件字符输入流
可以读取IDE默认编码格式(UTF-8)的文件,读取系统默认编码(中文GBK)会产生乱码

FileReader(String name);
FileReader(File file);

read(char[] cs);//一次读取多个字符

FileWriter:文件字符输出流

FileWriter(String name);//构造
FileWriter(File file);
FileWriter(String fileName,boolean append);
 FileWriter(File file,boolean append);

write(int c);
write(char[] sc);
write(char[] cs,int off,int len);
write(String str);
write(String str,int off,int len);
flush();
close();//关闭则不能继续写入数据

BufferedWriter:字符缓冲输出流

BufferedWriter(Writer out);
BufferedWriter(Writer out,int size);

write(int c);
write(char[] ch);
write(char[] ch,int off, int len);
write(String str);
write(String str,int off, int len);
flush();
closr();
newLine();//特有的成员方法,根据不同的操作系统,获取不同的行分割符

BufferedReader:字符缓冲流输入流

BufferedReader(Reader in);
BufferedReader(Reader in,int size);
read();
read(char[] ch);
close();
readLine();//特有的成员方法,读取一行数据//可以使用while循环,直至读取到null

Properties

Properties:可保存在流中,或从流中加载(双列集合,默认ket,value)

setProperty(String key,String value);//调用Hashtable的方法put
getProperty(String key);//通过key找到value值,此方法相当于Map集合中的get(key)方法
stringPropertyNames();//返回的是Set集合键集
store(OutputStream out,String comments);//OutputStream字节输出流,不可写入中文
store(Writer writer,String comments);//Writer字符输出流,可以写入中文
                                    //String comments注释,不能使用中文,默认是Unicode编码,一般使用空字符串""
load(InputStream inStream);
load(Reader reader);

字符通向字节

InputStreamReader:是字符流通向字节流的桥梁

InputStreamReader(InputStream in);
InputStreamReader(InputStream in,String charsetName);//指定编码标的名称要和文件相同,否则会出现乱码

read();
read(char ch);
close();

OutputStreamWriter:是字符流通向字节流的桥梁

OutputStreamWriter(OutputStream out);
OutputStreamWriter(OutputStream out,String charsetNamwe);//String charsetNamwe指定的编码表名称,不指定默认使用UTF-8

write(int c);
write(char[] ch);
write(char[] ch,int off,int len);
write(String str);
write(String str,int off,int len);
flush();
close();

对象的流

ObjectOutputStream:对象的序列化流,把对象以流的方式写入到文件中保存

ObjectOutputStream(OutputStream out);

writeObject(Object obj);

实现序列化和反序列化时,类必须实现Serializable接口,没有则会抛出NotSerialzableException异常

ObjectInputStream:对象的反序列化流,把文件中的对象,以流的方式读取出来

ObjectInputStream(InputStream in);
readObject();//特有的成员方法,从 ObjectInputStream读取该对象
//方法声明抛出了classNotFoundException,文件不存在时抛出

被static修饰的成员变量不能被序列化的,序列化的都是对象
transient瞬态关键字,不能被序列化
若是进行输出后,改动类中的关键字,需要固定版本号。

private static final long serialVersionUID=1L;

打印流

PrintStream:打印流
PrintStream特点:只负责数据的输出,不会抛出IOException
可以改变输出语句的目的地;输出语句默认在控制台输出

PrintStream(File file);
PrintStream(OutputStream out);
PrintStream(String fileName);
static void setOut(PrintStream out);

print(任意类型的值);//特有方法,写的数据照样输出97->97
println(任意类型的值并换行);//
close();
flush();
write(byte[] b);//将b.length字节从指定的字节数组写入此输出流
write(byte[] b,int off, int len);
write(int b);//97->a
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值