I am writing a server with protocol to translate text by client.
For getting the input code follows:
InputStreamReader isr = new InputStreamReader(clntSock.getInputStream(), "CP852");
BufferedReader br = new BufferedReader(isr);
while ((line = br.readLine()) != null) {
System.out.println(line);
The problem is that after printing out the input (for checking if its accepted correctly), it prints something like "îAU" instead of "ČAU".
I know I could use byte conversion, but wanted to do it this way and can't find the error. Please help
Related
I have a web service which returns xml document contains characters like "ÅUðSÅU". When I read this file using
InputStreamReader, it's generate string like "ÅUðâ??ÅU" .
I'm using UTF-8 but not working. My code is following-
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
int value=0;
// reads to the end of the stream
while ((line = br.readLine()) != null) {
System.out.println("data--"+line);
response += line;
}
I am working on a simple programm for a university course. Here is the code that I have problems with:
//everything before this is unrelevant
String message = "";
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while((message = br.readLine()) != null) {
System.out.println(message);
}
//everything after this is irrelevant
I also have a thread for accepting incoming connections and some other stuff that is irrelevant. The problem that I have is that I can read one message and after that nothing happens. I guess the readLine() method is the problem but I am not really sure how to solve it.
It even says in our assignment to use
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
to receive data.
Your client is only sending one line. You therefore don't need the loop. You should read one line and then do whatever you're supposed to do with it.
I am writing a program in Java that parses some text from a web page. But when I use the code below I get weird/incorrect characters.
code:
URL url = new URL(getSearchUrl(crit));
URLConnection connection = url.openConnection();
BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), "UTF-8"));
String line;
while((line = br.readLine()) != null){
System.out.println(line);
}
br.close();
I get the following output:
?}?v?8????...
So what am I doing wrong? I know that the site I want to gather info from uses utf-8.
Edit: I am currently in Crotia. I tried some other program I know worked in Serbia (my home country) but it doesn't work here.
It's g-zipped. you can see it using connection.getContentEncoding().
If you use a GZIPInputStream around the connection.getInputStream() it should work better.
BufferedReader br = new BufferedReader(
new InputStreamReader(new GZIPInputStream(connection.getInputStream()), "UTF-8"));
How do I print to stdout the output string obtained executing a command?
So Runtime.getRuntime().exec() would return a Process, and by calling getOutputStream(), I can obtain the object as follows, but how do I display the content of it to stdout?
OutputStream out = Runtime.getRuntime().exec("ls").getOutputStream();
Thanks
I believe you are trying to get the output from process, and for that you should get the InputStream.
InputStream is = Runtime.getRuntime().exec("ls").getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader buff = new BufferedReader (isr);
String line;
while((line = buff.readLine()) != null)
System.out.print(line);
You get the OutputStream when you want to write/ send some output to Process.
Convert the stream to string as discussed in Get an OutputStream into a String and simply use Sysrem.out.print()
Is there a better substitute for Scanner class when using sockets? i am getting really tired of the .next() and .nextLine() that are really annoying to work with because they skip lines all the time.
Use InputStreamReader along with BufferedReader
Eg:
Socket s = new Socket("10.0.0.1",4444);
InputStreamReader isr = new InputStreamReader(s.getInputStream());
BufferedReader br = new BufferedReader(isr);
String str = new String();
while ((str = br.readLine())!=null){
System.out.println(str);
}