반응형
Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags
more
Archives
Today
Total
관리 메뉴

비전공개미 개발노트

Java 6일차 - [문제풀이] 2차원배열, FileReader, FileWriter 본문

프로그래밍/Java

Java 6일차 - [문제풀이] 2차원배열, FileReader, FileWriter

비전공개미 2022. 9. 23. 17:57
반응형
SMALL

2차원배열

//ExScore.java
package net.bit.day23;
import java.text.DecimalFormat;

public class ExScore {
	public static void main(String[] args) {
		int sum=0;
		double avg=0.0;
		int count = 0;
		//일반for반복문, 향상된for반복문, java.lang패키지 Math클래스
		//8장 339페이지 DecimalFormat
		DecimalFormat f = new DecimalFormat("#.#");
		
		int score[][] = {
				{78, 48, 78, 98},	// 302, 75.5
				{99, 92},			// 191, 95.5
				{29, 64, 83},		// 176, 58.7
				{34, 78, 92, 56}	// 260, 65
		};
		for(int i=0;i<score.length;i++){
			for(int j=0;j<score[i].length;j++){
				sum += score[i][j];
				count = score[i].length;
			}
			System.out.println((i+1) + "번째 합 = " + sum);
			avg = (double)sum / count;
			System.out.println((i+1) + "번째 평균 = " + f.format(avg));
			sum=0; //각행을 출력하고 값을 초기화
		}
		
	}
}
[출력]
1번째 합 = 302
1번째 평균 = 75.5
2번째 합 = 191
2번째 평균 = 95.5
3번째 합 = 176
3번째 평균 = 58.7
4번째 합 = 260
4번째 평균 = 65

 

 

FileReader / FileWriter

//TestFile.java
package net.bit.day23;

import java.io.File;

public class TestFile {
	public static void main(String[] args) {
		File f1 = new File("E:\\testJAVA\\aaa.txt");
		File f2 = new File("E:\\testJAVA", "aaa.txt");
		File dir = new File("E:\\testJAVA");
		File f3 = new File(dir,"aaa.txt");
		File f4 = new File("E:" + File.separator + "testJAVA" + File.separator + "aaa.txt");
		System.out.println("f1객체 " + f1.toString());
		System.out.println("f2객체 " + f2.toString());
		System.out.println("f3객체 " + f3.toString());
		System.out.println("f4객체 " + f4.toString());

	}
}
[출력]
f1객체 E:\testJAVA\aaa.txt
f2객체 E:\testJAVA\aaa.txt
f3객체 E:\testJAVA\aaa.txt
f4객체 E:\testJAVA\aaa.txt

 

aaa.txt 파일내용

로렌입숨Lorem Ipsum is simply dummy더미더미 text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
홍길동

//TestFile2.java
package net.bit.day23;

import java.io.File;
import java.io.FileInputStream;

public class TestFile2 {
	public static void main(String[] args) {
		try {
			File f1 = new File("E:\\testJAVA\\aaa.txt");
			FileInputStream fis = new FileInputStream(f1);
			while(true) {
				int data = fis.read();
				if(data == -1)
					break;
				System.out.print((char)data); //한글이 깨짐
			}
			fis.close();
		}catch(Exception e) { }
		

	}
}
로렌입숨Lorem Ipsum is simply dummy더미더미 text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
안홍ê°

문제점 - 출력시 한글이 깨짐

 

 

 

해결방법1)

//TestFile3.java
package net.bit.day23;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

public class TestFile3 {
	public static void main(String[] args) {
		try {
			String path = "E:\\testJAVA\\aaa.txt";
			File f1 = new File(path);
			//FileInputStream fis = new FileInputStream(f1); //단점은 한글이 깨짐
			FileReader fr = new FileReader(f1);
			while(true) {
				int data = fr.read();
				if(data == -1)
					break;
				System.out.print((char)data);
			}
			fr.close();
			System.out.println();
			System.out.println(path + " 읽기 성공");
		}catch(Exception e) { }
		

	}
}
[출력]
로렌입숨Lorem Ipsum is simply dummy더미더미 text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
홍길동
E:\testJAVA\aaa.txt 읽기 성공

 

 

해결방법2)

//TestFile4.java
package net.bit.day23;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

public class TestFile4 {
	public static void main(String[] args) {
		try {
			String path = "E:\\testJAVA\\aaa.txt";
			File f1 = new File(path);
			//FileInputStream fis = new FileInputStream(f1); //단점은 한글이 깨짐
			FileReader fr = new FileReader(f1);
			BufferedReader br = new BufferedReader(fr);
			while(true) {
				String data = br.readLine();
				if(data == null) break;
				System.out.println(data);
			}
		}catch(Exception e) { }
		

	}
}
[출력]
로렌입숨Lorem Ipsum is simply dummy더미더미 text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and scrambled it to make a type specimen book. 
홍길동

 

 

해결방법2)에서 path를 한번에 쓰기

//TestFile5.java
package net.bit.day23;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;

public class TestFile5 {
	public static void main(String[] args) {
		try {
			//String path = "E:\\testJAVA\\aaa.txt";
			//File f1 = new File(path);
			//FileInputStream fis = new FileInputStream(f1); //단점은 한글이 깨짐
			//FileReader fr = new FileReader(f1);
			BufferedReader br = new BufferedReader(new FileReader(new File("E:\\testJAVA\\aaa.txt")));
			while(true) {
				String data = br.readLine();
				if(data == null) break;
				System.out.println(data);
			}
		}catch(Exception e) { }
		

	}
}

 

반응형
LIST
Comments