bufferedreader

Should BufferedReader and InputStreamReader be closed explicitly?

流过昼夜 提交于 2019-12-06 10:00:18
I want to read the content of a InputStream into a String : private String readToString(InputStream stream) { return new BufferedReader(new InputStreamReader(stream)) .lines().collect(Collectors.joining("\n")); } The stream comes from java.lang.Process . Question: Do I have to explicitly close any of the InputStream , InputStreamReader or BufferedReader in this case? Sidenote: the linked question is NOT a duplicate, as my question is about HOW to properly close the streams, not how to read the stream to a String! You only need to close the outer wrapper, but don't do that explicitly either way

How to clear/reset/open an input stream so it can be used in 2 different methods in Java?

↘锁芯ラ 提交于 2019-12-06 09:46:50
Here's the code: package testpack; import java.io.*; public class InputStreamReadDemo { private void readByte() throws IOException { System.out.print("Enter the byte of data that you want to be read: "); int a = System.in.read(); System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+"."); // tried writing System.in.close(); and System.in.reset(); } private void readLine() throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a line

Unhandled exception: FileNotFoundException

匆匆过客 提交于 2019-12-06 08:45:29
I have some problems reading file in java: my file is for example: 3,4 2 6 4 1 7 3 8 9 where first line 3 and 4 are the lenght of array A and B and then the element of each array. I made this import java.io.*; import java.util.Arrays; public class Progetto { public static void main(String args[]) { // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("prova.txt"); BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); String strLine = br.readLine(); // step 1 if (strLine != null) { String[] delims = strLine.split(","); //

Why is Java HashMap slowing down?

◇◆丶佛笑我妖孽 提交于 2019-12-06 07:31:22
I try to build a map with the content of a file and my code is as below: System.out.println("begin to build the sns map...."); String basePath = PropertyReader.getProp("oldbasepath"); String pathname = basePath + "\\user_sns.txt"; FileReader fr; Map<Integer, List<Integer>> snsMap = new HashMap<Integer, List<Integer>>(2000000); try { fr = new FileReader(pathname); BufferedReader br = new BufferedReader(fr); String line; int i = 1; while ((line = br.readLine()) != null) { System.out.println("line number: " + i); i++; String[] strs = line.split("\t"); int key = Integer.parseInt(strs[0]); int

BufferedReader blocking at read()

喜欢而已 提交于 2019-12-06 04:59:36
Im trying to create a simple chat program, with a "server" and a client, now my problem is that the program blocks while reading messages from the server to the client and vice-versa. This example features the problem with messages from Client to Server. Example of what I have on the server side: private Reader input; private Writer output; try { server = new ServerSocket(this.port); while (true) { Socket connection = server.accept(); serverDisplay("We have a connection"); input = new BufferedReader(new InputStreamReader( connection.getInputStream())); output = new BufferedWriter(new

Understanding how BufferedReader works in Java

我只是一个虾纸丫 提交于 2019-12-06 03:17:59
问题 Very basic question on how BufferedReader works. Given the string/phrase, I want to find and print it from the file with a lot of text in it. using BufferedReader in Java I did some research on this topic and that was the closest result. Not quite addressing my problem though. So with this information, why does the following code terminate? public class MainApp { String line = null; String phrase = "eye"; try { File file = new File("text.txt"); FileReader fr = new FileReader(file);

BufferedReader.readLine() waits for input from console

笑着哭i 提交于 2019-12-06 00:59:41
问题 I am trying to read lines of text from the console. The number of lines is not known in advance. The BufferedReader.readLine() method reads a line but after the last line it waits for input from the console. What should be done in order to avoid this? Please see the code snippet below: public static String[] getLinesFromConsole() { String strLine = ""; try { // Get the object of DataInputStream InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader

Comma-separated records into String Array?

╄→尐↘猪︶ㄣ 提交于 2019-12-05 22:36:54
I am trying to read a BufferedReader that reads in a file containing records separated by commas. I would like to split each string (or record) in between two commas, strip the double quotes, and put each of those into an index of a String array. For example: say I have this line in the file: ("0001", "00203", "82409" (newline) "0002", "00204", "82500" (newline) etc.) I want to put 0001 into a String array[1], I want 00203 into String array[2], and so on.... The following code traverses the file, putting all records in column two into String array[2]. This means, after I execute the code below

Java read from one file and write into another file using methods

南楼画角 提交于 2019-12-05 19:06:01
I am learning Java and working on File IO and right now stuck in reading text from One file and write in another file. I am using two different methods first one for reading and displaying text in console from file #1 and using another method to write in file#2. I can successfully read and display contents from File#1 but not sure how to write the text in file#2. Here is the code which I have written so far: import java.io.*; public class ReadnWrite { public static void readFile() throws IOException { BufferedReader inputStream = new BufferedReader(new FileReader( "original.txt")); String

java BufferedReader works on windows and not mac

对着背影说爱祢 提交于 2019-12-05 18:59:46
I am trying to read an output from a server using BufferedReader or Scanner but it gets stuck on read() or readLine() objects. It never returns from that line. However, it is working on Windows perfectly. The code to read the output is seen below: while ((serverResponce = this.in.readLine()) != null) { if (serverResponce.compareTo(message) == 0) { break; } } or using Scanner class: Scanner scanner = new Scanner(socket.getInputStream()); while (scanner.hasNextLine()) { serverResponce = scanner.nextLine(); if (serverResponce.compareTo(message) == 0) { break; } } I also tried ready() in