public class ReadWrite {
public static void main(String[] args) {
String line;
String[] csvArray = null;
File inputFile = new File("C:\test.csv");
File outputFile = new File("C:\seiseki.csv");
BufferedReader in = null;
BufferedWriter out = null;
try {
in = new BufferedReader(new FileReader(inputFile));
out = new BufferedWriter(new FileWriter(outputFile));
/** 1行ずつ読み込んで整形する */
while ((line = in.readLine()) != null) {
// 区切り文字カンマ[,]で、区切られた文字列を配列に格納する
csvArray = line.split("\\,");
// データを出力する
for (int i = 0; i < csvArray.length; i++) {
// 行の最後のデータはカンマ付けない
if (i != csvArray.length - 1) {
out.write(csvArray[i]);
out.write(",");
} else {
out.write(csvArray[i]);
}
}
// 改行する
out.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Student implements Comparable<Student> {
public final static int TOTAL = 0;
public final static int MATH = 1;
public final static int ENGLISH = 2;
public final static int SCIENCE = 3;
public final static String[] SUBJECTNAME = { "total", "math", "english",
"science" };
private static int SORTSUBJECT = TOTAL;
private String id;
private String name;
private int math;
private int english;
private int science;
public Student(String id, String name) {
this.id = id;
this.name = name;
this.math = 0;
this.english = 0;
this.science = 0;
}
public static String getSubjectName(int num) {
return SUBJECTNAME[num];
}
public int getScore(int subject) {
int score = 0;
switch (subject) {
case MATH:
score = this.math;
break;
case ENGLISH:
score = this.english;
break;
case SCIENCE:
score = this.science;
break;
case TOTAL:
score = this.math + this.english + science;
break;
}
return score;
}
public static void setSortSubject(int subject) {
SORTSUBJECT = subject;
}
public void setMath(int score) {
this.math = score;
}
public void setEnglish(int score) {
this.english = score;
}
public void setScience(int score) {
this.science = score;
}
public String getID() {
return this.id;
}
public String getName() {
return this.name;
}
public int getMath() {
return this.math;
}
public int getEnglish() {
return this.english;
}
public int getScience() {
return this.science;
}
@Override
public int compareTo(Student o) {
// TODO Auto-generated method stub
int value = 0;
switch (SORTSUBJECT) {
case MATH:
value = compareTo(this.math, o.getMath());
break;
case ENGLISH:
value = compareTo(this.english, o.getEnglish());
break;
case SCIENCE:
value = compareTo(this.science, o.getScience());
break;
case TOTAL:
int a = this.math + this.english + this.science;
int b = o.getMath() + o.getEnglish() + o.getScience();
value = compareTo(a, b);
break;
}
return value;
}
private int compareTo(int a, int b) {
// TODO Auto-generated method stub
if (a < b) {
return 1;
} else if (a > b) {
return -1;
}
return 0;
}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。