捕捉异常
try{} catch{}
finally 关键字用来创建在 try 代码块后面执行的代码块。
无论是否发生异常,finally 代码块中的代码总会被执行。
流(stream)
-
一维单向的
-
文件流 //FlieOutputStream out = new FlieOutputStream(“文 件名.dat”);
FileInputStream //out.write(); //以单字符输入
FlieOutputStream //out.close();DataOutputStream //DataOutputStream out = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(“文件名.dat”)));
DataInputStream //out.writeInt(); //以2进制输入
//out.close();
文本输入输出
- 文本输出
PrintWriter out = new PrintWriter(
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("文件名.txt"))));
int i = 123456;
out.println(i);
out.close();
prinf("格式", ...);
format("格式", ...);
- 文本读取
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 使用 System.in 创建 BufferedReader
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(文件名.txt")));
String line = in.readline(); //读取整行
System.out.println(line);
getLineNumber(); //可以得到行号
- 汉字编码
BufferedReader in = new BufferedReader(
new InputStreamReader(
new FileInputStream(utf8.txt"), "utf8")));
输入与输出流
InputStream
OutputStream
byte[] buffer = new byte[1024];
try {
int len = System.in.read(buffer); //123汉字abc
String s = new String(buffer, 0, len);
System.out.println("读到了" + len + "字节"); //读到了11个字节
System.out.println(s); //123汉字abc
System.out.println("s的长度是" + s.length()); //s的长度是9
} catch (IOException o) {
}