public class WriteData
{
WriteData ()
{

}

public static void main (String args[])
{
FileOutputStream outputFile = null;
String strTmp = null;
int i = 0;
int r = 0;

try
{
outputFile = new FileOutputStream ("D:\\dataFile.txt");
ObjectOutputStream serializeStream = new ObjectOutputStream (outputFile);

while (i < 5) 
{
r = System.in.read ();
strTmp = "";
while (r != 13)
{
if (((r >= 48) && (r <= 57)) || r == 46)
{
strTmp += (char) r;
} r = System.in.read ();
}
serializeStream.writeDouble(Double.parseDouble(strTmp));
i ++;
}
serializeStream.flush ();
}
catch (Exception e)
{
System.out.println ("Error during serialization");
}
finally
{
try
{
outputFile.close ();
}
catch (Exception e)
{
System.out.println (e.toString ());
}
}
}
}

解决方案 »

  1.   

    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.sql.Date;/**
     * @author Administrator
     *
     * To change the template for this generated type comment go to
     * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
     */
    public class ReadData
    { public static void main(String[] args)
    {
    Date wasThen;
    String theString;
    FileInputStream inputFile = null;
    int i = 0;
    double tmp = 0.0;
    double d [] = new double [5];
    double ave = 0.0;

    try
    {
    inputFile = new FileInputStream ("D:\\dataFile.txt");
    DataInputStream in = new DataInputStream (inputFile); ObjectInputStream serializeStream = new ObjectInputStream (inputFile);
    while (i < 5)
    {
    d [i] = serializeStream.readDouble(); i ++;
    }

    for (i = 0; i < 5; i ++)
    {
    for (int j = i; j < 5; j ++)
    {
    if (d [i] > d [j])
    {
    tmp = d [i];
    d [i] = d [j];
    d [j] = tmp;
    }
    }
    }

    tmp = 0.0;
    for (i = 0; i < 5; i ++)
    {
    tmp = tmp + d [i];
    System.out.println (d[i]);
    }
    ave = tmp / 5;
    System.out.println ("average:" + ave);
    }
    catch (Exception e)
    {
    System.out.println ("Error during serialization");
    return;
    }
    finally
    {
    try
    {
    inputFile.close ();
    }
    catch (Exception e)
    {
    System.out.println (e.toString ());
    }
    }
    }
    }
      

  2.   

    第一个需要引用!!import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
      

  3.   

    我也来凑个热闹,有些日子没写java程序了,复习复习//输入成绩import java.io.*;class WriteData
    {
    public static void main(String[] args)
    {
    byte[] byteBuf = new byte[81];//缓冲区
    File file = new File("g:\\data.txt");//输出文件
    FileOutputStream fos = null;//输出流
    try{
    fos = new FileOutputStream(file);
    }
    catch(FileNotFoundException fnfe)
    {
    fnfe.printStackTrace();
    } for(int i=0; i<5; i++)//输入成绩
    {
    System.out.print("请输入成绩 " + (i+1) + ":");
    try{
    System.in.read(byteBuf);
    String sTemp = new String(byteBuf);
    sTemp = sTemp.trim() + " ";
    fos.write(sTemp.getBytes());//写入文件
    }
    catch(IOException ioe)
    {
    ioe.printStackTrace();
    }
    } try{
    fos.close();//关闭流
    fos = null;
    }
    catch(IOException ioe)
    {
    ioe.printStackTrace();
    }
    }
    }//计算成绩import java.io.*;
    import java.util.*;class ComputeData
    {
    public static void main(String[] args)
    {
    String sTemp;
    try{
    byte[] byteBuffer = new byte[81];//缓冲区
    File file = new File("g:\\data.txt");//输入文件
    FileInputStream fis = new FileInputStream(file);//输入流
    fis.read(byteBuffer);
    fis.close();
    sTemp = new String(byteBuffer);
    }
    catch(IOException ioe)
    {
    ioe.printStackTrace();
    return;
    } System.out.println("原始成绩: " + sTemp);
    StringTokenizer st = new StringTokenizer(sTemp, " ");
    double[] dScore = new double[5];
    int iPosition = 0;
        while (st.hasMoreTokens()) {
         String s = st.nextToken();
         if(s != null)
         {
         s = s.trim();
         }
         if(s.length() > 0)
    dScore[iPosition++] = Double.parseDouble(s);
         }
    double dSum = 0.0;
    for(int i=0; i<dScore.length; i++)
    dSum += dScore[i];
    System.out.println("平均成绩: " + (dSum/dScore.length)); bubbleSort(dScore); System.out.println();
    System.out.print("排序后成绩: ");
    for(int i=0; i<dScore.length; i++)
    {
    System.out.print(dScore[i] + " ");
    }
    System.out.println();
    } //冒泡排序
    public static void bubbleSort(double[] dValue)
    {
    for(int i=0; i<dValue.length; i++)
    {
    for(int j=i; j<dValue.length; j++)
    {
    if(dValue[i] > dValue[j])
    {
    double dTemp = dValue[i];
    dValue[i] = dValue[j];
    dValue[j] = dTemp;
    }
    }
    }
    }}