[Java] System.out.print VS BufferedWriter

jipyo park·2022년 6월 17일
0

java

목록 보기
4/5

System.out.print()

public class TestClass {
	public static void main(String[] args) {
    	// 미리 인스턴스 생성할 필요없이 사용시에 호출을 하면 됨
    	System.out.print();
    }
}

BufferedWriter

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;

public class TestClass {
	public static void main(String[] args) throws IOException {
    	BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        writer.write("temp");
        writer.write("12345", 0, 3);
        writer.write('c');
        write.flush();
        write.close();
        /*
        출력 : 
        temp123c
        */
    }
}

BufferedWriter 사용시 IOException을 설정해주어야함

println도 똑같다 마지막에 줄바꿈을 해줄뿐

	void print(boolean)
	void print(char)
	void print(int)
	void print(long)   
	void print(float)   
	void print(double)   
	void print(char[])   
	void print(String)   
	void print(Object)

write() 매개변수

    // 출력도 character값으로 출력이 됨
    // ex) writer.write(65);	-> 출력 : A
	writer.write(int c);

	writer.write(String str);
    // off <= String 출력 길이 > len
	writer.write(String s, int off, int len);

	writer.write(char[] cbuf);
	writer.write(char[] cbuf, int off, int len);

예제 - print()

	// int
	System.out.print(100);
    // String
    System.out.print("temp");
    // double
    System.out.print(100.234);
    // Object
    System.out.println(new int[]{1, 2, 3});
    
    /*
    출력 :
    100temp100.234[I@34ce8af7
    
    Object 출력 시 출력되는 것은 객체의 주소이기 때문에 실행할 때 마다 값이 다르게 나음
    */

예제 - write()

        writer.write("temp");
        writer.write("12345", 0, 3);
        writer.write('c');
        write.flush();
        write.close();
        /*
        출력 : 
        temp123c
        */

속도 측면에서는 bufferedWriter가 더 좋다

7개의 댓글

comment-user-thumbnail
2023년 8월 3일

Once the decision for Pet euthanasia at home is made, the veterinarian and pet owner will schedule a suitable time for the procedure. This ensures that the pet owner has time to prepare emotionally and make any necessary arrangements.

답글 달기
comment-user-thumbnail
2023년 8월 8일

In a competitive marketplace, bulk photo editing service product photography can be a differentiating factor. Unique, captivating images set a brand apart, making it stand out in a sea of similar offerings.

답글 달기
comment-user-thumbnail
2023년 8월 10일

The education reaches far beyond the individual learner; it ripples through societies on https://taughtup.com/ blog, touching lives, transforming communities, and shaping nations.

답글 달기
comment-user-thumbnail
2023년 9월 6일

One of the remarkable aspects of cooking is its ability to bridge cultures and connect people. It's a window into the traditions, on https://cookingflavour.com/ blog stories, and histories of different communities around the world.

답글 달기
comment-user-thumbnail
2023년 9월 12일

Develop a marketing strategy that includes social media marketing, minimalist drop earrings email campaigns, content creation, and online advertising. Consistency and creativity are key.

답글 달기
comment-user-thumbnail
2023년 9월 13일

High-quality materials like marble and hardwood are often used to enhance the aesthetics and durability of these spaces brooksidemountainmistbb. The color schemes are carefully selected to evoke a sense of calm and serenity.

답글 달기
comment-user-thumbnail
2023년 9월 13일

High-quality materials like marble and hardwood are often used to enhance the aesthetics and durability of these spaces brooksidemountainmistbb. The color schemes are carefully selected to evoke a sense of calm and serenity.

답글 달기