Java中的IO流
一.IO流的基本介绍
java提供了标准的IO操作,即输入和输出;
- IO的作用是可以让我们的程序与外界进行数据交换,比如从网络读写数据,读取键盘数据,写入文件数据等等;
- 标准的IO根据将功能按照方向划分
- 输入:从外界到程序中的过程,这是"读"的过程;
- 输出:从程序发送至外界的过程,这是"写"的过程;
- 学习IO的重点:理解流链接操作,通过应用流链接,按照需要组合高级流与低级流,完成读写操作;
- 流分为:节点流(低级流),处理流(高级流)
- 节点流:实际链接程序与另一端的管道,负责在两端之间传送数据,注意:读写一定是建立在节点流基础上进行的.
- 处理流:不能独立存在,可以连接在其它流上,处理流自带某种对数据的加工操作,所以数据流经该流会对这些数据进行处理,这样可以简化我们对数据的处理操作;
- java.io.InputStream
- 所有字节输入流的父类,是一个抽象类,规定了所有字节输入流,必须具备的读取字节的方法;
- java.io.OutputStream
- 所有字节输出流的父类,是一个抽象类,规定了所有字节输出流,必须具备的写出字节的方法;
- 文件流:是一套低级流,作用是读写文件数据;
- 文件流和RandomAccessFile的对比:
- 1: 论功能是一致,都是用来读写文件数据;
- 2: RAF对于文件即可读也可写,但是文件流不行,文件输入流只用来读文件数据,文件输出流用来向文件中写入数据;
- 3: 流的特点是顺序读写操作,即:读写是不能回退的,RAF是随机读写操作,因为依靠指针位置进行读写,所以可以通过操作
- 指针对文件任意位置随意读写;
- 文件输出流支持两种模式:
- 1:覆盖写操作,即:若写出的文件有数据,则将原有数据全部删除,将本次通过写出的内容作为文件数据.
- FileOutputStream(String path)
- FileOutputStream(File file)
- 2:追加操作,若文件有数据则全部保留,从该文件末尾追加内容
- FileOutputStream(String path,boolean append)
- FileOutputStream(File file,boolean append)
二.文件流(字节流)
1.文件输出流
public class FileOutputStreamDemo1 {
public static void main(String[] args) throws IOException {
// //方式一:
// File file=new File("fis.txt");
// //创建文件输出流
// FileOutputStream fos=new FileOutputStream(file);
//方式二:
FileOutputStream bos=new FileOutputStream("fos.txt");
String str="而乏味二";
byte[] bs=str.getBytes("UTF-8");
bos.write(bs);
bs.clone();
}
}
2:追加操作,若文件有数据则全部保留,从该文件末尾追加内容
- FileOutputStream(String path,boolean append)
- FileOutputStream(File file,boolean append)
- append默认为false,当设置为true,则在文件末尾进行追加。
public class FileOutputStreamDemo2 {
public static void main(String[] args) throws IOException {
FileOutputStream bos=new FileOutputStream("fos.txt",true);
String str="大家大家";
byte[] bs=str.getBytes("UTF-8");
bos.write(bs);
System.out.println();
bs.clone();
}
}
上述程序运行结果是在fos.txt文件原有的内容上添加了“大家大家”;
2.文件输入流
文件输入流(读)
- FileInputStream(string path)
- FileInputStream(File file)
public class FileinputStreamDemo1 {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("fos.txt");
byte[] b=new byte[100];
int len=fis.read(b);
String str=new String(b,0,len,"UTF-8");
System.out.println(str);
fis.close();
}
}
三.转换流
转换流:
- java.io.InterStreamReader
- java.io.OutputStreamWriter
- java提供了其他高级字符流都有一个特点就是只能连接在其他字符流上,但是通常低级流都是字节流,这就导致了字符流不能直接搭配其他字节流使用,但是转换流除外,他们本身就是字符流,而它们可以连接字节流又可以连接字符流,所以在实际开发中,当我们使用高级的字符流时通常于字节流连接时需要使用转换流,他起到承上启下的作用.
- 提供的构造方法如下:
- OutputStreamWriter(outputstream out)
- OutputStreamWriter(outputstream out,string charset)
- InterStreamReader(inputstream in)
- InterStreamReader(inputstream in,string charset)
- charest:字符集
1.转换输出流
public class OutputStreamWriterDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("osw.txt",true);//文件输出流
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");//转换流
String str="刘永超啊";
osw.write(str);
System.out.println("写入完毕");
osw.close();
}
}
2.转换输入流
用转换流读取字符串;
public class InputStreamReaderDemo {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("osw.txt");
InputStreamReader isr=new InputStreamReader(fis,"utf-8");
// int len=-1;
// while((len=isr.read())!=-1){
// System.out.println((char)len);
// }
char[] c=new char[20];
int len=isr.read(c);
String str=new String(c,0,len);
System.out.println(str);
isr.close();
}
}
四.缓冲流
字节缓冲流:
- 字节缓冲流的缓冲区操作:
- 缓冲流实际上是进行块读写操作,无论我们缓冲流进行任何读写方式,最终都会缓冲流转换为块读写形式通过文件流进行实际操作,缓冲流内部维护了一个8k的字节流数组,用于块读写操作.
public class Buffer_flushDemo {
public static void main(String[] args) throws IOException {
//输出流
FileOutputStream fos=new FileOutputStream("yos.txt");
//字节缓冲流
//把字节输出流fos放到缓冲流
BufferedOutputStream pos=new BufferedOutputStream(fos);
String str="刘永擦";
byte[] bs=str.getBytes("UTF-8");
pos.write(bs);
//bos.flush();//强制写出
System.out.println("写入完毕");
pos.close();//在关闭的方法中自带强制写出方法flush();
}
}
五.字符流
字符流:
- Java将流按照读写单位划分为字节流和字符流。
- inputstream和outputstream是所有字节流的父类。
- java.io.reader和java.io.writer是字符流的父类。
- 字符流只是为了方便我们读写字符,底层还是读写字节,只是字节与字符的转换工作交给了转换流完成。
- PrintWriter:字符输出流:提供了构造方法如下:
- PrintWriter(String path)
- PrintWriter(Filefile)
- PrintWriter(Writer writer,Boolean autoflush)
- PrintWriter(String path,String charset)
- charset:编码格式
- writer:转换流
- autoflush:自动行刷新
1.字符输出流
1.用字符输出流将字符串写入文件
public class WriterDemo {
public static void main(String[] args) throws IOException {
//字符输出流
PrintWriter pw=new PrintWriter("pw.txt");
pw.println("啊哈哈");
System.out.println("写入完毕!");
pw.flush();
// pw.close();
}
}
2.用流连接的方式使用PrintWriter;
printWriter的构造方法中若有一个参数为流,那么就支持一个重载的构造方法,允许再传入一个boolean值类型的参数,若该值为true,则具有自动刷新功能,就是每当使用println()方法时写出一行字符串后会自动flush。
public class WriterDemo2 {
public static void main(String[] args) throws IOException {
//字节输出流(文件输出流)
FileOutputStream fos=new FileOutputStream("pw2.txt",true);
//转换输出流
OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
//字符缓冲输出流
BufferedWriter bw=new BufferedWriter(osw);
//字符输出流
PrintWriter pw=new PrintWriter(bw,true);
pw.println("刘少奇");
pw.println("马志愿");
System.out.println("写入完毕");
pw.close();
//规范书写
PrintWriter pd=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("pw3.txt",true),"UTF-8")),true);
pd.println("刘永超");
pd.close();
}
}
2.字符输入流
public class BufferedReaderDemo {
public static void main(String[] args) throws IOException {
//字节输入流
FileInputStream fis=new FileInputStream("pw3.txt");
//输入转换流
InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
//字符输入流
BufferedReader br=new BufferedReader(isr);
/*
* BufferedReader提供了String readline()方法
* 连续读取诺干个字符,直到读取到换行位置,然后将换行之前读取的字符以
* 一个字符串形式返回直到读到null,则表示读到末尾
*/
String line=null;
while((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
3.用字符流复制文件
public class PrintWriterReaderDemo {
public static void main(String[] args) throws IOException{
//字符输入流
BufferedReader br=new BufferedReader (
new InputStreamReader(
new FileInputStream("D:/CloudMusic/Against the Current - Legends Never Die.mp3"),"UTF-8"));
//字符输出流
PrintWriter pd=new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/CloudMusic/366152Against the Current - Legends Never Die.mp3",true),"UTF-8")),true);
String line=null;
while((line=br.readLine())!=null){
pd.println(line);
}
System.out.println("复制完毕!!");
}
}