要求:
如在E盘下有一个Test.txt文本文件内容是
#4
#刘德华#89#77#60
#张信哲#76#97#70
#周杰伦#89#88#90
#随便啦#87#70#80
要求通过java读取文本并把文本中的每一行后面的三个成绩相加,
最后按从大到小的顺序输出到另一个文本文件中.
输出后的文本文件内容应为:
#4
#周杰伦#267
#张信哲#243
#随便啦#237
#刘德华#226
尽量把此题做成一个模块性质的东西,并不是单独当成一道题做!
-----------------------------------------------------------------------------------
哪位朋友能做的好的帮忙写一个好吗?
我写了一个,但是过程方法太麻烦了...希望高手能帮忙写个更棒的!
期待中....................
-------------------------------------------------------------------------------------
以下是本人所写代码 :
FileRead类
import java.io.*;
import java.util.*;
class FileRead
{
static BufferedReader inReader = null;
static BufferedWriter outWriter = null;
static String strIn = "";
static String strOut = "";
static int[] scoreCount = new int[5];
static HashMap hm = new HashMap();
//读文件
public static void readFile(String fileName)
{
try
{
inReader = new BufferedReader(new FileReader(fileName));
strIn = inReader.readLine();
int num = 0;
while(strIn != null)
{
//System.out.println(strIn);
FileRead.splitFile(strIn,num);
strIn = inReader.readLine();
num++;
}
sortFile(scoreCount);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//以#分割文件
public static void splitFile(String strTmp,int num)
{
String[] strArray = strTmp.split("#");
//System.out.println(strArray.length);
int score = 0;
for(int i=2;i<strArray.length;i++)
{
score += Integer.parseInt(strArray[i]);
}
scoreCount[num] = score;
hm.put(score,strArray[1]);
//System.out.println("总分:" + score);
}
//排序
public static void sortFile(int[] n)
{
int tmp;
for(int i=0;i<n.length;i++)
{
for(int j=i+1;j<n.length;j++)
{
if(n[i] > n[j])
{
tmp = n[i];
n[i] = n[j];
n[j] = tmp;
}
}
}
writeFile("E:\\New.txt");
//排好后打印出来
writeNow("#4");
System.out.println("#4");
for(int i=4;i>=1;i--)
{
String strOut = "#"+hm.get(n[i])+"#"+n[i];
writeNow(strOut); //把内容写到新文件中.
System.out.println(strOut);
}
try
{
outWriter.flush();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
//写入文件
public static void writeFile(String fileName)
{
try
{
outWriter = new BufferedWriter(new FileWriter(fileName));
}
catch(Exception e)
{
e.printStackTrace();
}
}
//开始写内容
public static void writeNow(String strOut)
{
try
{
outWriter.write(strOut);
outWriter.newLine();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
Test类
class Test
{
public static void main(String[] args)
{
FileRead.readFile("E:\\Txt.txt");
}
}
________________________________________________________________________