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
볡μ¬