[20220919] Test02_output

kangsunยท2022๋…„ 9์›” 20์ผ
0

Java

๋ชฉ๋ก ๋ณด๊ธฐ
19/44

๐Ÿ“Œ output ์ถœ๋ ฅ

๋ฉ”๋ชจ์žฅ ํŒŒ์ผ์— ์ถœ๋ ฅํ•˜๊ธฐ

์ถœ๋ ฅํŒŒ์ผ (sungjuk.txt)
โ†’ ์—†์œผ๋ฉด ํŒŒ์ผ์€ ์ƒ์„ฑ๋œ๋‹ค(create)
โ†’ ์žˆ์œผ๋ฉด ๋ฎ์–ด์“ฐ๊ธฐ(overwrite) ๋˜๋Š” ์ถ”๊ฐ€(append)


[์ถœ๋ ฅํŒŒ์ผ ๋‹ด๊ธฐ]

String filename="I:/java202207/sungjuk.txt";

  • ํ˜„์žฌ ๋‚ด ์ปดํ“จํ„ฐ์— I:/java202207/sungjuk.txt ํŒŒ์ผ์ด ์—†๋Š”๋ฐ, ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด I:/java202207/sungjuk.txt ์ด ๊ฒฝ๋กœ๋กœ ํŒŒ์ผ์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

[์ถœ๋ ฅํŒŒ์ผ ํด๋ž˜์Šค ์ƒ์„ฑ]

FileWriter fw=null;
- ๋ฌธ์ž์—ด์„ ํŒŒ์ผ์— ์ถœ๋ ฅํ•  ๋•Œ ์‚ฌ์šฉ
- FileWriter (String fileName, boolean append)

PrintWriter out=null;
- ๊ฐœ์ฒด์˜ ํ˜•์‹ํ™”๋œ ํ‘œํ˜„์„ ํ…์ŠคํŠธ ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์— ์ถœ๋ ฅ
- PrintWriter๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ํฐ ์ด์œ ์ค‘ ํ•˜๋‚˜์ธ print ๊ด€๋ จ ๋ฉ”์„œ๋“œ์ด๋‹ค. ํŠนํžˆ println ๋ฉ”์„œ๋“œ๋Š” ์ž๋™ ๊ฐœํ–‰์„ ์‹œ์ผœ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์กฐ๊ธˆ ๋” ์‰ฌ์šด ์ถœ๋ ฅ ์ฝ”๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค€๋‹ค. (print / println / printf)
- PrintWriterํด๋ž˜์Šค๋Š” ๋‹ค๋ฅธ ์ŠคํŠธ๋ฆผ๊ณผ ๋‹ค๋ฅด๊ฒŒ ๋ฐ”์ดํŠธ์ถœ๋ ฅ์ŠคํŠธ๋ฆผ๊ณผ ๋ฌธ์ž ์ถœ๋ ฅ ์ŠคํŠธ๋ฆผ์„ ๊ฐ€์ง€๊ณ  ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ• ์ˆ˜์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค.
- ์ž๋™ ํ”Œ๋Ÿฌ์‰ฌ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
- PrintWriterํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—๋Š” FileNotFoundException์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ˜๋“œ์‹œ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค.


fw=new FileWriter(filename, true);

  • run ํด๋ฆญ ํ•  ๋•Œ๋งˆ๋‹ค println ์•ˆ์— ์žˆ๋Š” ๊ฐ’์ด ์ถ”๊ฐ€๋จ.

๐Ÿ’ป Console
๋ฌด๊ถํ™”,95,90,100
ํ™๊ธธ๋™,100,100,100
๋ผ์ผ๋ฝ,90,95,35
๊ฐœ๋‚˜๋ฆฌ,85,70,75
์ง„๋‹ฌ๋ž˜,35,40,60

fw=new FileWriter(filename, false);

  • ์ถ”๊ฐ€๋œ ์ •๋ณด ์ง€์›Œ์ง€๊ณ  ๊ธฐ๋ณธ ๊ฐ’๋งŒ ๋‚จ๋Š”๋‹ค.

๐Ÿ’ป Console
๋ฌด๊ถํ™”,95,90,100
ํ™๊ธธ๋™,100,100,100
๋ผ์ผ๋ฝ,90,95,35
๊ฐœ๋‚˜๋ฆฌ,85,70,75
์ง„๋‹ฌ๋ž˜,35,40,60


