作者:挤牙膏-- | 来源:互联网 | 2023-10-14 18:11
packagecom.demo1;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.Fi
package com.demo1;import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
//内存流
public class Test2 {public static void main(String[] args) {String path ="c:\\c.txt";String path1 ="c:\\d.txt";File file = new File(path);File file1 = new File(path1);FileInputStream stream = null;//读文件FileOutputStream fos = null;ByteArrayOutputStream bos = null;//写入内存try {bos = new ByteArrayOutputStream();fos= new FileOutputStream(file1);stream = new FileInputStream(file);byte []b = new byte[20];int len = 0;while((len=stream.read(b))!=-1){bos.write(b);//写入内存bos.flush();}//byte [] b2 = new byte[20];//int len2 =0;//while((len=bis.read(b2))!=-1){
// fos.write(b2,0,len2);
// fos.flush();//}bos.writeTo(fos);//从内存写入到文件bos.close();stream.close();fos.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}
}
}