关于InputStream和OutputStream的输入输出方向的理解
InputStream输入类,首先需要读取的内容转化成输入流,再从它那里进行读取,先关联源;之后过程中关联目的,这样形成了流; 把要读取的内容输入到输入流,再从输入流进行读取,所以是read()
OutputStream输出类,首先需要与写入的目的地相关联,然后通过它进行写入,首先关联的是流的目的;之后的过程中再关联源,这样形成了流 把要输出的东西通过输出流输出到目的地,所以是write()
下面的例子就是将f中的内容读至byte数组中,在实例化的时候,先关联的是f,也就是读取的源,然后input.read(b),关联了目的,也就是数组b
File f =
new File(
"d:"+File.separator+
"test.txt");
InputStream input =
new FileInputStream(f);
byte b[] =
new byte[1024];
int len = input.read(b);
input.
close();
System.out.
println(
new String(b
,0,
len));
先关联的是目的,文件f,然后out.write(b),指明了源
File f =
new File(
"d:"+
File.separator+
"test.txt");
OutputStream
out =
new FileOutputStream(f);
String str =
"hello,world";
byte b[] = str.getBytes();
out.write(b);
out.close();