------<ahref="http://www.itheima.com" target="blank">Java培训、Android培训、iOS培训、.Net培训</a>、期待与您交流! -------
黑马程序员——22,字节流InputStream,OutputStream,字节流缓冲区技术,BufferedInputStream,BufferedOutputStream
/*
字节流的相关讲解:
InputStream(读操作),OutputStream(写操作)
在用法上和之前的字符流差不多
*/
import java.io.*;
class Ioliou13
{
public static void main(String[] args)throws IOException
{
// method();
method1();
}
public static void method()throws IOException
{
FileInputStream fis=new FileInputStream("f:\\yuyuyu.txt");
int i=0;
char ch='0';
while((i=fis.read())!=-1)
{
ch=(char)i;
soc(ch);
/*
由于这里使用的是字节流对象操作,
此时这里能看到的只有数字和字母
*/
}
fis.close();
}
public static void method1()throws IOException
{
FileInputStream fis=new FileInputStream("f:\\yuyuyu.txt");
int num=fis.available();//获得字节个数
soc("num="+num);
byte[] b=new byte[3];
int i=0;
//char ch =' ';
while((i=fis.read(b))!=-1)
{
//ch=(char)i;
soc(new String(b,0,b.length));
//这里就是按照字节数组b的字节打印出字符,所以不用强制类型转换
}
fis.close();
}
public static void method2()throws IOException
{
FileOutputStream fos=new FileOutputStream("f:\\yuyuyu.txt");
fos.write("kankan".getBytes());
//getBytes方法是String类自己的方法用于转变成字节
/*
字节写入流不存在flush方法,因为字节已经是最小操作对象,
字节写入流会直接写入文件。
*/
fos.close();
}
public static void soc(Object obj)
{
System.out.print(obj);
}
}
——————分割线——————
/*
复制图片:
选用字节流
思路:
1,字节读取流与目标相关联
2,字节写入流创建图片文件
3,循环读写
4,关闭资源
*/
import java.io.*;
class Ioliou14
{
public static void main(String[] args)
{
method2();
}
public static void method()
{
FileInputStream fis=null;
FileOutputStream fos=null;
try
{
fis=new FileInputStream("f:\\无标题23.png");
fos=new FileOutputStream("f:\\无标题23_copy.png");
byte[] b=new byte[1024];
int i=0;
while((i=fis.read(b))!=-1)
{
fos.write(b,0,i);
}
}
catch(IOException e)
{
throw new RuntimeException("复制图片出问题了");
}
finally
{
try
{
fis.close();
}
catch(IOException e)
{
throw new RuntimeException("fis.close();出问题了");
}
try
{
fos.close();
}
catch (IOException e2)
{
throw new RuntimeException("fos.close();出问题了");
}
}
}
public static void method2()
{
FileInputStream fis=null;
FileOutputStream fos=null;
try
{
fis=new FileInputStream("f:\\转码的视频\\死亡地带01.mp4");
fos=new FileOutputStream("f:\\死亡地带01_copy.mp4");
byte[] b=new byte[1024];
int i=0;
while((i=fis.read(b))!=-1)
{
fos.write(b,0,i);
}
}
catch(IOException e)
{
throw new RuntimeException("复制出问题了");
}
finally
{
try
{
fis.close();
}
catch (IOException e)
{
throw new RuntimeException("fis.close();出问题了");
}
try
{
fos.close();
}
catch (IOException e2)
{
throw new RuntimeException("fos.close();出问题");
}
}
}
public static void soc(Object obj)
{
System.out.println(obj);
}
}
——————分割线——————
/*
字节流也有自己的缓冲区技术:
BufferedInputStream
BufferedOutputStream
用法上和字符流差不多。
*/
import java.io.*;
class Ioliou15
{
public static void main(String[] args)throws IOException
{
long start=System.currentTimeMillis();
method();
long end=System.currentTimeMillis();
soc("time="+(end-start));
}
public static void method()throws IOException
{
FileInputStream fis=new FileInputStream("f:\\无标题23.png");
FileOutputStream fos=new FileOutputStream("f:\\无标题23copy.png");
BufferedInputStream bis=new BufferedInputStream(fis);
BufferedOutputStream bos=new BufferedOutputStream(fos);
int i=0;
while((i=bis.read())!=-1)
{
bos.write(i);
//这里就直接把i扔进去就可以,方便多了
}
bis.close();
bos.close();
}
public static void soc(Object obj)
{
System.out.println(obj);
}
}