본문 바로가기

Study/정보처리기사

JAVA Level 9

1~5. 다음 코드에 대한 출력 결과를 쓰시오.

 

1.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"));
        writer.write("Hello, World!");
        writer.close();
        
        BufferedReader reader = new BufferedReader(new FileReader("output.txt"));
        String line = reader.readLine();
        System.out.println(line);
        reader.close();
    }
}

 

2.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"));
        dos.writeInt(1234);
        dos.close();

        DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"));
        int num = dis.readInt();
        System.out.println(num);
        dis.close();
    }
}

 

3.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        PrintWriter writer = new PrintWriter(new FileWriter("numbers.txt"));
        for (int i = 1; i <= 5; i++) {
            writer.print(i * 2 + " ");
        }
        writer.close();

        BufferedReader reader = new BufferedReader(new FileReader("numbers.txt"));
        String line = reader.readLine();
        System.out.println(line);
        reader.close();
    }
}

 

4.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("example.txt");
        fw.write("Java I/O Example");
        fw.close();

        FileReader fr = new FileReader("example.txt");
        int ch;
        while ((ch = fr.read()) != -1) {
            System.out.print((char) ch);
        }
        fr.close();
    }
}

 

5.

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("hello.txt");
        fos.write("Hello, Java!".getBytes());
        fos.close();

        FileInputStream fis = new FileInputStream("hello.txt");
        int ch;
        while ((ch = fis.read()) != -1) {
            System.out.print((char) ch);
        }
        fis.close();
    }
}

 


 

정답
(드래그 시 정답이 보입니다.)

1. Hello, World!
2. 1234
3. 2 4 6 8 10
4. Java I/O Example
5. Hello, Java!

'Study > 정보처리기사' 카테고리의 다른 글

JAVA Level 10  (0) 2025.02.21
JAVA Level 8  (0) 2025.02.21
JAVA Level 7  (0) 2025.02.21
JAVA Level 6  (0) 2025.02.21
JAVA Level 5  (0) 2025.02.21