简体   繁体   English

替代BufferedReader中的readLine()方法?

[英]alternative to readLine() method in BufferedReader?

I am new to java Network programming.I was googling the code for a TCP client in java.I came across the following example. 我是java网络编程的新手。我在谷歌搜索TCP客户端的代码。我遇到了以下示例。

import java.lang.*;
import java.io.*;
import java.net.*;

class Client {
   public static void main(String args[]) {
      try {
         Socket skt = new Socket("localhost", 1234);
         BufferedReader in = new BufferedReader(new
            InputStreamReader(skt.getInputStream()));
         System.out.print("Received string: '");

         while (!in.ready()) {}
         System.out.println(in.readLine()); // Read one line and output it

         System.out.print("'\n");
         in.close();
      }
      catch(Exception e) {
         System.out.print("Whoops! It didn't work!\n");
      }
   }
}

The client seems to read out the data one "line" at a time?. 客户似乎一次只读出一行数据? I am connecting to a server that is streaming OpenFlow packets.A wireshark screenshot of OpenFlow packets is given below. 我正在连接到一个流式传输OpenFlow数据包的服务器。下面给出了OpenFlow数据包的wireshark屏幕截图。

[http://www.openflow.org/downloads/screenshot-openflow-dissector-2008-07-15-2103.jpg][1] [http://www.openflow.org/downloads/screenshot-openflow-dissector-2008-07-15-2103.jpg][1]

Once I recieve the complete packets I want to dump that to a file and then later read it using wireshark for example.In the above code they are using calss BufferedReader to read the data in "lines"? 一旦我收到完整的数据包,我想将其转储到一个文件,然后稍后使用wireshark读取它。在上面的代码中,他们使用calss BufferedReader来读取“行”中的数据? At least that is how I understand it.Is there someway in which I can get full packets and then write it to the file? 至少这就是我理解它的方式。在某种程度上,我可以获得完整的数据包,然后将其写入文件?

Readers are for working with text data. 读者可以使用文本数据。 If you are working with binary data (it's not entirely clear from that screenshot), you should be working with some type of Stream (either InputStream or possibly DataInputStream ). 如果您正在使用二进制数据(从该屏幕截图中并不完全清楚),您应该使用某种类型的Stream( InputStream或可能是DataInputStream )。 Don't just look for random examples on online, try to find ones that actually apply to what you are interested in doing. 不要只是在网上寻找随机的例子,试着找到那些实际适用于你感兴趣的事情。

also, don't ever use InputStream.available , it's pretty much useless. 另外,不要使用InputStream.available ,它几乎没用。 as is any example code using it. 就像使用它的任何示例代码一样。

also, a simple google search for "OpenFlow java" had some interesting hits. 此外,一个简单的谷歌搜索“OpenFlow java”有一些有趣的点击。 are you sure you need to write something from scratch? 你确定你需要从头开始写东西吗?

No, but there are libraries that provides such functions. 不,但有些库提供了这样的功能。 See for example Guava 参见例如番石榴

http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/ByteStreams.html http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/io/ByteStreams.html

If you don't want to (or can't) use libraries you shoud consume a stream like this 如果您不想(或不能)使用库,您应该使用这样的流

List<String> lst = new ArrayList<String>();
String line;
while ((line = in.readLine()) != null) {
   lst.add(line);
}

or 要么

String str = "";
String line;
while ((line = in.readLine()) != null) {
   str += line + "\n";
}

Note that the BufferedReader.readLine() method will give you a new line on linebreaks ('\\n'). 请注意,BufferedReader.readLine()方法将为换行符('\\ n')提供一个新行。 If the InputStream is binary you should work with bytes instead. 如果InputStream是二进制文件,则应使用字节。

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

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