9730 2007-8-17 991003 3177.52 3232.56 3143.53 3151.33
9729 2007-8-16 991003 3242.35 3242.35 3139.71 3197.57
9728 2007-8-15 991003 3306.08 3318.7 3172.78 3257.37
9727 2007-8-14 991003 3319.27 3334.33 3239.01 3284.85
9726 2007-8-13 991003 3166.63 3332.57 3166.63 3295.11
9725 2007-8-10 991003 3116.31 3182.62 3084.23 3140.2
9724 2007-8-9 991003 3085.44 3140.31 3084.33 3108.31
9723 2007-8-8 991003 3125.47 3135.04 3022.11 3092.28
9722 2007-8-7 991003 3203.14 3206.49 3136.42 3161.44
9721 2007-8-6 991003 3138.6 3205.76 3128.1 3205.54
9720 2007-8-3 991003 3114.36 3127.37 3063 3116.38
9719 2007-8-2 991003 3002.28 3088.12 2975.9 3070.7
9718 2007-8-1 991003 3175.06 3195.15 2983.91 2994.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."); } }