[Java] ์ž…์ถœ๋ ฅ ๐Ÿšช

soyeonยท2022๋…„ 7์›” 15์ผ
0
post-thumbnail

IO (input/output)

: ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ฒ˜๋ฆฌํ•œ๋‹ค. java.io package๋กœ ์ œ๊ณต๋œ๋‹ค.

Java IO๋Š” Stream์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. (Stream instance)
: Java์—์„œ ํŠน์ • ์žฅ์น˜์—์„œ data๋ฅผ ์ฝ๊ฑฐ๋‚˜ ํŠน์ • ์žฅ์น˜๋กœ data๋ฅผ ๋ณด๋‚ผ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋งค๊ฐœ ๊ฐ์ฒด

Object Stream์„ ํ†ตํ•ด์„œ ๊ฐ์ฒด๋„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ, ๋ชจ๋“  ๊ฐ์ฒด๊ฐ€ ๋‹ค ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค. ๋งŒ์•ฝ instance๋ฅผ ์ƒ์„ฑํ•œ clas๊ฐ€ Serializable interface๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์œผ๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค.

ํ‘œ์ค€ ์ž…๋ ฅ / ํ‘œ์ค€ ์ถœ๋ ฅ

  • ํ‘œ์ค€ ์ž…๋ ฅ
    : Keyboard. System.in์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค.

  • ํ‘œ์ค€ ์ถœ๋ ฅ
    : Monitor. System.out์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค.

Stream

Stream ๊ฐ์ฒด์˜ ํŠน์ง•

  • ๋‹จ๋ฐฉํ–ฅ
    : ๋ฐ์ดํ„ฐ๋ฅผ ๋‚ด๋ณด๋‚ด๋Š” ๊ด€, ๋ฐ›์•„๋“ค์ด๋Š” ๊ด€์ด ๋”ฐ๋กœ ์žˆ๋‹ค.
    Stream์„ ์ƒ์„ฑํ•  ๋•Œ Stream์˜ ์ข…๋ฅ˜, ๊ฒฝ๋กœ ๋ฐฉํ–ฅ์„ ๊ฒฐ์ •ํ•œ๋‹ค.
    ๋‹จ๋ฐฉํ–ฅ
  • FIFO ๊ตฌ์กฐ
    : ๋จผ์ € ๋“ค์–ด๊ฐ„ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋‚˜๊ฐ„๋‹ค.
    FIFO
  • ๊ฒฐํ•ฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
    : Stream์„ ์ด์šฉํ•ด์„œ ์‚ฌ์šฉํ•˜๊ธฐ ํŽธํ•œ Stream์„ ์ƒ์„ฑ ํ•ด๋‚ธ๋‹ค.
    ๊ฒฐํ•ฉ์ด ๊ฐ€๋Šฅ1
    ๊ฒฐํ•ฉ์ด ๊ฐ€๋Šฅ2

Stream ๊ตฌ๋ถ„

  • input / output
  • Byte / ๋ฌธ์ž Stream

Stream ์˜ˆ์ œ ์ฝ”๋“œ

  • ํ•œ ์ค„ ์ž…๋ ฅ ๋ฐ›๊ธฐ
package lecture0715;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class KeyboardInput {
	
	public static void main(String[] args) {
		System.out.println("ํ‚ค๋ณด๋“œ๋กœ ํ•œ ์ค„์„ ์ž…๋ ฅํ•˜์„ธ์š”!");
		
		// ํ‚ค๋ณด๋“œ(ํ‘œ์ค€ ์ž…๋ ฅ)๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ 1์ค„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค.
		// ์ด System.in ์ด๋ผ๋Š” ์ œ๊ณต๋œ Stream์€ ์‚ฌ์šฉํ•˜๊ธฐ ๋„ˆ๋ฌด ํž˜๋“ค๋‹ค.
		InputStreamReader isr = new InputStreamReader(System.in);
		BufferedReader br = new BufferedReader(isr);
		
		try {
			String input = br.readLine();
			System.out.println("์ž…๋ ฅ ๋ฐ›์€ ๋ฌธ์ž์—ด์€ : " + input);
		} catch (Exception e) {
			
		}
	}
}
  • ๋ฉ”๋ชจ์žฅ ๋งŒ๋“ค๊ธฐ
