代码如下,问题在注释中:
————————————————————————————————————————
import java.io.File;
import java.io.OutputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
public class OutputStreamDemo01{
public static void main(String args[])throws Exception{
File f = new File("i:"+File.separator+"code"+File.separator+"java"+File.separator+"OutputStream.txt");
OutputStream ops = new FileOutputStream(f);
String str = "hezi";
byte[] b = str.getBytes();
ops.write(b);
ops.close();
InputStream ips = new FileInputStream(f);
byte b2[] = new byte[(int)f.length()];//此处字符串的字节数是四,一个字母占一个字节,
//char类型一个字母为什么占的是两个
int num = ips.read(b2);
int i = ips.read();//读取的i=-1,而不是104?
ips.close();
System.out.println("content:" + i + "\t" + "num:" + num +
"\t" + "length:" + f.length());
}
}
|