[英]Reading an entire char stream with a BufferedReader in Java
I am programming a simple AJAX application that's taking input from a form on a JSP and then using JSON to pass the data to a servlet. 我正在编写一个简单的AJAX应用程序,它从JSP上的表单中获取输入,然后使用JSON将数据传递给servlet。 In the servlet, I have the following quick and dirty code:
在servlet中,我有以下快速和脏代码:
char[] charBuffer = new char[500];
String[] names;
String[] values = new String[20];
int amountRead;
JSONObject jsonObject1;
JSONObject jsonObject2;
PrintWriter out = response.getWriter();
BufferedReader in = request.getReader();
amountRead = in.read(charBuffer);
charBuffer[amountRead]='\0';
String invalue = new String( charBuffer, 0, amountRead);
This is all then followed by a try
block that does some stuff with the JSON objects. 接下来是一个
try
块,它使用JSON对象执行一些操作。 Basically, as an exercise, I am trying to pass the same form data back and forth and display it. 基本上,作为练习,我试图来回传递相同的表格数据并显示它。
Here is the problem: In this quick and dirty code, of course, there's no guarantee that you get the full message from the request. 问题在于:在这个快速而肮脏的代码中,当然,并不能保证您从请求中获得完整的消息。 Knowing that the
read()
method of BufferedReader
will return -1 if the end of the stream has been reached, I've tried making the following addition: 知道
BufferedReader
的read()
方法如果已到达流的末尾将返回-1,我尝试进行以下添加:
while( in.read() != -1 ) {
amountRead = in.read(charBuffer);
}
Once I implement this, however, my output is blank. 但是,一旦我实现了这个,我的输出就是空白。 I'm just getting nothing.
我什么都没得到。 Even after trying to step through it with the debugger, I'm not entirely sure why that
while
loop isn't having the intended effect. 即使在尝试使用调试器逐步完成它之后,我也不完全确定为什么
while
循环没有达到预期的效果。
I'm sure it's something simple and stupid. 我确信这是一件简单而愚蠢的事。 Could somebody be so kind as to point out the error of my ways?
有人可以如此友善地指出我的方式的错误吗? As always, thanks in advance.
一如既往,先谢谢。
You're reading a char then throwing it away if you didn't get EOS. 如果你没有得到EOS,你正在阅读一个字符然后扔掉它。 The correct way to read streams in Java is like this:
在Java中读取流的正确方法如下:
while ((count = in.read(buffer)) > 0)
{
out.write(buffer, 0, count);
// or
String s = new String(buffer, 0, count);
// or whatever else you want.
}
Your insertion of a trailing null character is unnecessary, too. 您也不需要插入尾随空字符。 That's a C-ism.
这是一个C-ism。 You're already bounding the string by specifying the count, so the null character is never even seen.
您已经通过指定计数来绑定字符串,因此甚至看不到空字符。
The BufferedReader.readLine()
method will read exactly one line at a time and each read will block until the data is available. BufferedReader.readLine()
方法将一次读取一行,每次读取将阻塞,直到数据可用。 It'll return null when the end of the stream has been reached. 当到达流的末尾时,它将返回null。
So in a while loop you can use 所以在while循环中你可以使用
String line;
BuffererReader br = request.getReader();
while ((line = br.readLine()) != null) {
//process the line here
}
You can also use a library like Gson
which can deserialise a Java class from a Reader
for you. 您也可以使用像
Gson
这样的库,它可以为您从Reader
反序列化Java类 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.