读书人

java的,该怎么处理

发布时间: 2012-01-15 22:57:48 作者: rapoo

java的
97302007-8-179910033177.523232.563143.533151.33
97292007-8-169910033242.353242.353139.713197.57
97282007-8-159910033306.083318.73172.783257.37
97272007-8-149910033319.273334.333239.013284.85
97262007-8-139910033166.633332.573166.633295.11
97252007-8-109910033116.313182.623084.233140.2
97242007-8-99910033085.443140.313084.333108.31
97232007-8-89910033125.473135.043022.113092.28
97222007-8-79910033203.143206.493136.423161.44
97212007-8-69910033138.63205.763128.13205.54
97202007-8-39910033114.363127.3730633116.38
97192007-8-29910033002.283088.122975.93070.7
97182007-8-19910033175.063195.152983.912994.1
用Java I0 api将其中的所有数据读出,计算出每一行的后4列数据的平均值,存放到新文件AvgSomeData.txt文件中,存放时每一行前面要加上行号(行号占3位,右对齐),平均值放到行末并左对齐。

求整个的源代码,如何把后四排数据取出来啊~~~而且为什么我用
public static void main(String[] args) {
// TODO Auto-generated method stub

FileReader myReader;
BufferedReader myIn;
FileWriter myWriter;
BufferedWriter myOut;
String strWork;
int counter=1;
try
{
myReader=new FileReader("src\\SomeData.txt");
myIn=new BufferedReader(myReader);
myWriter=new FileWriter("src\\AvgSomeData.txt");
myOut=new BufferedWriter(myWriter);

while((strWork=myIn.readLine())!=null)
{
myOut.write(counter+ " "+strWork+"\r\n");
System.out.println(strWork);
counter++;

}
myIn.close();
myReader.close();

}
catch(IOException e)
{
System.out.println("Something wrong with the file.");
return;
}


}

我写入的第一个数据是乱码,这是为什么~~~~

[解决办法]

Java code
public static void main(String[] args)    {        FileReader myReader;        BufferedReader myIn;        FileWriter myWriter;        BufferedWriter myOut;        String strWork;        int counter = 1;        try        {            myReader = new FileReader("SomeData.txt");            myIn = new BufferedReader(myReader);            myWriter = new FileWriter("AvgSomeData.txt");            myOut = new BufferedWriter(myWriter);            String[] avg = null;            double sum = 0;            String countFormat = null;                        while ((strWork = myIn.readLine()) != null)            {                avg = strWork.split(" ");                countFormat = String.format("%3d", counter);                sum = 0;                for (int i = avg.length - 1; i >=  avg.length - 4; i--)                {                    sum += Double.parseDouble(avg[i]);                }                                myOut.write(countFormat + " " + sum/4 + "\r\n");                System.out.println(strWork);                counter++;                            }            myIn.close();            myReader.close();            myOut.close();            myWriter.close();                    }        catch (IOException e)        {            System.out.println("Something wrong with the file.");        }            } 

读书人网 >Java相关

热点推荐