-
Notifications
You must be signed in to change notification settings - Fork 0
/
Java IO
42 lines (34 loc) · 1.58 KB
/
Java IO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
java.io 包中的输入输出流
基于字节流的:
InputStream(声明了基于字节的输入方法) -> FileInputStream(文件输入) ObjectInputStream(对象输入)
OutputStream(声明了基于字节的输出方法) -> FileOutputStream(文件输出) ObjectOutputStream(对象输出)
基于字符流的:
Reader(声明了基于字符的输入方法) -> BufferedReader(带缓冲区读字符流) InputStreamReader(字节流与字符流的转换) FileReader(读写字符文件)
Writer(声明了基于字符的输出方法) -> BufferedWriter(带缓冲区写字符流) OutputStreamWriter(字节流与字符流的转换) FileWriter(读写字符文件)
Object -> InputStream OutputStream Reader Writer File RandomAccessFile(随机文件)
基于字符的输入输出:
import java.io.*;
public class Main {
public static void main(String[] args) {
String s;
BufferedReader ln = new BufferedReader(new InputStreamReader(System.in));
while((s = readLine()) != null) {
System.out.println("read: " + s);
}
}
}
文件输入输出:
import java.io.*;
public class Net {
public static void main(String[] args) throws java.io.IOException{
FileInputStream fin = new FileInputStream("C:\\Users\\Jianan Yuan\\Pictures\\a00af28fefbdfb1c0fddd2b02b1fe0c.jpg");
FileOutputStream fout = new FileOutputStream("123.jpg",true);
int len;
byte b[] = new byte[1024];
while((len = fin.read(b))!=-1) {
fout.write(b,0,len);
}
fin.close();
fout.close();
}
}