知识:
1.FileInputStream读文本数据基本步骤实现: 一次读一个字节和一次读多个字节
2.FileInputStreams输入流和FileOutputStream输出流综合练习,复制文件使用。
方式一实现: 一次读一个字节然后写一个字节
方式二实现: 一次读一个字节数组然后写一个字节数组
3.缓冲输出流和缓冲输入流
4.BufferedOutputStream缓冲输出流和BufferedInputStream缓冲输入流综合练习,复制文件使用。
方式一实现: 一次读一个字节然后写一个字节
方式二实现: 一次读一个字节数组然后写一个字节数组
一.FileInputStream字节流读数据(一次读一个字节)
1步骤:
(1).创建字节输入流对象。
如果文件存在,那么就不会报错.
如果文件不存在,那么就直接报错.
FileInputStream fis = new FileInputStream("bytestream\\a.txt");
(2).读数据
一次读取一个字节,返回值就是本次读到的那个字节数据.
也就是字符在码表中对应的那个数字.
如果我们想要看到的是字符数据,那么一定要强转成char
int read = fis.read();
(3).释放资源
fis.close();
2.实现案例:
public class OutputDemo7 {
public static void main(String[] args) throws IOException {
//如果文件存在,那么就不会报错.
//如果文件不存在,那么就直接报错.
FileInputStream fis = new FileInputStream("bytestream\\a.txt");
//一次读取一个字节,返回值就是本次读到的那个字节数据.
//也就是字符在码表中对应的那个数字.
//如果我们想要看到的是字符数据,那么一定要强转成char
int read = fis.read();
System.out.println((char)read);
//释放资源
fis.close();
}
}
二.FileInputStream字节流读多个数据
public class OutputDemo8 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("bytestream\\a.txt");
//读取文件中多个字节的数据
int b;
while ((b = fis.read())!=-1){
System.out.println((char) b);
}
fis.close();
}
}
三.FileInputStreams输入流和FileOutputStream输出流综合练习
需求:
把“C:\\itheima\\a.avi”复制到当前模块下
分析:
复制文件,其实就把文件的内容从一个文件中读取出来(数据源),然后写入到另一个文件中(目的地)
数据源:C:\\itheima\\a.avi --- 读数据 --- FileInputStream
目的地:模块名称\\copy.avi --- 写数据 --- FileOutputStream
代码实现:
//一次读一个字节然后写一个字节
public class OutputDemo9 {
public static void main(String[] args) throws IOException {
//创建了字节输入流,准备读数据.
FileInputStream fis = new FileInputStream("C:\\itheima\\a.avi");
//创建了字节输出流,准备写数据.
FileOutputStream fos = new FileOutputStream("bytestream\\a.avi");
int b;
while((b = fis.read())!=-1){
fos.write(b);
}
fis.close();
fos.close();
}
}
//一次读一个字节数组然后写一个字节数组
public class OutputDemo10 {
public static void main(String[] args) throws IOException {
//创建了字节输入流,准备读数据.
FileInputStream fis = new FileInputStream("C:\\itheima\\a.avi");
//创建了字节输出流,准备写数据.
FileOutputStream fos = new FileOutputStream("bytestream\\a.avi");
byte [] bytes = new byte[1024];
int len; //本次读到的有效字节个数 -- 这次读了几个字节.
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fis.close();
fos.close();
}
}
四.字节流缓冲流
1.字节缓冲流:
BufferOutputStream:缓冲输出流
BufferedInputStream:缓冲输入流
2.构造方法:
字节缓冲输出流:BufferedOutputStream(OutputStream out)
字节缓冲输入流:BufferedInputStream(InputStream in)
3.为什么构造方法需要的是字节流,而不是具体的文件或者路径呢?
字节缓冲流仅仅提供缓冲区,而真正的读写数据还得依靠基本的字节流对象进行操作
4.字节流缓冲流一次读一个字节然后写一个字节
//字节流缓冲流一次读一个字节然后写一个字节
public class OutputDemo10 {
public static void main(String[] args) throws FileNotFoundException {
//创建了字节输入流,准备读数据.
//在底层创建一个默认长度为8192的字节数组
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\itheima\\a.avi"));
//创建了字节输出流,准备写数据.
//在底层也创建一个默认长度为8192的字节数组
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bytestream\\a.avi"));
byte [] bytes = new byte[1024];
int len; //本次读到的有效字节个数 -- 这次读了几个字节.
while((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
//方法的底层会把字节流关闭
bis.close();
bos.close();
}
}
5.缓冲流结合数组进行文件拷贝
//缓冲流结合数组进行文件拷贝
public class OutputDemo10 {
public static void main(String[] args) throws IOException {
//创建了字节输入流,准备读数据.
//在底层创建一个默认长度为8192的字节数组
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\itheima\\a.avi"));
//创建了字节输出流,准备写数据.
//在底层也创建一个默认长度为8192的字节数组
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bytestream\\a.avi"));
byte [] bytes = new byte[1024];
int len;
while((len = bis.read(bytes))!=-1){
bos.write(bytes,0,len);
}
//方法的底层会把字节流关闭
bis.close();
bos.close();
}
}
五.更新精彩内容
http://www.gxcode.top/code