PrintStream 클래스는 출력 스트림을 텍스트로 쉽게 출력하는 데 사용되는 클래스입니다. 주로 콘솔에 텍스트를 출력하는 데에 사용되며, System.out의 출력 스트림이 PrintStream의 인스턴스입니다.
PrintStream 클래스는 OutputStream의 서브 클래스이며, 텍스트 형식의 데이터를 출력하는 데 특화되어 있습니다. 기본적으로 텍스트 데이터를 출력할 때 자동으로 문자 인코딩이 처리되어, 문자열, 숫자, 객체 등을 간편하게 출력할 수 있습니다.
print 메서드들
- print(boolean b): boolean 값을 출력합니다.
- print(char c): 문자를 출력합니다.
- print(int i): 정수를 출력합니다.
- print(long l): long 타입의 정수를 출력합니다.
- print(float f): float 값을 출력합니다.
- print(double d): double 값을 출력합니다.
- print(String s): 문자열을 출력합니다.
- print(Object obj): 객체를 출력합니다.
println 메서드들
- println(): 개행 문자를 출력하여 줄을 바꿉니다.
- println(boolean x): boolean 값을 출력하고 줄을 바꿉니다.
- println(char x): 문자를 출력하고 줄을 바꿉니다.
- println(int x): 정수를 출력하고 줄을 바꿉니다.
- println(long x): long 타입의 정수를 출력하고 줄을 바꿉니다.
- println(float x): float 값을 출력하고 줄을 바꿉니다.
- println(double x): double 값을 출력하고 줄을 바꿉니다.
- println(String x): 문자열을 출력하고 줄을 바꿉니다.
- println(Object x): 객체를 출력하고 줄을 바꿉니다.
printf 메서드
- printf(String format, Object... args): 형식화된 문자열을 출력합니다. 지정된 형식에 따라 객체를 출력할 수 있습니다. (Systehttp://m.out.printf와 유사)
write 메서드들
write(int b): 바이트를 출력합니다.
write(byte[] b): 바이트 배열의 모든 바이트를 출력합니다.
write(byte[] b, int off, int len): 주어진 오프셋부터 길이만큼의 바이트를 출력합니다.
import java.io.*;
public class PrintStreamExample {
public static void main(String[] args) {
try {
// 파일에 출력하는 PrintStream 생성
PrintStream printStream = new PrintStream("output.txt");
// 데이터 출력
printStream.println("Hello, PrintStream!");
printStream.printf("Formatted: %d %s%n", 42, "text");
// 스트림 닫기
printStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
이 예제에서는 PrintStream을 사용하여 파일에 간단한 텍스트를 출력하고 있습니다.
With ChatGPT
'JAVA > 포스팅' 카테고리의 다른 글
자바 Writer (0) | 2023.11.26 |
---|---|
자바 Reader (0) | 2023.11.26 |
자바 SequenceInputStream (0) | 2023.11.26 |
자바 DataOutputStream (0) | 2023.11.26 |
자바 DataInputStream (0) | 2023.11.26 |