浅谈write(byte[] b)和write(byte[] b,int off,int len)的区别
相信大家都会用着2个方法吧,但是真正理解他们的机制相信不多,现在我以代码来解释他们的机制及区别
举例新建一个类
package io;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] b = new byte[15];
byte bnum = 110;
for (int i = 0; i < b.length; i++) {
b[i] = bnum++;
}
ByteArrayInputStream bi = new ByteArrayInputStream(b);
FileOutputStream fos;
try {
System.out
.println("-------------用write(byte[] b)来写入文件---------------");
fos = new FileOutputStream(new File("C:\\文件1.txt"));
int i = 0;
byte[] buffer = new byte[10];
System.out.println("写入数据如下:");
while ((i = bi.read(buffer)) > 0) {
p(buffer);
fos.write(buffer);
}
System.out
.println("-------------用write(byte[] b,int off,int len)来写入文件---------------");
bi.reset();
fos = new FileOutputStream(new File("C:\\文件1.txt"));
i = 0;
buffer = new byte[10];
System.out.println("写入数据如下:");
while ((i = bi.read(buffer)) > 0) {
p(buffer);
fos.write(buffer, 0, i);
}
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void p(byte[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print((char) arr[i] + ",");
}
System.out.println();
}
}
打印结果如下:
-------------用write(byte[] b)来写入文件---------------
写入数据如下:
n,o,p,q,r,s,t,u,v,w,
x,y,z,{,|,s,t,u,v,w,
-------------用write(byte[] b,int off,int len)来写入文件---------------
写入数据如下:
n,o,p,q,r,s,t,u,v,w,
x,y,z,{,|,s,t,u,v,w,
其中写入到“文件1.txt”的内容为:nopqrstuvwxyz{|stuvw
其中写入到“文件1.txt”的内容为:nopqrstuvwxyz{|
相信大家都会用着2个方法吧,但是真正理解他们的机制相信不多,现在我以代码来解释他们的机制及区别
举例新建一个类
package io;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
byte[] b = new byte[15];
byte bnum = 110;
for (int i = 0; i < b.length; i++) {
b[i] = bnum++;
}
ByteArrayInputStream bi = new ByteArrayInputStream(b);
FileOutputStream fos;
try {
System.out
.println("-------------用write(byte[] b)来写入文件---------------");
fos = new FileOutputStream(new File("C:\\文件1.txt"));
int i = 0;
byte[] buffer = new byte[10];
System.out.println("写入数据如下:");
while ((i = bi.read(buffer)) > 0) {
p(buffer);
fos.write(buffer);
}
System.out
.println("-------------用write(byte[] b,int off,int len)来写入文件---------------");
bi.reset();
fos = new FileOutputStream(new File("C:\\文件1.txt"));
i = 0;
buffer = new byte[10];
System.out.println("写入数据如下:");
while ((i = bi.read(buffer)) > 0) {
p(buffer);
fos.write(buffer, 0, i);
}
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void p(byte[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print((char) arr[i] + ",");
}
System.out.println();
}
}
打印结果如下:
-------------用write(byte[] b)来写入文件---------------
写入数据如下:
n,o,p,q,r,s,t,u,v,w,
x,y,z,{,|,s,t,u,v,w,
-------------用write(byte[] b,int off,int len)来写入文件---------------
写入数据如下:
n,o,p,q,r,s,t,u,v,w,
x,y,z,{,|,s,t,u,v,w,
其中写入到“文件1.txt”的内容为:nopqrstuvwxyz{|stuvw
其中写入到“文件1.txt”的内容为:nopqrstuvwxyz{|