datainputstream

sending a file using DataOutputStream in java

放肆的年华 提交于 2019-12-04 18:03:53
I am trying to build a client that sends the file size and contents to server. I am trying to use DataOutputStream. I am assuming that I need to open the file and and get size of file and read the contents and send it. But I am not sure how to implement those because I am really new to java... Can anyone help me about this? Thank you! It's quite simple, but the code is a bit long to write it all and sounds like homework. I can give you some indications. Just open the file, use the long length() method of the class File to get the size, and the writeLong(long) method of DataOutputStream to send

Difference Between DataInputStream/DataOutputStream Class & InputStream/OutputStream Class

本小妞迷上赌 提交于 2019-12-04 13:47:43
问题 Whenever I use HttpConnection Class in Java ME , Android or in BlackBerry , I uses DataInputStream / DataOutputStream class for reading & writing datas over remote server. However there are other class like InputStream / OutputStream which can be use for same purpose. I saw Question regarding InputStream / OutputStream class with HttpConnection . So I would like to know from experts that what are the differences between these two ? 回答1: DataInputStream/DataOutputStream isa InputStream

Can I read a local text file line by line into a string array?

懵懂的女人 提交于 2019-12-03 08:59:50
The question "How to read a local (res/raw) file line by line?" deals with a similar issue but I was unable to construct a solution base on the answers provided there. I did get a very useful piece of info (the DataInputStream class which has a readLine method), and I have been researching this on the developer website and trying to make it work. What I am trying to do is read information stored in successive lines of a text file into a string array, such that the first line is the first array element, the second line is the next array element, etc... and then this string array is going to be

Can not read number send with DataOutputStream

放肆的年华 提交于 2019-12-02 11:03:32
this is my Client code Random rand = new Random(); int n = rand.nextInt(50) + 1; DataInputStream dis = new DataInputStream(_socket.getInputStream()); DataOutputStream dos = new DataOutputStream(_socket.getOutputStream()); dos.writeInt(n); and this is the Server code try { DataInputStream dis = new DataInputStream(socket.getInputStream()); BufferedReader input = new BufferedReader(new InputStreamReader(dis)); int fromClient = input.read(); System.out.println(fromClient); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } But i don't recive anything in fromClient

Getting byte arrays using TCP connections

好久不见. 提交于 2019-12-01 20:55:52
问题 I was using UDP to send/receive data but I now want to switch to TCP to avoid packet loss. I've read several tutorials on TCP and noticed that instead of using DatagramPacket like UDP , TCP uses InputStream/OutputStream. How do we get the byte[] from DataInputStream, something that's similar to this: byte[] receiveData = new byte[64000]; DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length); receiveData=receivePacket.getData(); 回答1: The answer has 2 parts. Dealing

Getting byte arrays using TCP connections

爱⌒轻易说出口 提交于 2019-12-01 20:09:06
I was using UDP to send/receive data but I now want to switch to TCP to avoid packet loss. I've read several tutorials on TCP and noticed that instead of using DatagramPacket like UDP , TCP uses InputStream/OutputStream. How do we get the byte[] from DataInputStream, something that's similar to this: byte[] receiveData = new byte[64000]; DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length); receiveData=receivePacket.getData(); The answer has 2 parts. Dealing with 2 separate problems your questions is related to. 1 . Network facts TCP is inherently stream based. i.e

Java, need a while loop to reach eof. i.e.while !eof, keep parsing

最后都变了- 提交于 2019-12-01 11:32:32
I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the same output file until the end of the input file. Looks something like this. try { // my code parsing the data and appending to eof of output. (works) } catch (EOFException eof){ } Everything is done except the while loop. It only parses once when I need it to keep parsing. I'm looking for a while loop function to reach eof. I'm also using a DataInputStream. Is there some sort of DataInputStream.hasNext function?

Java, need a while loop to reach eof. i.e.while !eof, keep parsing

*爱你&永不变心* 提交于 2019-12-01 08:33:22
问题 I currently have a working parser. It parses a file once(not what I want it to do) and then outputs parsed data into a file. I need it to keep parsing and appending to the same output file until the end of the input file. Looks something like this. try { // my code parsing the data and appending to eof of output. (works) } catch (EOFException eof){ } Everything is done except the while loop. It only parses once when I need it to keep parsing. I'm looking for a while loop function to reach eof

Reading Integer user input in DataInputStream in java?

谁说胖子不能爱 提交于 2019-12-01 04:40:33
I am trying to get input from user using DataInputStream. But this displays some junk integer value instead of the given value My code is: import java.io.*; public class Sequence { public static void main(String[] args) throws IOException { DataInputStream dis = new DataInputStream(System.in); String str="Enter your Age :"; System.out.print(str); int i=dis.readInt(); System.out.println((int)i); } } And the output is Enter your Age :12 825363722 Pls explain. Why am I getting this junk value and how to correct the error? The problem is that readInt does not behave as you might expect. It is not

Reading Integer user input in DataInputStream in java?

耗尽温柔 提交于 2019-12-01 01:51:54
问题 I am trying to get input from user using DataInputStream. But this displays some junk integer value instead of the given value My code is: import java.io.*; public class Sequence { public static void main(String[] args) throws IOException { DataInputStream dis = new DataInputStream(System.in); String str="Enter your Age :"; System.out.print(str); int i=dis.readInt(); System.out.println((int)i); } } And the output is Enter your Age :12 825363722 Pls explain. Why am I getting this junk value