inputstreamreader

Process.waitFor() a thread

被刻印的时光 ゝ 提交于 2019-12-10 10:54:28
问题 While running an external script, I want to read the ErrorStream and OutputStream of this script both simultaneously and separately and then process them further. Therefore, I start a Thread for one of the streams. Unfortunately, the Process doesn't seem to waitFor the Thread to be terminated, but return after the non-threaded stream has no further input. In a nutshell, here is what I am doing: ProcessBuilder pb = new ProcessBuilder(script); final Process p = pb.start(); new Thread(new

InputStreamReader and reading random lines from .txt file

元气小坏坏 提交于 2019-12-08 07:31:05
问题 I have a method for my app to read a random line from a text file and return it. Im using the randTxt() to read and return a random line from the txt file. but it only shows the same line (1st line) everytime. public String randTxt(){ // Read in the file into a list of strings InputStreamReader inputStream = new InputStreamReader(getResources().openRawResource(R.raw.randomstuff)); //ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); String theLine=""; int i; try { i =

Process.waitFor() a thread

£可爱£侵袭症+ 提交于 2019-12-06 13:04:08
While running an external script, I want to read the ErrorStream and OutputStream of this script both simultaneously and separately and then process them further. Therefore, I start a Thread for one of the streams. Unfortunately, the Process doesn't seem to waitFor the Thread to be terminated, but return after the non-threaded stream has no further input. In a nutshell, here is what I am doing: ProcessBuilder pb = new ProcessBuilder(script); final Process p = pb.start(); new Thread(new Runnable() { public void run() { BufferedReader br = new BufferedReader(new InputStreamReader(p

Reading the content of web page

孤人 提交于 2019-12-02 03:07:08
Hi I want to read the content of a web page that contains a German characters using java , unfortunately , the German characters appear as strange characters . Any help please here is my code: String link = "some german link"; URL url = new URL(link); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } You have to set the correct encoding. You can find the encoding in the HTTP header: Content-Type: text/html; charset=ISO-8859-1 This may be overwritten in the (X)HTML

How can I read from a BufferedReader in Java without blocking?

左心房为你撑大大i 提交于 2019-11-29 08:04:40
I want to send a command to a server, and find out if I get a response. Right now i am using BufferedReader 's readline() function, which blocks until there's a response from server, but all I want to do is verify that there's a response from the server in the first place. I tried using ready() or reset() to avoid this block, but it doesn't help. This is causing my program to get stuck waiting for the server to respond, which never happens. InputStreamReader seems to do the same thing, by my understanding of things. Other questions I found here on the subject didn't answer my question, so

Closing BufferedReader and InputStreamReader

心不动则不痛 提交于 2019-11-28 12:09:57
This piece of code is creating memory leak issues cause of BufferedReader and InputStreamReader which I think might be happening cause of some exceptions. How should I change it? try{ URL url = new URL(sMyUrl); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); while ((str = in.readLine()) != null) { jsonString += str; } in.close(); }catch(Exception e){ } It would be safer to close your stream using a try..finally block. You might also use a StringBuilder as it is designed for concatenating strings. You should also avoid catching Exception and doing nothing with

How can I read from a BufferedReader in Java without blocking?

独自空忆成欢 提交于 2019-11-28 01:47:54
问题 I want to send a command to a server, and find out if I get a response. Right now i am using BufferedReader 's readline() function, which blocks until there's a response from server, but all I want to do is verify that there's a response from the server in the first place. I tried using ready() or reset() to avoid this block, but it doesn't help. This is causing my program to get stuck waiting for the server to respond, which never happens. InputStreamReader seems to do the same thing, by my

Read all lines with BufferedReader

余生长醉 提交于 2019-11-27 12:56:10
问题 I want to type a multiple line text into the console using a BufferedReader and when I hit "Enter" to find the sum of the length of the whole text. The problem is that it seems I'm getting into an infinite loop and when I press "Enter" the program does not come to an end. My code is below: InputStreamReader instream = new InputStreamReader(System.in); BufferedReader buffer = new BufferedReader(instream); line= buffer.readLine(); while (line!=null){ length = length + line.length(); line=