콘솔 입력
System.in.read()
InputStreamReader reader = ...
BufferedReader br = ..
Scanner ... // 제일 많이 사용!
콘솔 출력
System.our.println(...); // 엔터키 포함 출력
System.out.print(...);
System.our.printf(...); // 포맷 지정해서 출력
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void referInputStream() throws IOException {
System.out.println("== System.in ==");
System.out.print("입력 :");
int a = System.in.read() - '0';
System.out.println("a = " + a);
System.in.read(new byte[System.in.available()]);
System.out.println("== InputStreamReader ==");
InputStreamReader reader = new InputStreamReader(System.in);
char[] c = new char[3];
System.out.print("입력: ");
reader.read(c);
System.out.println(c);
System.out.println("== BufferedReader ==");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("입력: ");
String s1 = br.readLine();
System.out.println("s1 = " + s1);
}
public static void main(String[] args) throws IOException {
referInputStream();
System.out.println("== Scanner ==");
Scanner sc = new Scanner(System.in);
System.out.print("입력1: ");
System.out.println(sc.next()); - 데이터 하나 받아오기
System.out.print("입력2: ");
System.out.println(sc.nextInt());
sc.nextLine();
System.out.print("입력3: ");
System.out.println(sc.nextLine());
int num = Integer.parseInt("12345");
String str = Integer.toString(12345)
System.out.println("== 출력 ==");
System.out.println("Hello");
System.out.println("World!");
System.out.print("Hello ");
System.out.print("World!");
System.out.println();
System.out.printf("Hello ");
System.out.printf("World!");
System.out.println();
String s = "자바";
int number = 3;
System.out.println(s + "는 언어 선호도 " + number + "위 입니다.");
System.out.printf("%s는 언어 선호도 %d위 입니다.\n", s, number);
System.out.printf("%d\n", 10);
System.out.printf("%o\n", 10);
System.out.printf("%x\n", 10);
System.out.printf("%f\n", 5.2f);
System.out.printf("%c\n", 'A');
System.out.printf("%s\n", "안녕하세요");
System.out.printf("%-5d\n", 123);
System.out.printf("%5d\n", 1234);
System.out.printf("%5d\n", 12345);
System.out.printf("%.2f\n", 1.12645123f);
}
}
파일 출력
FileOutputStream ...
FileWriter ...
PrintWriter ...
파일 입력
FileInputStream ...
BufferedReader ...
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("./memo.txt");
String memo = "헤드 라인\n";
fw.write(memo);
memo = "1월 1일 날씨 맑음\n";
fw.close();
PrintWriter pw = new PrintWriter("./memo.txt");
memo = "헤드 라인";
pw.println(memo);
memo = "1월 1일 날씨 맑음";
pw.println(memo);
pw.close();
FileWriter fw2 = new FileWriter("./memo.txt", true);
memo = "1월 2일 날씨 완전 맑음\n";
fw2.write(memo);
fw2.close();
PrintWriter pw2 = new PrintWriter(new FileWriter("./memo.txt", true));
memo = "1월 3일 날씨 또 맑음!";
pw2.println(memo);
pw.close();
BufferedReader br = new BufferedReader(new FileReader("./memo.txt"));
while (true) {
String line = br.readLine();
if (line = null) {
break;
}
System.out.println(line);
}
br.close();
}
}
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Practice {
public static void main(String[] args) throws IOException {
String inputFile = "./JamesArthurGosling.txt";
String outputFile = "./JamesArthurGosling_edit.txt";
System.out.print("찾을 단어: ");
Scanner sc = new Scanner(System.in);
String find = sc.nextLine();
System.out.println("변경 단어: ");
String to = sc.nextLine();
BufferedReader br = new BufferedReader(new FileReader(inputFile));
FileWriter fw = new FileWriter(outputFile);
while (true) {
String line = br.readLine();
if (line == null) {
break;
}
String newLine = line.replace(fine, to);
fw.write(newLine + '\n');
}
br.close();
fw.close();
}
}