๐Ÿ–ฅ ์ „์ฒด์ฝ”๋“œ

package oop0920;

import java.io.FileWriter;
import java.io.PrintWriter;

public class Test04_output {

	public static void main(String[] args) {
		// ๋ฉ”๋ชจ์žฅ ํŒŒ์ผ์— ์ถœ๋ ฅํ•˜๊ธฐ
		
		// ์ถœ๋ ฅํŒŒ์ผ (sungjuk.txt)
		// โ†’์—†์œผ๋ฉด ํŒŒ์ผ์€ ์ƒ์„ฑ๋œ๋‹ค(create)
		// โ†’์žˆ์œผ๋ฉด ๋ฎ์–ด์“ฐ๊ธฐ(overwrite) ๋˜๋Š” ์ถ”๊ฐ€(append)
		
		String filename="/Users/kangseonlee/Desktop/itwill/java202207/sungjuk.txt";
		// ํ˜„์žฌ ๋‚ด ์ปดํ“จํ„ฐ์— I:/java202207/sungjuk.txt ํŒŒ์ผ์ด ์—†๋Š”๋ฐ,
		// ์ด๊ฒƒ์„ ์‚ฌ์šฉํ•˜๋ฉด I:/java202207/sungjuk.txt ์ด ๊ฒฝ๋กœ๋กœ ํŒŒ์ผ์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ๋œ๋‹ค.

		
		FileWriter fw=null;
		//FileWriter (String fileName, boolean append)
		PrintWriter out=null;
		//PrintWriter (Writer out, boolean autiFlush)
		// textํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ ์ถœ๋ ฅํ• ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.
		// false ์ฃผ๋ฉด ํฐ์ผ๋‚จ.
		
		
		
		try {
			
			//true  : append๋ชจ๋“œ	   - ๋ฌธ์ž์—ด์ด ๊ณ„์† ์ถ”๊ฐ€๋จ.
			//false : overwrite๋ชจ๋“œ - ๊ธฐ์กด์˜ ์ •๋ณด ๋‹ค ์ง€์›Œ์ง€๊ณ  ๊ธฐ๋ณธ์ •๋ณด๋งŒ ๋‚จ์Œ.
			fw=new FileWriter(filename, true);
			
			//autoFlush : true ๋ฒ„ํผํด๋ฆฌ์–ด
			//
			out=new PrintWriter(fw, true);	//false์ฃผ๋ฉด ์•ˆ๋จ(๊ณต์‹์ž„)
			
			//oop0906.Test01_format.java์ฐธ์กฐ
//			out.printf()
//			printf = format์˜ ์•ฝ์ž (์ถœ๋ ฅํ•  ๋•Œ ์™ผ์ชฝ์ •๋ ฌ, ์˜ค๋ฅธ์ชฝ ์ •๋ ฌ, ์ˆซ์žํ˜•์จ๋ผ~ ๋“ฑ๋“ฑ ๋ช…๋ นํ•  ์ˆ˜ ์žˆ๋‹ค.)
			
			
			out.println("๋ฌด๊ถํ™”,95,90,100");
            out.println("ํ™๊ธธ๋™,100,100,100");
            out.println("๋ผ์ผ๋ฝ,90,95,35");
            out.println("๊ฐœ๋‚˜๋ฆฌ,85,70,75");
            out.println("์ง„๋‹ฌ๋ž˜,35,40,60");
            
            System.out.println("sungjuk.txt ๋ฐ์ดํ„ฐ ํŒŒ์ผ ์™„์„ฑ!!");
			
			
			
		} catch (Exception e) {
			System.out.println("ํŒŒ์ผ ์“ฐ๊ธฐ ์‹คํŒจ: " + e);
		}finally {
			//์ž์› ๋ฐ˜๋‚ฉ
			try {
				if(out!=null) { out.close(); }
			} catch (Exception e) {}
			try {
					if(fw!=null) { fw.close(); }
			} catch (Exception e) {}
	}
	


	} // main() end
	
} // class end
profile
์ฝ”๋”ฉ ๊ณต๋ถ€ ๐Ÿ’ป

0๊ฐœ์˜ ๋Œ“๊ธ€