Read all data from socket - java

I want read all data ,synchronously , receive from client or server without readline() method in java(like readall() in c++).
I don't want use something like code below:
BufferedReader reader = new BufferedReader(new inputStreamReader(socket.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null)
document.append(line + "\n");
What method should i use?

If you know the size of incoming data you could use a method like :
public int read(char cbuf[], int off, int len) throws IOException;
where cbuf is Destination buffer.
Otherwise, you'll have to read lines or read bytes. Streams aren't aware of the size of incoming data. The can only sequentially read until end is reached (read method returns -1)
refer here streams doc
sth like that:
public static String readAll(Socket socket) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");
return sb.toString();
}

You could use something like this:
public static String readToEnd(InputStream in) throws IOException {
byte[] b = new byte[1024];
int n;
StringBuilder sb = new StringBuilder();
while ((n = in.read(b)) >= 0) {
sb.append(b);
}
return sb.toString();
}

try this
public static String readToEnd(InputStream in) throws IOException {
return new String(in.readAllBytes(),StandardCharsets.UTF_8);
}

Related

Reading twice an InputStream throws IOException

I'm facing a problem, can someone tell me how to void this? It throws up an "java.io.IOException: Stream closed". I know where my mistake is but I dont know how to fix it. BufferedReader closes from the first function and I dont know how to reset it within the second one. Function should format text from one text file to another with tabs. Thank you
import java.io.*;
public class TestClass {
private void prosekStudentKRS(FileReader fr) throws IOException{
BufferedReader reader = null;
int j = 0, vksum = 0;
try{
reader = new BufferedReader(fr);
reader.readLine();
String line;
while((line = reader.readLine()) != null) {
int sum = 0;
j++;
String[] niza = line.split(",");
for(int i = 1; i < niza.length; i ++) {
sum+=Integer.parseInt(niza[i]);
}
vksum += Integer.parseInt(niza[1]);
System.out.printf("Student %d ima prosek %.2f\n", j, (float) sum / 3);
}
System.out.println("Prosek po KRS: " + vksum / (double) j);
} catch (Exception e){
e.printStackTrace();
} finally {
if(reader != null) {
reader.close();
}
}
}
private void TSV(FileReader fr, FileWriter fw) throws IOException {
BufferedReader reader = null;
BufferedWriter writer = null;
StringBuilder sb = null;
try {
reader = new BufferedReader(fr);
writer = new BufferedWriter(fw);
String line;
while ((line = reader.readLine()) != null) {
String[] niza = line.split(",");
sb = new StringBuilder();
for(int i = 0; i < niza.length; i ++) {
sb.append(niza[i] + "\t");
}
writer.write(sb.toString());
writer.newLine();
writer.flush();
}
} finally {
if (reader != null)
reader.close();
if(writer != null) {
writer.flush();
writer.close();
}
}
}
public static void main(String[] args) throws IOException{
FileReader fr = new FileReader("C:\\Users\\pc\\IdeaProjects\\LabOS01\\rezultaticsv.txt");
FileWriter fw = new FileWriter("C:\\Users\\pc\\IdeaProjects\\LabOS01\\rezultatitsv.txt");
TestClass filetest = new TestClass();
filetest.prosekStudentKRS(fr);
filetest.TSV(fr, fw);
}
}
Either create two distinct FileReader objects and pass a different to each method.
Otherwise you can also create a BufferedReader from the FileReader before invoking the methods, pass it to the first method, reset it with the reset() method and pass it to the other method.
As alternative if the file is not too big, you could store in a List<String> each line read rather than reading again the file.
It would be more efficient.

Read a line of a file using an InputStream

I'm required due to previous implementation to use an InputStream, I can't use a BufferedReader.
My test bench used a BufferedReader and a while loop, like so:
while ((line = br.readLine()) != null)
However br is now required to be an InputStream (and will be renamed). Is there any way of reading in this fashion with an InputStream, or do I have to read it bytes at a time and search for the \n?
If you must read with an InputStream, then wrap it into a InputStreamReader, and then wrap this in a BufferedReader, allowing you to use your familiar BufferedReader methods. There's no need to do non-buffered input in this situation.
// assuming that you have an InputStream named inputStream
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line = null;
while((line = br.readLine()) != null) {
// use line here
}
} catch (IOException e) {
e.printStackTrace();
}
Or alternatively, wrap the InputStream in a Scanner object:
try (Scanner scanner = new Scanner(inputStream)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// use line here
}
}
You can change the code like this :
public String readLine(InputStream inputStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int r;
for (r = inputStream.read(); r != '\n' && r != -1 ; r = inputStream.read()) {
baos.write(r);
}
if (r == -1 && baos.size() == 0) {
return null;
}
String lines = baos.toString("UTF-8");
return lines;
}
Maybe this example helps you..

Reading an InputStream in Java

I'm new to Java thus the question,
I'm using the following class to read a file into a string.
public class Reader {
public static String readFile(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
} finally {
br.close();
}
}
}
How can I modify the method signature of read to read a InputStream as opposed to a string.
Remove the String argument and create an argument of type InputStream. Pass this argument to the constructor of an InputStreamReader and this InputStreamReader can be passed to the constructor of your BufferedReader.
public static String readFile(InputStream is) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(is));
.
.
.
}
Maybee you want to try a try-with-resource statement. Then you can remove the final block. It looks like this.
public static String readFile(InputStream is) throws IOException
{
try (BufferedReader br = new BufferedReader(new InputStreamReader(is)))
{
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null)
{
sb.append(line);
sb.append("\n");
line = br.readLine();
}
return sb.toString();
}
}
If it is not for educational purposes, don't do this manually. E.g. you could use IOUtils.toString from Apache Commons.

