java计算读取文件已经_JAVA编程中中如何读取TXT文件数据并计算

JAVA中如何读取TXT文件数据并计算,觉得好久评论

import java.io.BufferedReader;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

public class Testa

{

public static void main(String[] args)

{

//传入参数为文件目录

test("d:/a.txt");

}

public static void test(String filePath){

BufferedReader br = null;

try {

br = new BufferedReader(new FileReader(filePath));

} catch (FileNotFoundException e) {

e.printStackTrace();

return;

}

String []columnName = {"Id", "Name", "Languages", "Math", "English"}; //列名

int []courseIndexs = {2, 3, 4}; //课程对应的列

int i,j,index;

String line;

List> students = new ArrayList>();

//记录Id和总值,用于排序

List> sortList = new ArrayList>();

try {

br.readLine(); //去掉第一行

while((line = br.readLine()) != null){

index = 0;

String []se = line.split(" ");

Map student = new HashMap();

for(i = 0; i < se.length; i++){

if("".equals(se[i])){

continue;

}

if(index >= columnName.length){

continue;

}

student.put(columnName[index], se[i]);

index++;

}

//计算平均值,总值

double total = 0;

for(j = 0; j < courseIndexs.length; j++){

total += Double.parseDouble((String) student.get(columnName[courseIndexs[j]]));

}

double average = total / courseIndexs.length;

//只取一位小数

average = Math.round(average * 10)/10;

student.put("Total", total);

student.put("Average", average);

Map sort = new HashMap();

sort.put("Id", student.get("Id"));

sort.put("Total", student.get("Total"));

sortList.add(sort);

students.add(student);

}

br.close();

//排序

for(i = 0; i < sortList.size(); i++){

for(j = i + 1; j < sortList.size(); j++){

if((Double)sortList.get(i).get("Total") < (Double)sortList.get(j).get("Total")){

Map temp = sortList.get(i);

sortList.set(i, sortList.get(j));

sortList.set(j, temp);

}

}

}

Map sortedId = new HashMap();

for(i = 0; i < sortList.size(); i++){

sortedId.put(sortList.get(i).get("Id"), i+1);

}

//设定序号

for(j = 0; j < students.size(); j++){

students.get(j).put("Order", sortedId.get(students.get(j).get("Id")));

}

//输出(写到原文件)

//PrintWriter pw = new PrintWriter(new File(filePath));

//输出(写到其他文件)

PrintWriter pw = new PrintWriter(new File("D:/b.txt"));

pw.println("Id\tName\tLan\tMath\tEnglish\tAverage\tTotal\tSort");

int cIndex;

for(i = 0; i < students.size(); i++){

Map st = students.get(i);

cIndex = 0;

pw.println(st.get(columnName[cIndex++]) + "\t" + st.get(columnName[cIndex++])

+ "\t" + st.get(columnName[cIndex++])+ "\t" + st.get(columnName[cIndex++])

+ "\t" + st.get(columnName[cIndex++])

+ "\t" + st.get("Total")

+ "\t" + st.get("Average")

+ "\t" + st.get("Order"));

}

pw.flush();

pw.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值