InputStream - 추상클래스
데이터를 읽는다.
이미지, 동영상 등의 데이터에 주로 사용
예외처리를 해야한다.
close()로 외부연결을 끝내야한다.
Mathod
read() - 1byte 씩 읽어온다. 속도가 느리다
read(byte[]) - Byte[] 씩 읽어온다. 속도가 빠르다
데이터를 읽는다.
이미지, 동영상 등의 데이터에 주로 사용
예외처리를 해야한다.
close()로 외부연결을 끝내야한다.
Mathod
read() - 1byte 씩 읽어온다. 속도가 느리다
read(byte[]) - Byte[] 씩 읽어온다. 속도가 빠르다
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
|
import java.io.FileInputStream;
import java.io.InputStream;
public class MyClass{
public static void main(String args[]){
InputStream is = null;
// InputStream을 쓰려면 try catch 문을 써야한다.
try{
// \\를 두번씩 써야 제대로 경로를 찾아갈수 있다.
is = new FileInputStream("C:\\aaa\\bbb.txt");
while(true){
int i = is.read();
System.out.println("데이터 : " + i);
// read() 는 데이터가 없을 때 반환값이 -1이다.
if(i == -1) break;
}
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
try{
if(is != null) is.close();
}catch(Exception e2){
System.out.println(e2.getMessage());
}
}
}
}
| cs |
OutputStream - 추상클래스
데이터를 쓴다.
이미지, 동영상 등의 데이터에 주로 사용
예외처리를 해야한다.
close()로 외부연결을 끝내야한다.
예외처리를 해야한다.
close()로 외부연결을 끝내야한다.
Mathod
write() - 1byte씩 데이터를 쓴다
write(byte[]) - Byte[]씩 데이터를 쓴다.
write(byte[], int, int) 데이터를 원하는 위치에서 원하는 만큼 쓸수 있다.
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
|
import java.io.FileOutputStream;
import java.io.OutputStream;
public class MyClass{
public static void main(String args[]){
OutputStream os = null;
try{
// 파일이 없으면 만든다.
os = new FileOutputStream("C:\\aaa\\ccc.txt");
String str = "아무말 대잔치";
byte[] bs = str.getBytes(); // 배열형 반환
os.write(bs);
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
try{
if(os != null) os.close();
}catch(Exception e2){
System.out.println(e2.getMessage());
}
}
}
}
| cs |
문자열에 대한 input output은 주로 Reader, Writer 클래스를 사용한다.
댓글
댓글 쓰기