java: how to use bufferedreader to read specific line

Lets say I have a text file called: data.txt (contains 2000 lines)
How do I read given specific line from: 500-1500 and then 1500-2000
and display the output of specific line?
this code will read whole files (2000 line)
public static String getContents(File aFile) {
StringBuffer contents = new StringBuffer();
try {
BufferedReader input = new BufferedReader(new FileReader(aFile));
try {
String line = null;
while (( line = input.readLine()) != null){
contents.append(line);
contents.append(System.getProperty("line.separator"));
}
}
finally {
input.close();
}
}
catch (IOException ex){
ex.printStackTrace();
}
return contents.toString();
}
How do I modify above code to read specific line?
I suggest java.io.LineNumberReader. It extends BufferedReader and
you can use its LineNumberReader.getLineNumber(); to get the current line number
You can also use Java 7 java.nio.file.Files.readAllLines which returns a List<String> if it suits you better
Note:
1) favour StringBuilder over StringBuffer, StringBuffer is just a legacy class
2) contents.append(System.getProperty("line.separator")) does not look nice
use contents.append(File.separator) instead
3) Catching exception seems irrelevant, I would also suggest to change your code as
public static String getContents(File aFile) throws IOException {
BufferedReader rdr = new BufferedReader(new FileReader("aFile"));
try {
StringBuilder sb = new StringBuilder();
// read your lines
return sb.toString();
} finally {
rdr.close();
}
}
now code looks cleaner in my view. And if you are in Java 7 use try-with-resources
try (BufferedReader rdr = new BufferedReader(new FileReader("aFile"))) {
StringBuilder sb = new StringBuilder();
// read your lines
return sb.toString();
}
so finally your code could look like
public static String[] getContents(File aFile) throws IOException {
try (LineNumberReader rdr = new LineNumberReader(new FileReader(aFile))) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for (String line = null; (line = rdr.readLine()) != null;) {
if (rdr.getLineNumber() >= 1500) {
sb2.append(line).append(File.pathSeparatorChar);
} else if (rdr.getLineNumber() > 500) {
sb1.append(line).append(File.pathSeparatorChar);
}
}
return new String[] { sb1.toString(), sb2.toString() };
}
}
Note that it returns 2 strings 500-1499 and 1500-2000
A slightly more cleaner solution would be to use FileUtils in apache commons.
http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html
Example snippet:
String line = FileUtils.readLines(aFile).get(lineNumber);
The better way is to use BufferedReader. If you want to read line 32 for example:
for(int x = 0; x < 32; x++){
buf.readLine();
}
lineThreeTwo = buf.readLine();
Now in String lineThreeTwo you have stored line 32.

Java Text Input: How to ignore lines starting with certain characters in them?

I basically want to ignore certain lines with characters in them, like if there's a line
// hello, i'm bill
I want to ignore that line while reading it because it contains the character "//". How can I do that? I tried method skip(), but it gives me errors.
public String[] OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int i;
for (i=0; i<numberOfLines; i++) {
textData[i] = textReader.readLine();
}
// close the line-by-line reader and return the data
textReader.close();
return textData;
}
int readLines() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
String line;
int numberOfLines = 0;
while ((line = textReader.readLine()) != null) {
// I tried this:
if (line.contains("//")) {
line.skip();
}
numberOfLines++;
}
reader.close();
return numberOfLines;
}
Update: HERE's MY MAIN METHOD:
try{
ReadFile files = new ReadFile(file.getPath());
String[] anyLines = files.OpenFile();
}
while ((line = textReader.readLine()) != null) {
// I tried this:
if (line.contains("//")) {
continue;
}
numberOfLines++;
}
note that continue might seem a bit goto like and be prone to critique
edit here's what you are after (note this doesn't need the countLines method)
public String[] OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
List<String> textData = new LinkedList<String>();//linked list to avoid realloc
String line;
while ((line = textReader.readLine()) != null) {
if (!line.contains("//")) textData.add(line);
}
// close the line-by-line reader and return the data
textReader.close();
return textData.toArray(new String[textData.size()]);
}
As Andrew Thompson points out, it would be best to read the file line by line into an ArrayList. Pseudo-Code:
For Each Line In File
If LineIsValid()
AddLineToArrayList()
Next
UPDATE to fix your actual code:
public String[] OpenFile() throws IOException {
FileReader reader = new FileReader(path);
BufferedReader textReader = new BufferedReader(reader);
int numberOfLines = readLines();
String[] textData = new String[numberOfLines];
int BufferIndex = 0;
String line;
while ((line = textReader.readLine()) != null) {
if (line.trim().startsWith("//")) {
// Don't inject current line into buffer
}else{
textData[BufferIndex] = textReader.readLine();
BufferIndex = BufferIndex + 1;
}
}
// close the line-by-line reader and return the data
textReader.close();
return textData;
}
In your ReadLines() Function:
while ((line = textReader.readLine()) != null) {
if (line.trim().startsWith("//")) {
// do nothing
}else{
numberOfLines++;
}
}
Basically, you're on the right track.
Note: You may be interested in the startsWith() string function

Categories