日期:2014-05-20 浏览次数:21047 次
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.");
}
}