最近在学习FileOutputStream,BufferedOutputStream这两个东西,这两个怎么能完美配合起来读取除了txt以外的文件,比如jpg,mp4之类的,哪位大哥能给个详细的操作流程和注释呢?谢谢
我存的资料 你看看吧
链接: https://pan.baidu.com/s/1dELvqal 密码: sfze
读啥都一样,其实就几类输入,输出流(inputStream,outputStream)字符字节流(reader,writer),后面的类就是对具体操作的一些封装,去看看java源码,或者看看别人写的博客,有很多
0.0好像写错地方了
BufferedInputStream in = new BufferedInputStream(new FileInputStream("文件地址"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("读到另一个文件地址"));
int i;
while((i=in.read())!=-1){
out.write(i);
}
out.flush();
out.close();
in.close();
FileOutputStream 字节输出流
BufferedOutputStream 带缓冲区的字节输出流
至于读jpg mp4 他们都是文件,感觉没什么区别
加我微信837768700
InputStream is = null ;
OutputStream out = null ;
try{
is = new FileInputStream("c://jdk.exe");
out = new FileOutputStream("d://copy//jdk.exe");
int length = -1 ;
byte[] bs = new byte[1024];
while(-1!=(length=is.read(bs))){
out.write(bs,0,length);
}
}catch(Exception e)e{
e.printStackTraceT();
}
public static void main(String[] args)
throws Exception{
//打开原始输入文件
FileInputStream in =
new FileInputStream(
"d:/TETRIS.zip");
//打开目标的输出文件
FileOutputStream out =
new FileOutputStream(
"d:/TETRIS_new.zip");
byte[] buf=new byte[1024*8];//1K byte
//从输入流in中读取尽可能多的byte填充
//到缓存 buf 中,返回读取个数 1024
//int n = in.read(buf);//1024
//int n = in.read(buf);//1024
//...
//n = in.read(buf); // 1 ~ 1024
//n = in.read(buf); // -1
int n;
while((n = in.read(buf))!=-1){
//将buf中从0开始的连续n个byte
//写到 文件流out中
out.write(buf, 0, n);
}
in.close();
out.close();
System.out.println("OK!");
}
import java.io.*;public class image {
public static void main(String []args)throws IOException{
Image[] array = new Image[10];
Image image = ImageIO.read(new File("c:\supermaket1.jpg"));//这里是你要读取的图像文件
array[0] = image;
ImageIO.write((RenderedImage) image, "png", new File("f:\test.txt"));//这里是你要写入的文件,如果不存在这个文件,那么系统会自动创建它
}
建议楼主搞清楚基础流和包装流,输出流和输入流,然后多使用 @chenjiexixi 这位写的就很清楚了:
BufferedInputStream in = new BufferedInputStream(new FileInputStream("文件地址"));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("读到另一个文件地址"));