자바 파일입출력 질문욤
Soeun
FileReader file = new FileReader(input.txt);
FileWriter fw = new FileWriter(oupput.txt);
이것을 이용해야하는건 알겠는데
fw.write() 이걸 우찌해야할지도 몰것고;;
방법좀 갈쳐주세욤
import java.text.DecimalFormat;
class Student
{
/******************
* 멤버변수선언
******************/
private String classNum; // 반번호
private String stdNo; // 학생번호
private String stdName; // 학생명
private double korScore; // 국어점수
private double engScore; // 영어점수
private double mathScore; // 수학점수
private double totalScore; // 총점
private double avg; // 평균
private int classRanking; // 반석차
private int totalRanking; // 전교석차
private String totalScoreNm; // 학점
// 생성자
public Student(String classNum,
String stdNo,String stdName,
double korScore,
double engScore,
double mathScore) {
this.classNum = classNum;
this.stdNo = stdNo;
this.stdName = stdName;
this.korScore = korScore;
this.engScore = engScore;
this.mathScore = mathScore;
this.totalScore = korScore + engScore + mathScore;
this.avg = totalScore/3;
setTotalScoreNm(totalScore);
}
public String getClassNum() {
return classNum;
}
public int getClassRanking() {
return classRanking;
}
public double getEngScore() {
return engScore;
}
public double getKorScore() {
return korScore;
}
public double getMathScore() {
return mathScore;
}
public String getStdName() {
return stdName;
}
public String getStdNo() {
return stdNo;
}
public int getTotalRanking() {
return totalRanking;
}
public double getTotalScore() {
return totalScore;
}
public double getAvg() {
return avg;
}
public String getTotalScoreNm() {
return totalScoreNm;
}
public void setClassNum(String classNum) {
this.classNum = classNum;
}
public void setClassRanking(int classRanking) {
this.classRanking = classRanking;
}
public void setEngScore(double engScore) {
this.engScore = engScore;
}
public void setKorScore(double korScore) {
this.korScore = korScore;
}
public void setMathScore(double mathScore) {
this.mathScore = mathScore;
}
public void setStdName(String stdName) {
this.stdName = stdName;
}
public void setStdNo(String stdNo) {
this.stdNo = stdNo;
}
public void setTotalRanking(int totalRanking) {
this.totalRanking = totalRanking;
}
public void setTotalScore(double totalScore) {
this.totalScore = totalScore;
}
public void setAvg(double avg) {
this.avg = avg;
}
public void setTotalScoreNm(double totalScore) {
int score = (int)Math.round(totalScore);
int ts = (int)(score/3);
if(ts = 90) {
totalScoreNm = A;
} else if(ts = 80) {
totalScoreNm = B;
} else if(ts = 70) {
totalScoreNm = C;
} else if(ts = 60) {
totalScoreNm = D;
} else {
totalScoreNm = F;
}
}
public String toString() {
DecimalFormat df1 = new DecimalFormat(##0);
DecimalFormat df2 = new DecimalFormat(##0.00);
DecimalFormat df3 = new DecimalFormat(###);
return classNum + \t +
stdNo + \t +
stdName + \t +
korScore+ \t +
engScore + \t +
mathScore + \t +
df1.format(totalScore) + \t +
df2.format(avg) + \t +
df3.format(classRanking) + \t +
df3.format(totalRanking) + \t +
totalScoreNm;
}}
public class Studentimf {
public String stdName; // 학생명
private String stdNum;//학번
private String address; // 주소
private String phone; // 전화번호
public Studentimf(String stdName, String stdNing stdNum, String address, String phone){
this.stdName=stdName;
this.stdNum = stdNum;
this.address =address;
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getStdNum() {
return stdNum;
}
public void setStdNum(String stdNum) {
this.stdNum = stdNum;
}
public String getStdName() {
return stdName;
}
public void setStdName(String stdName) {
this.stdName = stdName;
}
public String toString() {
return
stdName+ \t + stdNum + \t + phone+ \t + address ;
}}
import java.io.*;
import java.util.*;
class StudentManagement
{
public static void main(String[] args) throws IOException {
if(args.length != 1) {
System.err.println(Usage : java classCount);
System.exit(1);
}
int classCnt = Integer.parseInt(args[0]); //반수
ArrayList[] classMgt = new ArrayList[classCnt];
for(int i=0; iclassCnt; i++) {
classMgt[i] = new ArrayList();
}
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// 반별 정보 입력
for(int i = 0; i classCnt; i++) {
System.out.print(i + 1 + 반의 인원 = );
int manCnt = Integer.parseInt(in.readLine());
// 개별 학생정보 입력
for(int j=0; j manCnt; j++) {
// 각 학생의 이름을 번호순서대로 입력하도록 화면에 표시한다
System.out.println(j + 1 + 번 학생의 정보 입력!);
System.out.print(이름 = );
String stdName = in.readLine();
System.out.print(국어 = );
String korScore = in.readLine();
System.out.print(영어 = );
String engScore = in.readLine();
System.out.print(수학 = );
String mathScore = in.readLine();
classMgt[i].add(new Student(Integer.toString((i+1)),
Integer.toString((j+1)),
stdName,
Double.parseDouble(korScore),
Double.parseDouble(engScore),
Double.parseDouble(mathScore)));
System.out.print(학번 = );
String stdNum = in.readLine();
System.out.print(주소 = );
String address = in.readLine();
System.out.print(전화번호 = );
String phone = in.readLine();
Studentimf s =new Studentimf(이름 : + stdName, 학번 : +stdNum,
주소 : +address, 전화번호 : + phone);
System.out.println(s.toString());
}
}
int i,j;
// 개별 학생정보 입력 끝
/*******************************
* 반별석차 구해서 저장
*******************************/
for(i=0; iclassMgt.length; i++) {
for(j=0; jclassMgt[i].size(); j++) { //new Student()를 통해 저장된 사이즈를
int defaultRanking = 1;
Student srcStd = (Student)classMgt[i].get(j); // 저장된 순서대로 각 인스턴스를 가져오는 겁니다.
double srcScore = srcStd.getTotalScore();
for(int k=0; kclassMgt[i].size(); k++) {
Student compStd = (Student)classMgt[i].get(k);
double compScore = compStd.getTotalScore();
if(srcScore compScore)
defaultRanking++;
}
srcStd.setClassRanking(defaultRanking);
}
}
// 반별석차 구하기 끝
/*******************************
* 전체석차 구해서 저장
*******************************/
int defaultRanking = 1;
for( i=0; iclassMgt.length; i++) {
for(j=0; jclassMgt[i].size(); j++) {
Student srcStd = (Student)classMgt[i].get(j);
double srcScore = srcStd.getTotalScore();
for(int k=0; kclassMgt.length;k++) {
for(int m=0; mclassMgt[k].size(); m++) {
Student compStd = (Student)classMgt[k].get(m);
double compScore = compStd.getTotalScore();
if(srcScore compScore)
defaultRanking++;
}
}
srcStd.setTotalRanking(defaultRanking);
defaultRanking = 1;
}
}
// 전체 석차 구하기 끝
// 전체 출력
System.out.println(반\t번호\t이름\t국어\t영어\t수학\t총점\t평균\t반석차\t전교\t학점);
for( i=0; iclassMgt.length; i++) {
for( j=0; jclassMgt[i].size(); j++) {
System.out.println((Student)classMgt[i].get(j));
}
}
}
}