繁体   English   中英

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

[英]Couldn't write integer file into a file in java

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

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 
 }

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

一旦关闭流,就不应尝试对其进行任何其他操作。 考虑:

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

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

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

使用此方法可以保存文件。 从源目录读取到目的地目录的方法。

    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();
            }
        }
    }
}

希望对您有帮助。 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM