I/O 스트림이란 데이터의 입출력을 처리하는 자바의 기능입니다. 스트림은 데이터를 어떤 원하는 형태로 걸러내고 가공하는 역할을 합니다. I/O 스트림은 데이터의 입출력을 담당하며, 이를 통해 파일이나 네트워크 등 다양한 데이터 소스와 상호작용할 수 있습니다. 스트림은 데이터의 소스에서 목적지까지 데이터를 이동시키는 일종의 통로로 볼 수 있습니다.
필터스트림은 노드 스트림에 연결하여 다른 처리를 도와주는 스트림입니다. 이는 데이터를 저장소로부터 하나씩 가져오면 노드 스트림을 통과하게 되며, 이렇게 가져온 데이터는 순수 바이트로 바로 사용하기에는 문제가 있을 수 있습니다.
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
File inputFile = new File("a.jpeg");
File outputFile = new File("b.jpeg");
try (
FileInputStream in = new FileInputStream(inputFile);
FileOutputStream out = new FileOutputStream(outputFile)
) {
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} catch(IOException e) {
e.printStackTrace();
}
}
}
대상 파일: a.jpeg
사본 이름: b.jpeg
버퍼링은 데이터를 미리 로드하는 방식을 의미합니다. 이는 스트리밍을 더욱 원활하게 실행하는 데 도움이 됩니다 Buffered 필터스트림은 입력에 대한 버퍼링을 하고 mark() 메소드와 reset() 메소드를 지원하기 위한 기능을 제공해 줍니다. BufferedInputStream 객체가 생성될 때, 버퍼링을 위한 내부 배열 객체가 같이 생성됩니다. 이는 데이터를 더 효율적으로 처리할 수 있게 합니다.
문자스트림은 데이터를 문자(character)의 형태로 입출력하는 스트림입니다. 주로 텍스트 데이터를 다룰 때 사용되며, 문자 인코딩 및 문자 집합 처리가 자동으로 이루어져 텍스트 처리가 간편합니다. 문자스트림은 java.io 패키지의 클래스 중에 Reader와 Writer 클래스가 있습니다. Reader 클래스는 문자 스트림의 입력 기능을 제공하고, Writer 클래스는 문자 스트림의 출력 기능을 제공합니다.
import java.io.*;
public class ReadFile {
public static void main(String[] args) {
try {
File file = new File("phone.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class ConvertToUpper {
public static void main(String[] args) {
try {
File file = new File("system.ini");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line.toUpperCase());
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class AddLineNumber {
public static void main(String[] args) {
try {
File file = new File("system.ini");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
int lineNumber = 1;
while ((line = br.readLine()) != null) {
System.out.println(lineNumber + ": " + line);
lineNumber++;
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}