无法将整数文件写入java中的文件

时间:2012-05-15 14:38:51

标签: java integer writefile

我正在尝试将计算数据速率的输出保存在文件中。数据速率是整数类型,我无法将其保存在文件中,我收到以下错误:

Exception in thread "main" java.io.IOException: Write error
at java.io.FileOutputStream.write(Native Method)
at java.io.DataOutputStream.write(Unknown Source)
at UDPSend.main(UDPSend.java:60)

代码如下:

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;


public class UDPSend {


public static void main(String[] args) throws IOException
{
    FileInputStream fstream = new FileInputStream("T.txt");
    DataInputStream in = new DataInputStream(fstream);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String path="DataRateBefore.txt";
    FileOutputStream f = new FileOutputStream(path);
    DataOutputStream fo = new DataOutputStream (f);
    InetAddress addr = InetAddress.getByName("localhost");
    byte[]  buf  = new byte[10000];

    String DataLine;
    long lastTime = System.currentTimeMillis();
    long firstTime = lastTime;
    int nobits = 0;

    long start = System.currentTimeMillis();
    System.out.println("Start Time: " + start);

    while ((DataLine = br.readLine()) != null)  
    { 
      DatagramPacket packet =new DatagramPacket(DataLine.getBytes(),DataLine.length() , addr, 4553);
      System.out.println (DataLine);
      DatagramSocket socket = new DatagramSocket();
      socket.send(packet);

      nobits+=packet.getLength() * 8;
      System.out.println(packet.getLength());
      System.out.println("no of bits is:");
      System.out.println(nobits);



      if ((System.currentTimeMillis() - lastTime) > 11) 
      {
         lastTime = System.currentTimeMillis();
         System.out.println("data rate is ");
         System.out.println(nobits); 
         fo.write(nobits);
         nobits= 0;
         fo.close();
         fo.flush();
      }

  }//end while loop

  // Get the end time of the process
    long end = System.currentTimeMillis();
    System.out.println("End Time : " + end);
    long elapsedTime = end - start;
  // Show how long it took to finish the process
    System.out.println("The process took approximately: " + elapsedTime + " milliseconds");
  }//end main method 
 }

可以请任何人帮我解决这个问题吗?

3 个答案:

答案 0 :(得分:2)

关闭流后,您不应该尝试对其进行任何操作。考虑:

fo.close(); 
fo.flush(); 

您至少需要交换这两行。另外,要么重新打开fo,要么可以在循环的其他迭代中写入它,或者不要在循环的中间关闭它(但是在完成循环之后)。

答案 1 :(得分:1)

是否因为您在写入文件后关闭文件然后再次尝试写入?尝试在while循环之后放入fo.close()。

答案 2 :(得分:0)

使用此方法

可以保存文件。该方法从源目录读取到目标目录。

    static public void movArchivo(File sourceDir, File destinyDir) {
    InputStream in = null;
    OutputStream out = null;

    byte[] buffer = new byte[1024];
    int len;

    try {
        in = new FileInputStream(sourceDir);
        out = new FileOutputStream(destinyDir);

        while((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        out.flush();
    } catch(FileNotFoundException e) {
        System.out.println("Sistem not found file: " + sourceDir.getAbsolutePath() + " or not existed path.");
    } catch(IOException e) {
        System.out.println("ERROR reading file. " + e.getMessage());
        e.printStackTrace();
    } finally {
        if(in != null) {
            try {
                in.close();
                sourceDir.delete();
                out.close();
            } catch(Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}

我希望能帮助你。 :)