《2018年2月1日》【连续113天】
标题:Java流;
内容:
A.
public static void main(String[] args) {
System.out.println("Hello world!");
byte[] buffer =new byte[1024];
try {
int len =System.in.read(buffer);
String s =new String(buffer,0,len);
System.out.println("读到了"+len+"个字节");
System.out.println(s);
System.out.println("s的长度:"+s.length());
} catch (IOException e) {
e.printStackTrace();
}
其中,汉字占两个字节,普通字符是一个字节;
我在Mooc上观看老师的操作,输入n个字符,len是n+1,但我在eclipse运行上,len一直是n+2,debug时,发现如下:
以输入Aa为例:
字节数组的内容如下:
13,10在ASCII码表中查得为回车键,换行键;
问题猜想:老师用的是Mac os系统,它的回车换行是\r,
而我用的是windows系统,换行是\r\n。
打开macbook验证了一下:
B.文件读写:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
byte[] buffer =new byte[1024];
for(int i=0;i<buffer.length;i++) {
buffer[i]=(byte)i;
}
try {
FileOutputStream out =new FileOutputStream("a.dat");
out.write(buffer);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
建立了一个a.dat文件;
明日计划:学习;