자바 입/출력 1byte 입/출력

자바 입/출력 1byte 입/출력


1byte 입/출력


자바에서 처리하는 입력과 출력은 스트림(Stream)에 의존
다시말해 모든 형태의 입력과 출력은 1byte의 흐름으로 이루어져 있다는것

1byte 출력


  1. 콘솔 출력용
    FileOutputStream fos = new FileOutputStream(FileDescriptor.out);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);
    dos.write…..

  2. 파일 출력용
    File file = new File(“파일명”);
    FileOutputStream fos = new FileOutoutStream(file);
    BufferedOutputStream bos = new BufferedOutoutStream(fos);
    DataOutputStream dos = new DataOutputStream(bos);
    dos.write….

  3. 네트워크 출력용
    Socket soc = new Socket(….);
    BufferedOutputStream bos = new BufferedOutputStream(soc.getOutputStream());
    DataOutputStream dos = new DataOutputStream(bos);
    dos.write….

1byte 입력


  1. 키보드 입력용
    FileInputStream fis = new FileInputStream(FileDescriptor.in);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    dis.read…..

  2. 파일 입력용
    File file = new File(“파일명”);
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStreamStream dis = new DataInputStream(bis);
    dis.read…..

  3. 네트워크 입력용
    Socket soc = new Socket(‘’’);
    BufferedInputStream dis = new DataInputStream(bis);
    dis.read….

ex 1)

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
import java.io.*;

public class Round16_Ex05 {
public static void main(String[] ar) {
File file = new File("C:\\java\\work\\abc.txt");
// 파일에 대한 객체를 생성한다
try {
FileOutputStream fos = new FileOutputStream(FileDescriptor.out);
// 콘솔에 대한 출력스트림을 생성한다.
FileOutputStream fos1 = new FileOutputStream(file);
// 파일에 대한 출력스트림을 생성한다
byte[] data = { 66, 68, 70, 72, (byte) '!' };
fos.write(data);
// 콘솔에 출력
fos1.write(data);
// C:\\java\\work\\abc.txt"); 에출력
} catch (FileNotFoundException fe) {
System.out.println("파일이 없을 때 이것출력");
System.exit(1);
} catch (IOException io) {
System.out.println("파일 입출력 에러");
System.exit(1);
}
System.out.println("실행끝");
}
}

ex 2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;

public class Round16_Ex06 {
public static void main(String[] ar) throws FileNotFoundException,
IOException {
FileInputStream fis = new FileInputStream(FileDescriptor.in);
// 콘솔에서 입력받는다
System.out.print("입력 = ");
byte by = fis.read();
// byte타입으로 읽어들인다
File file = new File("C:\\java\\work\\abc.txt");
// 파일로부터 입력객체를 생성한다.
FileInputStream fis = new FileInputStream(file);
// 파일객체의 입력스트림 생성한다.
byte[] by = new byte[65536];
int count = fis.read(by);
// fis의 입력스트림의 갯수를 샌다
for (int i = 0; i < count; i++) {
System.out.println(i + " : " + (char) by[i]);
// 출력
}
}
}

ex 3)

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
import java.io.*;

public class Round16_Ex07 {
public static void main(String[] ar) throws IOException {
File dir = new File("C:\\java\\work");
File file = new File(dir, "shs.txt");
// 파일 객체를 생성한다
FileOutputStream fos = new FileOutputStream(file);
// 파일객체를 파일 출력 스트림에 담는다
BufferedOutputStream bos = new BufferedOutputStream(fos); // 512Byte
// 파일 출력 스트림을 출력버퍼에 담는다 버퍼의 용량은 512Byte이다
// BufferedOutputStream bos1 = new BufferedOutputStream(fos, 1024);
// //1024Byte
// 파일 출력 스트림을 출력버퍼에 담는다 버퍼의 용량은 1024Byte 이다
DataOutputStream dos = new DataOutputStream(bos);
// 출력버퍼를 데이터 출력 스트림에 담는다
// 1byte 출력을 위한 객체
DataOutputStream dos1 = new DataOutputStream(new BufferedOutputStream(
new FileOutputStream(new File(new File("C:\\java\\work"),
"shs.txt"))));
// 위의 데이터 출력 스트림의 담는 과정을 좀더 짧게 표현한것
dos1.writeInt(23);
// int 형 숫자를 출력한다 . 이것을 출력하면 파일에는 4byte의 영역(int이기 때문에)을 확보하고
// 데이터가 표시된다 결과에는 알아볼수없는 문자열로 표시된다
dos1.writeDouble(12.345);
// double형 숫자를 출력한다, 역시 int형과 마찬가지로 8byte의 영역을 확보하고 데이터가 표시
dos1.writeBytes("ABCDEFG!!!!");
// 1byte씩 문자 형태로 출력한다 정상적으로 보인다.
dos1.close();
}
}

ex 4)

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
import java.io.*;

public class Round16_Ex08 {
public static void main(String[] ar) {
DataInputStream dis1 = null;
// 데이터 입력 스트림 선언
try {
dis1 = new DataInputStream(new BufferedInputStream(
new FileInputStream(new File(new File("C:\\java\\work"),
"shs.txt"))));
// 데이터 입력스트림을 선언해 파일에 입력스트림을 객체에 생성한다
} catch (FileNotFoundException fe) {
}
int a = 0;
double b = 0.0;
byte[] c = null;
try {
a = dis1.readInt();
// 데이터 입력 스트림의 객체에서 int의 값을 읽어온다
b = dis1.readDouble();
// 데이터 입력 스트림의 객체에서 Double값을 읽어온다
c = new Byte[10];
dis1.read(c);
// 데이터 입력 스트림의 객체에서 byte값의 배열[10]을 읽어온다
dis1.close();
// 데이터 입력 스트림을 닫는다
} catch (IOException ee) {
}
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + new String(c));
}
}

Share