一般来说,IO流的分类是按照数据类型来分的
字节流抽象基类
FileOutputStream:文件输出流用于将数据写入File
使用字节输出流写数据的步骤:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("myBytesStream\\fos.txt");
/*
* 做了三件事情:
* A:调用系统功能创建了文件
* B:创建了字节流输出对象
* C:让字节输出流对象指向创建好的文件
* */
fos.write(97);
fos.write(57);
fos.write(55);
//释放资源
fos.close();
}
结果
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo02 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("myBytesStream\\fos.txt");
/*File file = new File("myBytesStream\\fos.txt");
FileOutputStream fos2 = new FileOutputStream(file);*/
//FileOutputStream fos3 = new FileOutputStream(new File("myBytesStream\\fos.txt"));
fos.write(97);
fos.write(98);
fos.write(99);
fos.write(100);
fos.write(101);
byte[] bys = {97,98,99,100,101};
fos.write(bys);
byte[] bytes = "feifei".getBytes();
fos.write(bytes);
fos.write(bys,1,3);
fos.close();
}
}
结果
字节流写数据如何实现换行呢?
写完数据后,加换行符
window:\r\n
linux:\n
mac:\r
字节流写数据如何实现追加写入呢?
finally:在异常处理时提供finally块来执行所有清除操作。比如说IO流中的释放资源
特点:被finally控制的语句一定会执行,除非JVM退出
try{
可能出现异常的代码;
}catch(异常类名 变量名){
异常的处理代码;
}finally{
执行所有清除操作;
}
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStremDemo03 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
//fos = new FileOutputStream("Z:myBytesStream\\fos.txt");
fos = new FileOutputStream("myBytesStream\\fos.txt");
fos.write("fei".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo04 {
public static void main(String[] args) {
try(FileOutputStream fos = new FileOutputStream("myBytesStream\\fos.txt");){
fos.write("feiling".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream:从文件系统中的文件获取输入字节
使用字节输入流读数据的步骤:
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo01 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("myBytesStream\\fos.txt");
int by;
while((by=fis.read())!=-1){
System.out.print((char)by);
}
fis.close();
}
}
//复制文本文件
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputStreamDemo02 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("myBytesStream\\fos.txt");
FileOutputStream fos = new FileOutputStream("myBytesStream\\fei\\fos.txt");
int by;
while ((by = fis.read()) != -1) {
fos.write(by);
}
fis.close();
fos.close();
}
}
使用字节输入流读数据的步骤:
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamDemo03 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("myBytesStream\\fos.txt");
byte[] bys = new byte[1024];
int len;
while ((len = fis.read(bys)) != -1) {
System.out.println(new String(bys, 0, len));
}
fis.close();
}
}
//复制图片
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileInputStreamDemo04 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("myBytesStream\\111.jpg");
FileOutputStream fos = new FileOutputStream("myBytesStream\\fei\\111.jpg");
byte[] bys = new byte[1024];
int len;
while((len= fis.read(bys))!=-1){
fos.write(bys,0,len);
}
fis.close();
fos.close();
}
}
字节缓冲流:
构造方法:
为什么构造方法需要的是字节流,而不是具体的文件或者路径呢?
import java.io.*;
public class BufferStreamDemo {
public static void main(String[] args) throws IOException {
/*FileOutputStream fos = new FileOutputStream("myBytesStream\\bos.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);*/
/*BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("myBytesStream\\bos.txt"));
bos.write("hello\r\nworld\r\n".getBytes());*/
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("myBytesStream\\bos.txt"));
byte[] bys = new byte[1024];
int len;
while ((len = bis.read(bys)) != -1) {
System.out.println(new String(bys,0,len));
}
/*bos.close();*/
bis.close();
}
}