package cn.zhouzhou;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.management.RuntimeErrorException;
/*
* 一、流?
* 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。
* 即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,
* 方便更直观的进行数据操作。
*二、 字节流?
* 先读后写!
* 1.输出流-------程序到文件------OutputStream类!写
* 1.OutputStream 2.加上代表换行!【\r\n】
*
* 2.输入流-------文件到程序-------InputStream类!读
* a.方法 read() 返回值-1 利用while循环 读取文件!int len=0; 返回值!
*
* b.读取字节数组
* byte b[]=new byte[1024];
* int len=0; while((len=a1.read(b))!=-1);
* System.out.println(new String(b,0,len));
* 三、文件复制?
* 方法:
* 1.用InputStream读取,OutputStream写!
* 2.创建数组缓冲!
* byte b[]=new byte[1024*10];
* int len=0;
* while((len=a1.read(b))!=-1);
* {a2.write(b,0,len)}
*
*
*/
public class OutputStreamDemo {
public static void main(String[] args) throws IOException {
run01();//1.输出流 写!OutputStream write();
run02();//2.输入流 读!InputStream read();
run03();//3.读取字节数组 byte [] b=new byte[1024];
copy();// 4.简单的文件复制 异常抛出!
copy01();//5.利用 try catch解决异常
}
//"E:\\老师的代码\\day20\\视屏\\001.mp4" "d:\\game\\001.mp4"
private static void copy01() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("E:\\老师的代码\\day20\\视屏\\001.mp4");
fos = new FileOutputStream("d:\\game\\001.mp4");
int len = 0;
byte []b=new byte[1024*10000]; //复制速度很快!
while ((len = fis.read(b)) != -1)
{
fos.write(b,0,len);
}
}
catch (IOException ex)
{
System.out.println(ex);
throw new RuntimeException("文件复制失败");
}
finally
{
try
{
if (fos != null)
{
fos.close();
}
}
catch (IOException ex)
{
throw new RuntimeException("释放资源失败");
} finally
{
try
{
if (fis != null)
{
fis.close();
}
}
catch (IOException ex) {
throw new RuntimeException("释放资源失败");
}
}
}
}
private static void copy() throws IOException {
//想要复制 先读后写!
FileInputStream a1=null;
FileOutputStream a2=null;
a1=new FileInputStream("E:\\老师的代码\\day25\\视屏\\1.mp4");
a2=new FileOutputStream("d:\\game\\1.mp4");
//定义一个数组,缓冲![1024*10]就够了
byte []b=new byte[1024*10000]; //1024*10000 复制速度很快!我的计算机有些卡顿!
int len=0;
while ((len=a1.read(b))!=-1) {
a2.write(b,0,len);
}
a1.close();
a2.close();
}
private static void run03() throws IOException {
FileInputStream a1=new FileInputStream("d:\\game\\aaa.txt");
//创建字节数组, 一般都是1024 读取速度快!
byte [] b=new byte[1024];
int len=0;
while ((len=a1.read(b))!=-1) {
System.out.println(new String(b,0,len)); //new String(b,0,len);数组byte中 字节从0开始,到len 最后一个!
}
a1.close();
}
private static void run02()throws IOException {
FileInputStream a1=new FileInputStream("d:\\game\\hello.txt"); //读取文件aaa.txt.文本内容是中文 有时会有乱码!--->以后解决。
int len=0; //读取一个字节的方法 read() 返回值 int int len=0;实在接受read方法的返回值!
while ((len =a1.read())!=-1) { //a1.read() 赋值给len 当len等于-1时,循环结束!
System.out.print((char)len); //使用循环,读取文件,循环结束的条件 read()方法返回值为-1
}
a1.close(); //流 有始有终的!
}
public static void run01() throws IOException{
//创建文件! 在 内存中写入到硬盘! OutputStream();
FileOutputStream a1=new FileOutputStream("d:\\game\\aaa.txt");
//在文件aaa.TXT中写入东西。 添加97 在字节中是a。 这种写法一次只能添加一个,慢!
a1.write(97);
//创建字节数组 ,写的就快! 一次传递很多个字节!
byte []bytes={98,99,97};
//(bytes,0,1); 获取bytes数组中,角标从0 到2的元素!注意。取不到角标为2的元素! 有头无尾!
a1.write(bytes, 0, 3);
System.out.println("b是"+bytes[0]+" c是"+bytes[1]+" a是"+bytes[2]);
//写入 字符串。不能直接写入,需要先转换成bytes!
a1.write("你好\r\n".getBytes()); // \r\n 换行!
a1.write("世界!".getBytes());
a1.close(); //输出流打开后,需要关闭!
}
}