Search
Duplicate

Input and Output

νƒœκ·Έ
μž…μΆœλ ₯
ꡬ뢄
코데카데미
Printing using System.out
μš©μ–΄ μ„€λͺ…
System 은 이미 μžλ°” μ•ˆμ— λ‚΄μž₯ λ˜μ–΄μžˆλŠ” ν΄λž˜μŠ€μ΄λ‹€.
out 은 System 클래슀 μ•ˆμ— μžˆλŠ” μΈμŠ€ν„΄μŠ€ ν˜Ήμ€ λ©”μ„œλ“œμ΄λ‹€.
PrintStream μœ ν˜• 쀑 ν•˜λ‚˜μ΄λ©°, μžλ°” ν”„λ‘œκ·Έλž¨μ—μ„œ κ²°κ³Όλ₯Ό λ³΄μ—¬μ£ΌλŠ” 역할을 ν•œλ‹€.
μ’…λ₯˜
System.out.println(): 좜λ ₯ ν›„ μ€„λ°”κΏˆμ„ ν•œλ‹€.
System.out.print(): 좜λ ₯ ν›„ μ€„λ°”κΏˆμ„ ν•˜μ§€ μ•ŠλŠ”λ‹€.
System.out.printf(): 좜λ ₯ ν›„ μ€„λ°”κΏˆμ„ ν•˜μ§€ μ•ŠλŠ”λ‹€.
syntex : System.out.printf(β€μΆœλ ₯ν•  λ‚΄μš©β€, β€œμ§€μ‹œμž(좜λ ₯κ°’)”);
μ§€μ‹œμž
ν˜•μ‹
μ§€μ‹œμž
ν˜•μ‹
%b
boolean
%f
μ†Œμˆ˜μ 
%d
int
%c
문자
%o
8 μ§„μˆ˜
%n
μ€„λ°”κΏˆ
%x or %X
16 μ§„μˆ˜
%e or %E
μ§€μˆ˜ ν‘œν˜„
%s
λ¬Έμžμ—΄
The Scanner Class
μš©μ–΄ μ„€λͺ…
Scanner 은 이미 μžλ°” μ•ˆμ— λ‚΄μž₯ λ˜μ–΄μžˆλŠ” ν΄λž˜μŠ€μ΄λ‹€.
μ‚¬μš©ν•  λ•ŒλŠ” Scanner class λ₯Ό import ν•΄μ€˜μ•Ό ν•œλ‹€.
import java.util.Scanner;
Scanner ν΄λž˜μŠ€λŠ” μ•„λž˜μ™€ 같이 μ„ μ–Έν•œλ‹€.
Scanner [input λͺ…] = new Scanner(System.in);
Scanner ν΄λž˜μŠ€λŠ” νŒŒμΌλ‘œλΆ€ν„° input 을 읽어낼 수 도 μžˆλ‹€.
Scanner λŠ” λ‹€μ–‘ν•œ νƒ€μž…μ˜ λ³€μˆ˜λ₯Ό 읽고, μ €μž₯ν•  수 μžˆλ‹€.
λ³€μˆ˜
μ½”λ“œ
int
int num = input.nextInt();
Double
double numDouble = input.nextDouble();
Byte
byte numByte = input.nextByte();
Boolean
boolean isTrue = input.nextBoolean();
Long
long numLong = intput.nextLong();
Short
short numShort = input.nexShort();
Scanner ν΄λž˜μŠ€λŠ” λ‹€μ–‘ν•œ κΈ°λŠ₯이 μžˆλ‹€.
μ½”λ“œ
κΈ°λŠ₯
input.hasNextLine()
이 ν•¨μˆ˜λŠ” μ •μ˜λœ μŠ€μΊλ„ˆμ˜ μž…λ ₯에 λ‹€λ₯Έ 쀄이 μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” bool 을 λ°˜ν™˜ν•œλ‹€.
input.hasNextInt()
이 ν•¨μˆ˜λŠ” μ •μ˜λœ μŠ€μΊλ„ˆμ˜ μž…λ ₯에 λ‹€λ₯Έ μ •μˆ˜κ°€ μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” bool 을 λ°˜ν™˜ν•œλ‹€.
input.useDelimiter(",")
이 ν•¨μˆ˜λŠ” μš°λ¦¬κ°€ μ‚¬μš©ν•˜κ³ μž ν•˜λŠ” delimiter λ₯Ό νŠΉμ •ν•˜λŠ” 것을 λ•λŠ”λ‹€.
num.toString()
이 ν•¨μˆ˜λŠ” λ‹€μ–‘ν•œ λ³€μˆ˜μ˜ νƒ€μž…μ„ String νƒ€μž…μœΌλ‘œ λ°”κΎΈλŠ” 데 μ‚¬μš©ν•œλ‹€.
delimiter
delimiter λŠ” 데이터λ₯Ό λ‚˜λˆ„κΈ° μœ„ν•΄ μ‚¬μš©ν•œλ‹€.
delimiter λŠ” 특히 ν”„λ‘œκ·Έλž¨μ΄ csv νŒŒμΌμ„ 읽어야 ν•  λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λœλ‹€.
FileInputStream
FileOutputStream κ³Ό FileInputStream 은 μžλ°”μ— λ‚΄μž₯λ˜μ–΄ μžˆλŠ” class 이며 μ™ΈλΆ€νŒŒμΌμ„ μ΄μš©ν•΄μ„œ μž‘μ—…μ„ ν•΄μ•Όν•  λ•Œ μœ μš©ν•˜κ²Œ μ‚¬μš©λœλ‹€. FileInputStream 은 파일의 데이터λ₯Ό ν”„λ‘œκ·Έλž¨μ— λ‚˜νƒ€λ‚΄μ€€λ‹€. 두 클래슀 λ‹€ λ°”μ΄νŠΈ ν˜•μ‹μœΌλ‘œ 읽고 μ“΄λ‹€. 이 ν΄λž˜μŠ€λ“€μ„ μ΄μš©ν•˜κΈ° μœ„ν•΄μ„œλŠ” λ°˜λ“œμ‹œ import java.io.*; λ₯Ό μž…λ ₯ν•΄μ•Όν•œλ‹€. λ˜ν•œ IOException 을 throws ν•΄μ€˜μ•Ό ν•œλ‹€.
νŒŒμΌμ„ μ½κΈ°μœ„ν•΄ ν”„λ‘œκ·Έλž¨μ€ FileInputStream 을 μ–΄λ–»κ²Œ μ‚¬μš©ν•˜λŠ”μ§€ μ•Œμ•„λ³΄μž.

Step 1: Declare your input stream.

μ„ μ–Έμ‹œ μ‚¬μš©λ˜λŠ” ꡬ문: FileInputStream input = new FileInputStream(filePath)
μœ„μ™€ 같이 μ„ μ–Έν•˜λ©΄ μ§€μ •λœ 파일 κ²½λ‘œμ—μ„œ νŒŒμΌμ„ μ°Ύμ•„λ‚Έλ‹€.
파일 경둜의 μ’…λ₯˜
μ ˆλŒ€κ²½λ‘œ
FileInputStream input = new FileInputStream("C:/SampleFolder/input.txt")
μƒλŒ€κ²½λ‘œ
FileInputStream input = new FileInputStream("../documents/SampleFolder/input.txt")
File νƒ€μž…μ˜ 객체λ₯Ό μƒμ„±ν•˜κ³  FileInputStream 으둜 pass ν•œλ‹€.
//Option 1: Pass file path/name directly to FileInputStream FileInputStream input1 = new FileInputStream("input.txt"); // Option 2: Use File object to pass file info to FileInputStream // Save file path that has been passed in by the user into a string variable. String fileName = args[0]; // Pass path to File object File inputFile = new File(fileName); // Pass File object to FileOutputSteam FileInputStream input2 = new FileInputStream(inputFile);
Java
볡사

Step 2: Read your file.

FileInputStream 은 λ°”μ΄νŠΈλ₯Ό μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— νŒŒμΌμ—μ„œ μ½μœΌλ €λŠ” 데이터λ₯Ό λ°”μ΄νŠΈλ‘œ λ°”κΏ”μ•Ό ν•œλ‹€. νŒŒμΌμ„ ν•œλ²ˆμ— ν•œ λ°”μ΄νŠΈμ”© 읽기둜 ν•œ 경우 이λ₯Ό int λ˜λŠ” char λ³€μˆ˜μ— μ €μž₯ν•˜λ„λ‘ 선택할 수 μžˆλ‹€. μ•„λž˜ μ½”λ“œμ—μ„œ ν™•μΈν•΄λ³΄μž
// Counter variable to loop through the file int i = 0; // A loop to access each character while((i = inputFile.read()) != -1) { // Printing each character as it's reached System.out.print((char)i); }
Java
볡사

Step 3: Close the file.

close() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄μ„œ νŒŒμΌμ„ λ‹«λŠ”λ‹€.
FileOutputStream

Step 1: Declare your output stream.

