OSI 7 계층이란, 네트워크 프로토콜이 통신하는 구조를 7개의 계층으로 분리하여 각 계층간 상호 작동하는 방식을 정해 놓은 것 이다.
소켓이란 네트워크상에서 서로 다른 호스트 사이의 통신을 위한 수단 입니다.
서버란 특정 서비스를 제공하는 '서비스 제공자'이며
클라이언트는 서비스를 요청하는 '서비스 소비자'이다.
IO 스트림은 1바이트 단위로 입출력을 실행한다.
문자 스트림은 2바이트(16bit)의 유니코드 문자를 입출력 하기 위한 스트림이며, 문자 단위로 입출력을 실행한다.
실행 결과(콘솔)
대상 파일: a.java
사본 이름: x:\b.java
public class CopyFile {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("대상 파일 >> ");
String original = sc.nextLine();
System.out.println("사본 이름 >> ");
String copied = sc.nextLine();
try (InputStream in = new FileInputStream(original); OutputStream out = new FileOutputStream(copied)) {
int data;
while (true) {
data = in.read();
if (data == -1)
break;
out.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("데이터 복사가 완료되었습니다.");
}
}
출력되어야 할 결과
공부에 있어서 돈이 꼭 필요한 것은 아니다.
Life is long if you know how to use it.
소스코드
public class HomeworkN6 {
public static void main(String[] args) {
String s1 = "공부에 있어서 돈이 꼭 필요한 것은 아니다.";
String s2 = "Life is long if you know how to use it.";
try(BufferedWriter bw = new BufferedWriter(new FileWriter("Test.txt"))){
bw.write(s1, 0, s1.length()); // s1을 0부터 s1.length까지 써라
bw.newLine(); // 개행(\n을 쓰지 않는 이유는, 운영체제마다 개행하는 이스케이프 시퀀스가 다를수 있기 때문이다.)
bw.write(s2, 0, s2.length());
}
catch(IOException e) {
e.printStackTrace();
}
}
}
String.txt 내용
공부에 있어서 돈이 꼭 필요한 것은 아니다.
Life is long if you know how to use it.
소스코드
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("불러올 파일명을 입력해주세요.");
String file = sc.next();
try(FileReader fr = new FileReader(file)){
int data;
while(true) {
data = fr.read();
if(data == -1)
break;
System.out.print((char)data);
}
}
catch(IOException e){
e.printStackTrace();
}
}
소스코드
public static void main(String[] args) {
try(Writer out = new FileWriter("data.txt")) { // data.txt에 넣음
for(int ch = (int)'A'; ch< (int)('Z'+1); ch++)
out.write(ch);
}
catch(IOException e) {
e.printStackTrace();
}