Menu
  • HOME
  • TAGS

Reading a .dat file into an array in Java

java,arrays,datainputstream,dataoutputstream

If you want to write binary data, use DataInputStream/DataOutputStream. Scanner is for text data and you can't mix it. WriteInts: import java.io.*; public class WriteInts { public static void main(String[] args) throws IOException { DataOutputStream output = new DataOutputStream(new FileOutputStream( "data.dat")); for (int i = 0; i < 100; i++)...

java socket DataInputStream

java,sockets,datainputstream

The problem is here: // read data into buffer dataReception.read(bs); Read doesn't read exactly that amount of bytes that you want to have in that array. It can read any number of bytes. Therefore you always have to check the return value of the read operation; and only when all...

How to get String from DataInputStream in Java?

java,android,sockets,utf,datainputstream

You write the data once, and on the other side you try to read it twice, but there is only one copy of the data there to be read. DataInputStream in = new DataInputStream(server.getInputStream()); System.out.println(in.readUTF()); //This prints "My String Message" This also consumes the only copy of the string that...

How to serialize/deserialize with DataOutputStream and DataInputStream?

java,serialization,deserialization,datainputstream,dataoutputstream

For a short explanation, calling dos.flush() will force the system to take anything buffered and actually write it to disk. For this reason, you need to call it before you try reading from the same file. For more details on flush(), I recommend looking at What is the purpose of...

DataInput: Bytes lost over TCP

java,sockets,tcp,datainputstream

I have confirmed that each write is matched by the appropriate read. Clearly that isn't the case. Recheck. Clearly you are out of sync with the sender. Possibly you are for example using read() where you should be using readFully(). NB TCP doesn't lose data. This is shifted by...

Java - Socket read from datainputstream and not get stuck in it

java,sockets,datainputstream

The method never returns because the while loop never ends, and this is caused by the connection or the DataInputStream remaining open. To send a variable number of bytes over a network connection where the reader reads a stream of characters you have three options: Send the number of bytes...

issue while extending InputStream class

java,inputstream,datainputstream,bufferedinputstream

I created the DataInputStream object using MyInputStream and got it working. Here is the updated code: public class MyClient { //InStreams protected DataInputStream mInStream; public int read(byte[] buffer) { MyClass obj1 = new MyClass(); mInStream = new DataInputStream(obj1.getInputStream()); try { int i = mInStream.read(buffer); return i; } catch (IOException ex)...

PrintStream Error with no reason?

java,streamwriter,datainputstream,printstream

The readUTF() method expects the data to be formatted in a specific way ( more details here ), the data send by what looks like a C# program is not formatted in a way that can be interpreted correctly by the readUTF() method. You can read the raw bytes from...

java Connection reset error

java,sockets,connection,datainputstream,dataoutputstream

In your server example, readUTF is only called once on the DataInputStream, even though the client wrote to the DataOutputStream twice. Thus, simply adding str = inp.readUTF(); str = str + " buddy!"; out.writeUTF(str); to your server example, after the last out.writeUTF(str), will solve your problem....

Detect DataInputStream end of stream

java,arrays,networking,packet,datainputstream

Your understanding of read(byte[]) is incorrect. It doesn't set a value in your array to -1. The Javadoc says: Returns: the total number of bytes read into the buffer, or -1 if there is no more data because the end of the stream has been reached. You need to check...

TCP can't recieve text messages after sending a file from server in Java

java,sockets,tcp,file-transfer,datainputstream

You've read the input until end of stream in your 'while ' loop, then you're trying to read more input with readUTF(). There isn't any more. Something wrong with your application protocol here. You need to send the file length ahead of the file, and only read that many bytes...

Why different type of exceptions thrown in below scenario?

java,double,java.util.scanner,datainputstream

The JavaDoc of Scanner.nextDouble() says: Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched. Returns: The double scanned from...

Reading specific parts of a binary file in Java

java,c#,binary-data,fileinputstream,datainputstream

Well I see one issue: you are writing the index variable of the for loop instead of the next byte from the file. You should at the very least switch out.write(c) to out.write(din.). Do you expect to write the next ten integers or the next 10 bytes? Assuming you want...