FileInputStream(),FileOutputStream() 和BufferedInputStream(),BufferedOutputStream()
package listdamo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class iofiledamo {
/*
* IO流的分类:
* 流向:
* 输入流 读取数据
* 输出流 写出数据
* 数据类型:
* 字节流
* 字节输入流 读取数据 InputStream
* 字节输出流 写出数据 OutputStream
* 字符流
* 字符输入流 读取数据 Reader
* 字符输出流 写出数据 Writer
*
* 注意:一般我们在探讨IO流的时候,如果没有明确说明按哪种分类来说,默认情况下是按照数据类型来分的。
*
* 需求:我要往一个文本文件中输入一句话:"hello,io"
*
* 分析:
* A:这个操作最好是采用字符流来做,但是呢,字符流是在字节流之后才出现的,所以,今天我先讲解字节流如何操作。
* B:由于我是要往文件中写一句话,所以我们要采用字节输出流。
*
* 通过上面的分析后我们知道要使用:OutputStream
* 但是通过查看API,我们发现该流对象是一个抽象类,不能实例化。
* 所以,我们要找一个具体的子类。
* 而我们要找的子类是什么名字的呢?这个时候,很简单,我们回想一下,我们是不是要往文件中写东西。
* 文件是哪个单词:File
* 然后用的是字节输出流,联起来就是:FileOutputStream
* 注意:每种基类的子类都是以父类名作为后缀名。
* XxxOutputStream
* XxxInputStream
* XxxReader
* XxxWriter
* 查看FileOutputStream的构造方法:
* FileOutputStream(File file)
* FileOutputStream(String name)
*
* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:写数据
* C:释放资源
*/
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
//ioFileTest();
//ioFileTest2();
//fileInputStream();
//copyFileTest();
bufferedInputStream();
long end = System.currentTimeMillis();
System.out.println("共耗时:" + (end - start) + "毫秒");
}
public static void ioFileTest()throws IOException
{
// 创建字节输出流对象
FileOutputStream fos = new FileOutputStream("fos.txt");// 多态
fos.write("hello.io".getBytes());
fos.write("\r\n".getBytes());
fos.write("java".getBytes());
//写一个字节数组
byte[] bys={97,98,99,100,101};
fos.write("\r\n".getBytes());
fos.write(bys);
fos.write("\r\n".getBytes());
fos.write(bys, 1, 3);
fos.write("\r\n".getBytes());
for(int x=0;x<10;x++){
fos.write(("hello"+x).getBytes());
fos.write("\r\n".getBytes());
}
//释放资源
fos.close();
}
/*
* 如何实现数据的换行?
* 为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。
* 如何实现呢?写入换行符号即可呗。
* 刚才我们看到了有写文本文件打开是可以的,通过windows自带的那个不行,为什么呢?
* 因为不同的系统针对不同的换行符号识别是不一样的?
* windows:\r\n
* linux:\n
* Mac:\r
* 而一些常见的个高级记事本,是可以识别任意换行符号的。
*
* 如何实现数据的追加写入?
* 用构造方法带第二个参数是true的情况即可
*/
public static void ioFileTest2()throws IOException
{
// 创建字节输出流对象
// FileOutputStream fos = new FileOutputStream("fos2.txt");
// 创建一个向具有指定 name 的文件中写入数据的输出文件流。如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
FileOutputStream fos = new FileOutputStream("fos2.txt", true);
// 写数据
for (int x = 0; x < 10; x++) {
fos.write(("hello" + x).getBytes());
fos.write("\r\n".getBytes());
}
// 释放资源
fos.close();
}
/*
* 字节输入流操作步骤:
* A:创建字节输入流对象
* B:调用read()方法读取数据,并把数据显示在控制台
* C:释放资源
*
* 读取数据的方式:
* A:int read():一次读取一个字节
* B:int read(byte[] b):一次读取一个字节数组
*/
public static void fileInputStream() throws IOException{
// 创建字节输入流对象
FileInputStream fis=new FileInputStream("listdamo4.java");
/* int by1 =0;
while((by1=fis.read())!=-1){
System.out.println((char) by1);
}*/
// 最终版代码
// 数组的长度一般是1024或者1024的整数倍
byte[]bys=new byte[1024];
int len=0;
while((len=fis.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
// 释放资源
fis.close();
}
/*
* 需求:把listdamo4.java内容复制到fos2.txt中
*
* 数据源:
* listdamo4.java -- 读取数据 -- FileInputStream
* 目的地:
* fos2.txt -- 写出数据 -- FileOutputStream
*/
public static void copyFileTest() throws IOException{
// 封装数据源
FileInputStream fis= new FileInputStream("listdamo4.java");
// 封装目的地
FileOutputStream fos=new FileOutputStream("fos2.txt");
// 复制数据
byte[] bys=new byte[1024];
int len =0;
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
// 释放资源
fos.close();
fis.close();
}
// 高效字节流一次读写一个字节数组:
public static void bufferedInputStream() throws IOException{
// 封装数据源
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"listdamo4.java"));
// 封装目的地
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("fos2.txt"));
// 复制数据
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
}
// 释放资源
bos.close();
bis.close();
}
// 高效字节流一次读写一个字节
public static void bufferedInputStream2() throws IOException{
// 封装数据源
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
"listdamo4.java"));
// 封装目的地
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("fos2.txt"));
int by=0;
while((by=bis.read())!=-1){
bos.write(by);
}
bos.close();
bis.close();
}
// 基本字节流一次读写一个字节数组
public static void fileInputStreamCopy() throws IOException{
FileInputStream fis =new FileInputStream("listdamo4.java");
FileOutputStream fos =new FileOutputStream("fos2.txt");
byte []bys =new byte[1024];
int len =0;
while((len=fis.read(bys))!=-1){
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
// 基本字节流一次读写一个字节
public static void fileInputStreamCopy2() throws IOException{
FileInputStream fis =new FileInputStream("listdamo4.java");
FileOutputStream fos =new FileOutputStream("fos2.txt");
int bys =0;
while((bys=fis.read())!=-1){
fos.write(bys);
}
fos.close();
fis.close();
}
//end class
}