비전공개미 개발노트
Java 6일차 - [문제풀이] 2차원배열, FileReader, FileWriter 본문
반응형
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
'프로그래밍 > Java' 카테고리의 다른 글
Java 7일차 - Thread, interface, implements (0) | 2022.09.28 |
---|---|
Java 6일차 - [문제풀이] File, FileWriter를 이용해 구구단파일 저장하기 (1) | 2022.09.23 |
Java 6일차 - Stream(스트림), abstract(추상클래스) (0) | 2022.09.23 |
Java - 몽고db연결하여 사원테이블 입력, 출력, 삭제, 수정 (0) | 2022.09.22 |
Java - [문제풀이] 캠프장 예약 (0) | 2022.09.22 |
Comments