bufferedreader

Android - Interactive shell (Runtime.getRuntime().exec())

 ̄綄美尐妖づ 提交于 2020-01-03 02:53:17
问题 How to run an interactive shell on android? (rooted device) I need the nexts steps: 1 - Execute shell process (onCreate) Process p = Runtime.getRuntime().exec(String[]{"su","-c","sh"}); 2 - Get the output (onCreate) BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream)); //dos is a field (a class attribute) dos = new DataOutputStream(p.getOutputStream()); new Thread(new Runnable() { @Override public void run() { String l; //wait the console output and write it while(

Can BufferedReader continue to read line in text file when pass to another function?

那年仲夏 提交于 2020-01-02 18:57:09
问题 I have this function to read text file with BufferedReader. I want to know if I can called another function and pass the readline/continue to read text file. If it can pass, can it loop the same function? 回答1: A BufferedReader object is self-contained (thanks to Java's heavy emphasis on Object Orientation), so you are free to pass it between functions and inside loops, and it will retain it's state - including the read line. However, if you are trying to call a method to process the line you

Read in N Lines of an Input Stream and print in reverse order without using array or list type structure?

偶尔善良 提交于 2020-01-02 04:25:19
问题 Using the readLine() method of BufferedReader , can you print the first N lines of a stream in reverse order without using a list or an array? 回答1: I think you can do it through recursion with something like: void printReversed(int n) { String line = reader.readLine(); if (n > 0) printReversed(n-1); System.out.println(line); } 回答2: How about recursion to reverse the order? Pseudo code: reverse(int linesLeft) if (linesLeft == 0) return; String line = readLine(); reverse(linesLeft - 1); System

Android - open file from phone memory using intent

守給你的承諾、 提交于 2020-01-02 00:24:07
问题 I am working on an app that takes the .txt file from a phone as input and prints it on the TextView, public class MainActivity extends AppCompatActivity { Button button; Intent intent;private StringBuilder text = new StringBuilder(); private InputStream getResources(String s) { return null; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.btn); button

How can you make BufferedReader.readLine() not hang?

五迷三道 提交于 2020-01-01 11:26:11
问题 Is there a way to make BufferedReader.readLine() not hang? I'm creating a server that: Checks if the client has delivered any input. If not, it executes other code and eventually loops back to checking the client for input. How can I check if the client has delivered any input without running readLine() ? If I run readLine() , the thread will hang until input is delivered? 回答1: You can use BufferedReader.ready() , like this: BufferedReader b = new BufferedReader(); //Initialize your

BufferedReader reset fail after read to end of file

非 Y 不嫁゛ 提交于 2019-12-31 03:01:43
问题 I have a BufferedReader wrapped on a file, I want to mark a place and use reset() later to get back to this position. I have read the java api, it states mark (readlimit), when read too many bytes, the reset will fail. So i figure I can set a large limit. However if i have code BufferedReader br=...; br.mark(1024000); // large but not as large as file. while(br.ready()){ //keep reading. to find some stuff. } //now the br.ready() is false br.reset() // It will fail with mark invalid exception

java reader vs. stream

心已入冬 提交于 2019-12-31 00:46:12
问题 I was reading about Java I/O and found some interesting areas like streams, readers etc. InputStream input = new FileInputStream("input-file.txt"); int data = input.read(); while(data != -1){ data = input.read(); } I can do the same thing by using Readers as follows: Reader reader = new FileReader("input-file.txt"); int data = reader.read(); while(data != -1){ char dataChar = (char) data; data = reader.read(); } As I know, Streams are used to retrieve input from continuously flowing data. Now

Randomizing text file read in Java

别说谁变了你拦得住时间么 提交于 2019-12-30 18:34:15
问题 I am trying to read a text file in Java, basically a set of questions. With four choices and one answer. The structure looks like this: question option a option b option c option d answer I have no trouble reading it that way: public class rar{ public static String[] q=new String[50]; public static String[] a=new String[50]; public static String[] b=new String[50]; public static String[] c=new String[50]; public static String[] d=new String[50]; public static char[] ans=new char[50]; public

Randomizing text file read in Java

时间秒杀一切 提交于 2019-12-30 18:34:02
问题 I am trying to read a text file in Java, basically a set of questions. With four choices and one answer. The structure looks like this: question option a option b option c option d answer I have no trouble reading it that way: public class rar{ public static String[] q=new String[50]; public static String[] a=new String[50]; public static String[] b=new String[50]; public static String[] c=new String[50]; public static String[] d=new String[50]; public static char[] ans=new char[50]; public

java.io.IOException: BufferedInputStream is closed in Android 2.3

倖福魔咒の 提交于 2019-12-30 06:47:12
问题 The following code is working fine in Android 1.5-2.2.1 but it's not in 2.3 and higher. BufferedReader rd; rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = rd.readLine()) != null){ sb.append(line); } rd.close(); The stracktrace: 01-30 08:21:42.668: WARN/System.err(594): java.io.IOException: BufferedInputStream is closed 01-30 08:21:42.668: WARN/System.err(594): at java.io.BufferedInputStream