package lecture0715;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

// note pad๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž
// JavaFX๋ฅผ ์ด์šฉํ•œ๋‹ค.
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class MyNotepad extends Application {

	TextArea textarea;
	Button openBtn, saveBtn;
	File file;

	@Override
	public void start(Stage primaryStage) throws Exception {
		// ํ™”๋ฉด์„ ๊ตฌ์„ฑํ•˜๋Š” ์ „์ฒด ํŒ
		BorderPane root = new BorderPane();
		// BorderPane์˜ ๊ฐ€๋กœ ์„ธ๋กœ ๊ธธ์ด
		root.setPrefSize(700, 500);

		// TextArea ์ƒ์„ฑ
		textarea = new TextArea();
		root.setCenter(textarea);

		// ๋ฒ„ํŠผ ๋งŒ๋“ค๊ธฐ
		openBtn = new Button("ํŒŒ์ผ ์—ด๊ธฐ");
		openBtn.setPrefSize(150, 40);
		openBtn.setOnAction(e -> {
			// ์˜คํ”ˆํ•  ํŒŒ์ผ์„ ์ฐพ์•„์„œ ๊ทธ ๋‚ด์šฉ์„ textarea์— ์ถœ๋ ฅ
			textarea.clear();  // textarea์˜ ๋‚ด์šฉ์„ ๋‹ค ์ง€์šด๋‹ค. ์ƒˆ๋กœ์šด ๋‚ด์šฉ์„ ๋„ฃ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
			FileChooser fileChooser = new FileChooser();
			fileChooser.setTitle("์˜คํ”ˆํ•  ํŒŒ์ผ์„ ์„ ํƒํ•ด ์ฃผ์„ธ์š”");
			file = fileChooser.showOpenDialog(primaryStage);
			
			try {
				FileReader fr = new FileReader(file);
				BufferedReader br = new BufferedReader(fr);  // fr์ด ๋ถˆํŽธํ•ด์„œ ์กฐ๊ธˆ ๋” ์ข‹์€ ํ†ต๋กœ(stream)๋ฅผ ๋šซ๋Š”๋‹ค.
				String line = "";
				while((line = br.readLine()) != null) {
					textarea.appendText(line + "\n");
				}
				br.close();  // ํ†ต๋กœ ๋‹ซ๊ธฐ
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e2) {
				
			}		
		});
		
		// ์œ„์˜ ์ฝ”๋“œ๋ฅผ ํ’€์–ด์“ด ์ฝ”๋“œ
//		openBtn.setOnAction(new EventHandler<ActionEvent>() {
//			
//			@Override
//			public void handle(ActionEvent arg0) {
//				System.out.println("๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ์–ด์š”!");
//			}
//		});

		saveBtn = new Button("ํŒŒ์ผ ์ €์žฅ");
		saveBtn.setPrefSize(150, 40);
		saveBtn.setOnAction(e -> {
			String text = textarea.getText();
			
			try {
				FileWriter fw = new FileWriter(file);
				fw.write(text);
				fw.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			
		});

		FlowPane flowPane = new FlowPane();
		flowPane.setPadding(new Insets(10, 10, 10, 10));
		flowPane.setColumnHalignment(HPos.CENTER); // ๊ฐ€์šด๋ฐ ์ •๋ ฌ
		flowPane.setPrefSize(700, 40);
		flowPane.setHgap(10); // ๊ฐ„๊ฒฉ

		// flowPane์— ๋ฒ„ํŠผ์„ ๋ถ™์ธ๋‹ค.
		flowPane.getChildren().add(openBtn);
		flowPane.getChildren().add(saveBtn);

		root.setBottom(flowPane);

		// Scene ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
		Scene scene = new Scene(root);
		primaryStage.setScene(scene);
		primaryStage.setTitle("๋ฉ”๋ชจ์žฅ");
		primaryStage.show();
	}

	public static void main(String[] args) {
		launch(); // static method - ๋‚ด๋ถ€์ ์œผ๋กœ thread๊ฐ€ ์ƒ์„ฑ๋œ๋‹ค.
	}
}

0๊ฐœ์˜ ๋Œ“๊ธ€