Reputation: 29
I am trying to save the output of calculated data rate in a file. The data rate is of integer type, and I am not able to save it in a file, and I get the following error:
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)
The code is as follows:
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
}
can please anyone help me solving this?
Upvotes: 0
Views: 1407
Reputation: 1388
with this method can you save file. The method reading from source directory to destiny directory.
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();
}
}
}
}
I hope help you. :)
Upvotes: 0
Reputation: 28772
Once you close the stream, you should not try to do any more operations on it. Consider:
fo.close();
fo.flush();
You, at the minimum, will need to swap those two lines. Also, either re-open fo
so you can write to it in the other iterations of the loop or do not closit it in the middle of the loop (but after you are done with the loop).
Upvotes: 2
Reputation: 897
Is it because you close the file after writing to it and then try to write again? Try putting the fo.close() after the while loop.
Upvotes: 1