# 명품 자바 에센셜 연습문제 13장(이론)

- Author: @baealex
- Published: 2018-11-29
- Updated: 2018-11-29
- Source: http://blex.me/@baealex/%EB%AA%85%ED%92%88-%EC%9E%90%EB%B0%94-%EC%97%90%EC%84%BC%EC%85%9C-%EC%97%B0%EC%8A%B5%EB%AC%B8%EC%A0%9C-13%EC%9E%A5%EC%9D%B4%EB%A1%A0
- Tags: 자바에센셜

---

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(254, 254, 184); background-color: rgb(254, 254, 184); padding: 10px;">
<p>문제와 해답은 직접 작성한 것으로 오타가 있을 수도 있으며 해답역시 틀린 부분이 있을 수 있습니다. 틀린 부분에 대한 지적은 적극 수용하도록 하겠습니다. 문제를 먼저 풀어보시고 참고용으로만 봐주시길
        바랍니다.<br/></p>
</div>

#### 연습문제 13장

1. 스트림에 대한 설명으로 틀린 것은? 
 ① 스트림은 단방향이다. 
 <span style="color: rgb(255, 0, 0);">② 스트림은 다른 스트림과 연결될 수 없다.</span> 
 ③
 스트림은 선입선출 구조이다. 
 ④ 스트림은 버퍼를 가질 수 있다.

2. 다음 파일을 읽고자 할 때 바이트 스트림 클래스와 문자 스트림 클래스 중 어떤 것이 더 적합한지 설명하라. 
 (1) 동영상 파일(.avi) <span style="color: rgb(255, 0, 0);">바이트 스트림 클래스</span> 
 (2) 메모장으로 작성한 파일(.txt) <span style="color: rgb(255, 0, 0);">문자 스트림 클래스</span> 
 (3) 자바 클래스 파일(.class) <span style="color: rgb(255, 0, 0);">바이트 스트림 클래스</span> 
 (4) HTML 파일(.html) <span style="color: rgb(255, 0, 0);">문자 스트림 클래스</span> 
 <span style="color: rgb(47, 157, 39);">자바 클래스는 컴파일 된 상태이므로 바이트 스트림이며, HTML의 경우 </span> <span style="color: rgb(47, 157, 39);"> 모두 텍스트로 이루어</span> <span style="color: rgb(47, 157, 39);">진 파일이다.</span>

3. 다음 중 바이트 스트림 클래스가 아닌 것은? 
 ① OutputStream 
 <span style="color: rgb(255, 0, 0);">② FileReader</span> 
 ③
 BufferedInputStream 
 ④ FileInputStream

4. 동영상 파일을 읽으려고 한다. 가장 적합한 스트림 클래스는? 
 ① InputStream 
 <span style="color: rgb(255, 0, 0);">② FileInputStream</span> 
 ③
 FileReader 
 ④ InputStreamReader

5. c:\tmp\test.txt 파일이 다음과 같을 때, 다음 코드의 실행 결과는?

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); padding: 10px;">
<p>1234567890<br/></p>
</div>

```java
FileInputStream fin;
try {
    fin = new FileInputStream("c:\\tmp\\test.txt");
    int c;
    for(int i=0; i<5; i++) {
        c = fin.read();
        if(c != -1) System.out.print((char)c);
        else break;
    }
    fin.close();
} catch (IOException e) { }
```

<span style="color: rgb(47, 157, 39);">12345</span>

6. 다음은 파일 입력 스트림을 생성하는 코드이다. 이 코드는 어떤 경우에 예외가 발생하는가? 그리고 예외를 처리하기 위해 try-catch 블록으로 감싸라.

```java
FileInputStream fin;
FileReader fin = new FileReader("c:\\tmp\\sample.txt");
```

```java
try {
    FileReader fin = new FileReader("c:\\Temp\\sample.txt");
} catch (FileNotFoundException e) { }
```

7. 다음은 128바이트 크기의 버퍼를 이용하여 fin으로부터 한번에 128바이트씩 파일을 읽어 fout 스트림에 복사하는 프로그램이다. 빈칸에 적절한 코드를 삽입하라.

```java
FileIOututStream fout;
FileInputStream fin;
try {
    fout = new FileOutputStream("c:\\tmp\\test2.txt");
    fin = new FileInputStream("c:\\tmp\\test.txt");
    byte [] buf = __________; // 버퍼 할당
    while(true) {
        int n = fin.read(buf); // 버퍼 크기만큼 읽는다.
        _________________ // 읽은 버퍼만큼 쓴다.
        if(n < _______) // 버퍼 크기보다 적게 읽었다면
            break; // 파일 끝에 도달했으므로 복사 완료
    fin.close();
    fout.close();
} catch (IOException e) { }
```

6행: <span style="color: rgb(47, 157, 39);">new byte[128]</span> 
 9행: <span style="color: rgb(47, 157, 39);">fout.write(buf,
 0, n</span> <span style="color: rgb(47, 157, 39);">);</span> 
 10행: <span style="color: rgb(47, 157, 39);">buf.length</span>

8. File 클래스가 제공하는 기능이 아닌 것은? 
 <span style="color: rgb(255, 0, 0);">① 파일 읽기</span> 
 ② 파일 크기 
 ③ 파일의 부모
 디렉터리 명 
 ④ 파일 수정 시간

9. 다음 코드에 대해 답하라.

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); padding: 10px;">
<p>File f = <span style="color: rgb(255, 94, 0);">new</span> File(<span style="color: rgb(0, 85, 255);">"c:\\Program
            Files\\java\\jre8\\Welcome.html</span><span style="color: rgb(0, 85, 255);">"</span>);</p>
</div>

(1) f.isFile()의 리턴 값은? <span style="color: rgb(47, 157, 39);">true</span> 
 (2) f.getParent()의 리턴 값은? <span style="color: rgb(47, 157, 39);">"c:\Program
 Files\java\jre8</span> <span style="color: rgb(47, 157, 39);">"</span> 
 (3) f.getPath()의 리턴 값은? <span style="color: rgb(47, 157, 39);">"c:\Program
 Files\java\jre8\Welcome.html"</span> 
 (4) f.getName()의 리턴 값은? <span style="color: rgb(47, 157, 39);">"Welcome.html</span> <span style="color: rgb(47, 157, 39);">"</span> 
 (5) 다음 코드의 빈칸을 채워 문제와 동일한 파일 객체를 생성하라.

<div class="txc-textbox" style="border-style: solid; border-width: 1px; border-color: rgb(0, 0, 0); background-color: rgb(255, 255, 255); padding: 10px;">
<p>File f = <span style="color: rgb(255, 94, 0);">new</span> File(<span style="color: rgb(47, 157, 39);">"c:\Program
            Files\java\jre8</span><span style="color: rgb(47, 157, 39);">"</span>, <span style="color: rgb(0, 85, 255);">"</span><span style="color: rgb(0, 85, 255);">Welcome.html"</span>);</p>
</div>

10. 다음 코드의 빈칸에 적절한 코드를 삽입하라.

```java
File f = new File("c:\\tmp\\a.txt");
____________________ // (1) c:\tmp에 a.txt 파일이 존재하면 yes 출력
____________________ // (2) a.txt 파일 크기 출력
____________________ // (3) a.txt를 b.txt로 이름 변경
____________________ // (4) b.txt 삭제
```

2행: <span style="color: rgb(47, 157, 39);">if(f.exists()) System.out.println("yes");</span> 
 3행: <span style="color: rgb(47, 157, 39);">System.out.println(f.length());</span> 
 4행: <span style="color: rgb(47, 157, 39);">f.renameTo(new File("c:\\Temp\\b.txt"));</span> 
 5행: <span style="color: rgb(47, 157, 39);">new
 File("c:\\Temp\\b.txt</span> <span style="color: rgb(47, 157, 39);">").delete();</span>
