Java学习第二十天知识点(IO流)
1.流的类别
1).按照流向区分:
输入流/输出流
2).按照数据单位区分
a.字节流
b.字符流
3)按照层次区分
a.底层流
b.高级流(缓冲流)
2.字节流:InputStream/OutputStream
主要作用: 做文件的复制
子类:
1)FileInputStream©
(1).构造方法:
new FileInputStream(File)
new FileInputStream("相对路径")
(2)常用API:
int read() -> 读一个字节
注意:
a.读一个字节= 8位,读的是低8位,其他24位全补0,所以永远是正数
b.读到末尾返回-1
int read(byte[]) -> 读字节数组
2)FileOutputStream©
(1).构造方法:
new FileOutputStream(File)
new FileOutputStream(File,true) ->询问是否在原文件上进行追加内容,可以嵌套
new FileOutputStream("相对路径")
new FileOutputStream("相对路径",true)
(2).常用API:
void write(int i) -> 写入一个字节(写入低8位)
void write(byte[] bs) -> 写一个字节数组
void write(byte[], int off, int len) -> 写入一个字节数组,从字节数组的第几位下标开始,写入长度为多少
public class StreamDemo01 {
@Test
public void test0() throws IOException {
FileOutputStream fos = new FileOutputStream(new File("test0.txt"),true);
fos.write(65);
}
@Test
public void test01() throws IOException {
FileOutputStream fos = new FileOutputStream("test01.txt");
fos.write(0);
fos.write(65);
fos.write(97);
fos.write(98);
fos.write(99);
}
@Test
public void test1() throws IOException {
//FileInputStream fis = new FileInputStream("test01.txt");
File file = new File("test01.txt");
FileInputStream fis = new FileInputStream(file);
/*System.out.println(fis.read());
System.out.println(fis.read());
System.out.println(fis.read());
System.out.println(fis.read());
System.out.println(fis.read());*/
System.out.println(file.length());
/*int index=-2;
while (index!=-1){
index=fis.read();
System.out.println(index);
}*/
int len;
while ((len=fis.read())!=-1){
System.out.println(len);
}
}
}
缓冲字节流(高级流):
作用: 提高效率
子类/实现类:
1.缓冲字节输入流:BufferedInputStream
1).创建:
new BufferedInputStream(底层流)
2.缓冲字节输出流:BufferedOutputStream
1).创建:
new :BufferedOutputStream(底层流)
2)常用API:
void flush() - 手动刷新缓冲区
文件的复制并对缓冲流和底层流进行对比 :
public class FileCopyDemo02 {
@Test
public void test0() throws IOException {
long startTime=System.currentTimeMillis();
//读文件
FileInputStream fis = new FileInputStream("火柴人.gif");
//写文件
FileOutputStream fos = new FileOutputStream("火柴人_copy.gif");
int len;
while ((len=fis.read())!=-1){
fos.write(len);
}
long endtTime=System.currentTimeMillis();
System.out.println(endtTime-startTime);
}
/*运数组进行传输用*/
@Test
public void test1() throws IOException {
long startTime=System.currentTimeMillis();
//读文件
FileInputStream fis = new FileInputStream("火柴人.gif");
//写文件
FileOutputStream fos = new FileOutputStream("火柴人_copy.gif");
byte[] bytes=new byte[1024];
int len;
//len1 为1024
/*int len1=fis.read(bytes);
System.out.println(len1);*/
while ((len=fis.read(bytes))!=-1){
fos.write(bytes);
//fos.write(bytes,0,len);
}
long endtTime=System.currentTimeMillis();
System.out.println(endtTime-startTime);
}
@Test
public void test2() throws IOException {
long startTime=System.currentTimeMillis();
//读文件
/*FileInputStream fis = new FileInputStream("火柴人.gif");
BufferedInputStream bis = new BufferedInputStream(fis);*/
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("new.txt"));
//写文件
/*FileOutputStream fos = new FileOutputStream("火柴人_copy.gif");
BufferedOutputStream bos = new BufferedOutputStream(fos);*/
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("1.txt"));
int len;
//len1 为1024
/*int len1=fis.read(bytes);
System.out.println(len1);*/
while ((len=bis.read())!=-1){
bos.write(len);
}
long endtTime=System.currentTimeMillis();
bos.flush();
System.out.println(endtTime-startTime);
}
}
字符流:Reader/Writer
1.作用: 做文件的读写
2.子类/实现类:
1)字符输入流:InputStreamReader()
(1)创建:
a.InputStreamReader(底层流(即字节流)) - 默认字符集
底层流:可以决定文件是否进行追加
b.InputStreamReader(底层流,字符集) - 指定字符集
字符集:
UFT-8 1字符 = 3字节
GBK 1字符 = 2字节
(2)常用API
int read() - 读取单个字符。
注意: 读到末尾返回-1
2)字符输出流:OutputStreamWriter
(1)创建:
OutputStreamWriter(底层流) - 默认字符集
OutputStreamWriter(底层流,字符集) - 指定字符集
(2)常用API:
void flush() 刷新该流的缓冲。(需要及时刷新缓冲流,否者可能造成部分数据丢失)
void write(char[] cbuf, int off, int len) - 写入字符数组的某一部分。
void write(int c) -写入单个字符。
void write(String str, int off, int len) - 写入字符串的某一部分。
缓冲字符流(高级流/包装流):
1.缓冲字符输入流:BufferedReader
1)创建:
BufferedReader(底层字符流)
2)常用API:
String readLine() - 读一整行
主要:注意返回null,标记读到末尾
2.缓冲字符输出流:PrintWriter
1)创建:
PrintWriter(OutputStream out, boolean autoFlush)
作用:
a.OutputStream,字节输出流,可以指定是否进行追加
b.是否刷新缓冲区
PrintWriter(fileName, 字符集) -指定字符集
作用:
a.可以指定字符集
PrintWriter(Writer, boolean autoFlush)
作用:
a.Writer需要使用底层字符流进行构建
底层字符流可以指定字符集
b.底层字符流使用底层字节流进行构建
底层字节流可以指定是否进行追加
c.是否进行自动刷新缓冲区
2)常用API:
void print() - 写出内容,打印不换行
注意: 使用print()结束,必须手动调用刷新缓冲区
pw.flush()
void println() - 写出内容,打印并且换行
注意: 要设置了自动刷新缓冲区,只要遇到println()才会进行自动刷新
对应测试代码:
public class Demo01zifudc {
@Test
public void test0() throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("z.txt"));
osw.write(65);
osw.flush();
//osw.close();
}
@Test
public void test1() throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("z1.txt",true),"GBK");
osw.write(65);
osw.write("张三");
osw.flush();
//osw.close();
}
@Test
public void test2() throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("z,txt"),"GBK");
}
@Test
public void test3() throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("z1.txt",true),"UTF-8");
osw.write(65);
osw.write("李四");
osw.write("hello",2,3);
char[] ch={'h','e','l','l','o'};
osw.write('\n');
osw.write(ch,2,3);
osw.flush();
//osw.close();
InputStreamReader isr = new InputStreamReader(new FileInputStream("z1.txt"));
int len;
while ((len=isr.read())!=-1){
System.out.println(len);
}
}
@Test
public void test4() throws IOException {
//添加 字符集 是否刷新
PrintWriter pw = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("cs.txt",true),"GBK"),true);
pw.print(1);
pw.println(2);
}
@Test
public void test5() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("cs.txt")));
//System.out.println(br.readLine());
String len;
while ((len= br.readLine())!=null){
System.out.println(len);
}
}
}