public class ByteStreamMax {
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
//打印文件内容
public void inPut() throws IOException {
File file = new File("D:\\IdeaProjects\\javase\\src\\IOStream1");
fileInputStream = new FileInputStream(file);
byte[] bytes = new byte[1024];
int len = 0;
String str="";
while((len=fileInputStream.read(bytes))!=-1){
str = new String(bytes,"utf-8");
System.out.print(str);
}
System.out.println("\n");
fileInputStream.close();
}
//修改文件内容
public void outPut() throws IOException {
File file1 = new File("D:\\IdeaProjects\\javase\\src\\IOStream1");
File file2 = new File("D:\\IdeaProjects\\javase\\src\\IOStream2");
//读取file2的内容
fileInputStream = new FileInputStream(file2);
//将file2的内容写入到file1中
fileOutputStream = new FileOutputStream(file1);
byte[] bytes = new byte[1024];
int len = 0;
while((len=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,len);
}
fileInputStream.close();
fileOutputStream.flush();
fileOutputStream.close();
}
public static void main(String[] args) throws IOException {
ByteStreamMax byteStreamMax = new ByteStreamMax();
//打印文件内容
byteStreamMax.inPut();
//修改文件内容
byteStreamMax.outPut();
//再次打印文件内容,检测是否已修改
byteStreamMax.inPut();
}
}