io流概述:
io:是输入/输出(Input/output)
流:是一种抽象的概念,是对数据传输的总称,数据在设备间传输称为流,流的本质是数据传输
io流:就是用来处理设备间数据传输问题的,应用有:文件复制,文件上传,文件下载
io流分类:
按数据流向分:输入流:读数据;输出流:写数据
按数据类型分类:字节流:字节输入流,字节输出流;字符流:字符输入流,字符输出流
(用记事本打开文件,可以读懂就用字符流,如果读不懂就用字节流)
字节流写数据:
字节流抽象基类:
inputStream:这个抽象类是表示字节输入流发所有类超类
outputStream:这个抽象类是表示字节输出流发所有类超类
子类特点:子类名称都以其父类名作为后缀
FileOutputStream:文件输出流用于将数据写入File
方法:FileOutputStream(String name):创建文件输出流以指定的名称写入文件
public class demo {
public static void main(String[] args) throws IOException {
FileOutputStream fil = new FileOutputStream("D:\\chengyue.txt");
fil.write(97);
fil.write(98);
fil.write(99);
fil.close();
}
}
创建字节输出流对象做的三件事:1:调用系统功能创建了文件;2:创建了字节输出流对象;3:让字节输出流对象指向创建好的文件
void write (int b):方法:将指定的字节写入此文件输出流
void close();方法:关闭此文件输出流并释放与此流相关联的任何系统资源
字节流写数据的3种方式:
void write(int b):将指定的字节写入此文件输出流,一次写一个字节数据;
void write(byte[] b ):将b.length字节从指定的字节数组写入此文件输出流,一次写一个字节数组数据
void writer(byte[] b,int off,int len):将len字节从指定的字节数组开始。从偏移量off开始写入此文件输出流,一次写一个字节数组的部分数据
public class demo {
public static void main(String[] args) throws IOException {
FileOutputStream fil = new FileOutputStream("D:\\wkx\\chcengyue.txt");
fil.write(97);
fil.write(98);
fil.write(99);
byte [] bys ={97,98,99,100};
fil.write(bys);
byte[] bytes = "chengzi".getBytes();
fil.write(bytes,0,4);
fil.close();
File f = new File("D:\\wkx\\chceng.txt");
FileOutputStream fi = new FileOutputStream(f);
byte[] by = "橙子憨憨".getBytes();
fi.write(by);
fi.close();
}
}
字节流写数据的两个小问题:
字节流写数据如何实现换行:写完数据后,加换行符;Windows:\r\n;Linux:\n;mac:\r
字节流写数据如何实现追加写入呢:采用方法:public FileOutputStream(String name ,boolean append);创建文件输出流 以指定的名称写入文件,如果第二个参数为true,则字节将写入文件的末尾而不是开头
字节流写数据加异常处理:
用finally:在异常处理时提供finally块来执行所有的清除操作,比如说io流中的释放资源;特点:被finally控制的语句一定会执行,除非jvm退出
public class demo {
public static void main(String[] args) {
FileOutputStream fil = null;
try{
fil = new FileOutputStream("D:\\wkx\\cheng.txt");
fil.write("橙子".getBytes());
}catch (IOException e){
e.printStackTrace();
}finally {
if (fil!=null){
try{
fil.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
字节流读数据:
创建字节输入流对象;调用字节输入流对象的读数据方法;释放资源
一次读一个字节数据:
public class demo {
public static void main(String[] args) throws IOException {
FileInputStream fil = new FileInputStream("D:\\wkx\\chceng.txt");
int by;
while ((by=fil.read())!=-1) {
System.out.print((char)by);
}
}
}
字节流复制文本文件案例:等于-1,表示读取到最后一个字节
public class demo {
public static void main(String[] args) throws IOException {
FileInputStream fil = new FileInputStream("D:\\wkx\\xingyun\\橙子的故事.txt");
FileOutputStream fos = new FileOutputStream("D:\\wkx\\shangluo\\橙子的故事.txt");
int by;
while ((by=fil.read())!=-1) {
fos.write(by);
}
fos.close();
fil.close();
}
}
一次读一个字节数组数据:
public class demo {
public static void main(String[] args) throws IOException {
FileInputStream fil = new FileInputStream("D:\\wkx\\xingyun\\橙子的故事.txt");
byte [] bys = new byte[1024];
int len;
while((len = fil.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
fil.close();
}
}
字节流复制图片案例:
public class demo {
public static void main(String[] args) throws IOException {
FileInputStream fil = new FileInputStream("D:\\wkx\\xingyun\\hanhan.jpg");
FileOutputStream fos = new FileOutputStream("D:\\wkx\\shangluo\\hanhan.jpg");
byte [] bys = new byte[1024];
int len;
while((len = fil.read(bys))!=-1){
fos.write(bys,0,len);
}
fil.close();
fos.close();
}
}
字节缓冲流:
字节缓冲流:1:BufferedOutputStream:该类实现缓冲输出流,通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的 每个字节导致底层系统的调用; 2:BufferedinputStream创建BufferinputStream将创建一个内部缓冲区数组。当从流中读取或者跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
构造方法:
1:字节缓冲输出流:BufferedOutputStream(OutputStream out);
2:字节缓冲输入流:BufferedinputStream(inputStream in);
为什么构造方法要的是字节流,而不是具体文件或者路径呢? 因为字节缓冲流仅仅提供缓冲区,而真正的读写数据还得依靠基本的字节流对象进行操作
public class demo {
public static void main(String[] args) throws IOException {
BufferedInputStream fis = new BufferedInputStream(new FileInputStream("D:\\wkx\\xingyun\\橙子的故事.txt"));
int by;
while ((by = fis.read())!=-1){
System.out.print((char)by);
}
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys))!=-1){
System.out.println(new String (bys,0,len));
}
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream("D:\\wkx\\xingyun\\橙子的故事.txt"));
fos.write("橙子很可爱".getBytes());
fos.write("橙子最好看".getBytes());
fos.close();
fis.close();
}
}