FileOutStream output = new FileOutputStream(”output.txt”) ꡬ문은 μžλ°” ν”„λ‘œκ·Έλž¨μ΄ μ €μž₯λ˜μ–΄ μžˆλŠ” 폴더와 같은 곳에 output.txt νŒŒμΌμ„ μƒˆλ‘œ λ§Œλ“€κ²ƒμ΄λ‹€. (μœ„μΉ˜λ₯Ό λ”°λ‘œ μ •ν• μˆ˜λ„ μžˆλ‹€) ν˜Ήμ€ FileInputStream μ—μ„œμ™€ 같이 객체λ₯Ό 생성할 μˆ˜λ„ μžˆλ‹€.
//Option 1: Pass file path/name directly to FileOutputStream FileOutputStream output1 = new FileOutputStream("output.txt"); // Option 2: Use File object to pass file info to FileOutputStream // Save file path that has been passed in by the user into a string variable. String fileName = args[0]; // Pass path to File object File outputFile = new File(fileName); // Pass File object to FileOutputSteam FileOutputStream output2 = new FileOutputStream(outputFile);
Java
볡사

Step 2: Write to your file.

μ•„λž˜ μ˜ˆμ‹œλ₯Ό 톡해 ν™•μΈν•΄λ³΄μž
// Declare FileOutputSteam FileOutputStream output = new FileOutputStream("output.txt"); // Declare statement String statement = "Hello World!"; // Convert statement to bytes. byte[] statementBytes = statement.getBytes(); // Option 1: // Write all contents to file output.write(statementBytes); // You can do this more succinctly using output.write(statement.getBytes()); // Option 2: // If you want to write specific bytes to a file you may choose to use the following statement // output.write(byte[] bytes, int startingIndex, int endingIndex); output.write(statementBytes, 0, 4);
Java
볡사

Step 3: Close the file.

close() λ©”μ„œλ“œλ₯Ό μ΄μš©ν•΄μ„œ νŒŒμΌμ„ λ‹«λŠ”λ‹€.
IOExceptions
μ˜ˆμ™ΈλŠ” ν”„λ‘œκ·Έλž¨μ΄ μ‹€ν–‰ 쀑일 λ•Œ λ°œμƒν•  수 μžˆλŠ” 예츑 κ°€λŠ₯ν•œ 문제이며, 두가지 μœ ν˜•μ΄ μžˆλ‹€.
checked
checked excpetion 은 try-catch ꡬ문을 μ΄μš©ν•΄μ„œ λ‹€λ£°μˆ˜ μžˆλŠ” μ˜ˆμ™Έμ΄λ©°, ν”„λ‘œκ·Έλž¨μ΄ compiling ν•  λ•Œ λ°œκ²¬λœλ‹€.
unchecked
unchecked μ˜ˆμ™ΈλŠ” checked μ˜ˆμ™Έμ™€ 달리 runtime λ™μ•ˆμ— λ°œμƒν•œλ‹€.
IOException 은 ν”„λ‘œκ·Έλž¨μ˜ μž…μΆœλ ₯κ³Ό κ΄€λ ¨λœ μ˜ˆμ™Έμ΄λ©° checked μ˜ˆμ™Έλ‘œ λΆ„λ₯˜λœλ‹€.
IOExcetption 의 μ˜ˆλŠ” λ‹€μŒκ³Ό κ°™λ‹€.
(1) 파일 μ ‘κ·Ό μ‹œλ„ μ‹€νŒ¨
(2) 파일의 끝에 도달
(3) ν”„λ‘œκ·Έλž¨μ΄ μ ‘κ·Όν•˜λ €λŠ” νŒŒμΌμ„ 찾을 수 μ—†λ‹€.
(4) μž…/좜λ ₯ μž‘μ—…μ΄ 쀑단
μ•„λž˜ μ½”λ“œλŠ” IOException 을 μ΄μš©ν•΄ μ˜ˆμ™Έμ²˜λ¦¬λ₯Ό ν•˜λŠ” 두가지 방식을 보여쀀닀.
[Try-catch λ¬Έ μ‚¬μš©]
// Declare FileOutputSteam FileOutputStream output = new FileOutputStream("output.txt"); String statement = "Hello World!"; try { // Convert statement to bytes. byte[] statementBytes = statement.getBytes(); // Write all contents to file output.write(statementBytes); // Close file output.close(); } catch(IOException e) { // Handle exception System.out.println("There has been an IO exception"); System.out.println(e);
Java
볡사
[throws ν‚€μ›Œλ“œ μ‚¬μš©]
public class Sample { public static void main(String[] args) throws IOException { … } }
Java
볡사