java读取marc
发布网友
发布时间:2023-08-19 19:27
我来回答
共1个回答
热心网友
时间:2023-08-24 14:18
实例package test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
class Test {
private static BufferedReader bw;
public static void main(String[] args) {
String ss = null;
List<TestPo> list = new ArrayList<TestPo>();
try{
File file=new File("E:\\test.txt");
FileOutputStream out=new FileOutputStream(new File("E:\\test1.txt"),true);
bw=new BufferedReader(new FileReader(file));
boolean flag = true;
int[] temps = null;
while((ss=bw.readLine())!=null){
String[] mark = ss.split(":");
int sum =0;
TestPo po = new TestPo();
int[] temp = new int[mark.length-1];
temps = new int[mark.length-1];
for (int i = 1;i< mark.length;i++) {
temp[i-1] = Integer.parseInt(mark[i].trim());
sum += temp[i-1];
if (flag) {
temps[i-1] = 0;
}
}
flag = false;
po.setMark(temp);
po.setName(mark[0]);
po.setAvg(sum/(mark.length-1));
list.add(po);
out.write(("\t\n"+ss).getBytes());
}
bw.close();
out.close();
int row = list.size();
int[][] marks = new int[row+1][];
for (int i = 0; i < row; i++) {
marks[i] = list.get(i).getMark();
}
marks[row] = temps;
int sumTemp =0 ;
int count = list.get(0).getMark().length;
for (int i = 0; i < count; i++) {
for (int j = 0; j < row; j++) {
sumTemp += marks[j][i];
}
marks[row][i] = sumTemp/row;
sumTemp = 0;
}
for (int[] is : marks) {
for (int i : is) {
System.out.print(i+" ");
}
System.out.println();
}
}catch(IOException e){
e.printStackTrace();
}
for (TestPo po : list) {
System.out.println("Name:"+po.getName()+" AVG:"+po.getAvg());
}
}
}
class TestPo{
private String name;
private Integer avg;
private int[] mark;
public int[] getMark() {
return mark;
}
public void setMark(int[] mark) {
this.mark = mark;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAvg() {
return avg;
}
public void setAvg(Integer avg) {
this.avg = avg;
}
}