Getting meaningful text from Java.io.Reader
I have a program that I'm writing where I am using another company's library to download some reports from their website. I want to parse these reports before I write them to a file, because if they match certain criteria, I want to disregard them.
Problem is, their method, called download() returns a java.io.Reader. The only method available to me is
int read(char cbuf);
Printing this returned array out gives me meaningless characters. I want to be able to identify what character set I'm working with or convert it to a byte array but I can't figure out how to do it. I've tried
//retrievedFile is my Reader object
char cbuf = new char[2048];
int numChars = retrievedFile.read(cbuf);
//I've tried other character sets, too
new String(cbuf).getBytes("UTF-8");
and I'm afraid to downcast to a more useful reader because I can't know for sure if it will work or not. Any suggestions?
EDIT
When I say it prints out "meaningless characters", I don't mean that it looks like the example given by Jon Skeet. It's really hard to describe because I'm not at my machine right now, but I think it's an encoding issue. The characters seem to have indentations and structure similar to the look of the reports. I'll try these suggestions as soon as I get back on Tuesday (I'm only an intern, so I haven't bothered with setting up a remote account or anything).
java java-io
add a comment |
I have a program that I'm writing where I am using another company's library to download some reports from their website. I want to parse these reports before I write them to a file, because if they match certain criteria, I want to disregard them.
Problem is, their method, called download() returns a java.io.Reader. The only method available to me is
int read(char cbuf);
Printing this returned array out gives me meaningless characters. I want to be able to identify what character set I'm working with or convert it to a byte array but I can't figure out how to do it. I've tried
//retrievedFile is my Reader object
char cbuf = new char[2048];
int numChars = retrievedFile.read(cbuf);
//I've tried other character sets, too
new String(cbuf).getBytes("UTF-8");
and I'm afraid to downcast to a more useful reader because I can't know for sure if it will work or not. Any suggestions?
EDIT
When I say it prints out "meaningless characters", I don't mean that it looks like the example given by Jon Skeet. It's really hard to describe because I'm not at my machine right now, but I think it's an encoding issue. The characters seem to have indentations and structure similar to the look of the reports. I'll try these suggestions as soon as I get back on Tuesday (I'm only an intern, so I haven't bothered with setting up a remote account or anything).
java java-io
1
Have you triedBufferedReader
? There is no reason why it should not work...
– fge
Dec 30 '11 at 20:49
IfSystem.out.print(cbuf[i])
gives you garbage for i=0, 1, 2.., then the other company's lib has a problem, or you did not configure it well.
– Wolfgang Kuehn
Dec 30 '11 at 21:02
add a comment |
I have a program that I'm writing where I am using another company's library to download some reports from their website. I want to parse these reports before I write them to a file, because if they match certain criteria, I want to disregard them.
Problem is, their method, called download() returns a java.io.Reader. The only method available to me is
int read(char cbuf);
Printing this returned array out gives me meaningless characters. I want to be able to identify what character set I'm working with or convert it to a byte array but I can't figure out how to do it. I've tried
//retrievedFile is my Reader object
char cbuf = new char[2048];
int numChars = retrievedFile.read(cbuf);
//I've tried other character sets, too
new String(cbuf).getBytes("UTF-8");
and I'm afraid to downcast to a more useful reader because I can't know for sure if it will work or not. Any suggestions?
EDIT
When I say it prints out "meaningless characters", I don't mean that it looks like the example given by Jon Skeet. It's really hard to describe because I'm not at my machine right now, but I think it's an encoding issue. The characters seem to have indentations and structure similar to the look of the reports. I'll try these suggestions as soon as I get back on Tuesday (I'm only an intern, so I haven't bothered with setting up a remote account or anything).
java java-io
I have a program that I'm writing where I am using another company's library to download some reports from their website. I want to parse these reports before I write them to a file, because if they match certain criteria, I want to disregard them.
Problem is, their method, called download() returns a java.io.Reader. The only method available to me is
int read(char cbuf);
Printing this returned array out gives me meaningless characters. I want to be able to identify what character set I'm working with or convert it to a byte array but I can't figure out how to do it. I've tried
//retrievedFile is my Reader object
char cbuf = new char[2048];
int numChars = retrievedFile.read(cbuf);
//I've tried other character sets, too
new String(cbuf).getBytes("UTF-8");
and I'm afraid to downcast to a more useful reader because I can't know for sure if it will work or not. Any suggestions?
EDIT
When I say it prints out "meaningless characters", I don't mean that it looks like the example given by Jon Skeet. It's really hard to describe because I'm not at my machine right now, but I think it's an encoding issue. The characters seem to have indentations and structure similar to the look of the reports. I'll try these suggestions as soon as I get back on Tuesday (I'm only an intern, so I haven't bothered with setting up a remote account or anything).
java java-io
java java-io
edited Dec 30 '11 at 23:01
Tom
asked Dec 30 '11 at 20:47
TomTom
4364720
4364720
1
Have you triedBufferedReader
? There is no reason why it should not work...
– fge
Dec 30 '11 at 20:49
IfSystem.out.print(cbuf[i])
gives you garbage for i=0, 1, 2.., then the other company's lib has a problem, or you did not configure it well.
– Wolfgang Kuehn
Dec 30 '11 at 21:02
add a comment |
1
Have you triedBufferedReader
? There is no reason why it should not work...
– fge
Dec 30 '11 at 20:49
IfSystem.out.print(cbuf[i])
gives you garbage for i=0, 1, 2.., then the other company's lib has a problem, or you did not configure it well.
– Wolfgang Kuehn
Dec 30 '11 at 21:02
1
1
Have you tried
BufferedReader
? There is no reason why it should not work...– fge
Dec 30 '11 at 20:49
Have you tried
BufferedReader
? There is no reason why it should not work...– fge
Dec 30 '11 at 20:49
If
System.out.print(cbuf[i])
gives you garbage for i=0, 1, 2.., then the other company's lib has a problem, or you did not configure it well.– Wolfgang Kuehn
Dec 30 '11 at 21:02
If
System.out.print(cbuf[i])
gives you garbage for i=0, 1, 2.., then the other company's lib has a problem, or you did not configure it well.– Wolfgang Kuehn
Dec 30 '11 at 21:02
add a comment |
6 Answers
6
active
oldest
votes
Try this:
BufferedReader in = new BufferedReader(retrievedFile);
String line = null;
StringBuilder rslt = new StringBuilder();
while ((line = in.readLine()) != null) {
rslt.append(line);
}
System.out.println(rslt.toString());
Don't typecast the Reader to any class because you don't know the real type of it.
Instead, use BufferedReader and pass Reader into it. And BufferedReader take any subclass of java.io.Reader as the argument so it is save to use it.
Worked perfectly in my scenario.
– Haseeb Wali
May 22 '13 at 10:25
add a comment |
Printing out the char
itself will probably give you something like:
[C@1c8825a5
That's just the normal output of calling toString
on a char
array in Java. It sounds like you want to convert it into a String
, which you can do with a String(char)
constructor. Here's some sample code:
public class Test {
public static void main(String args) {
char chars = "hello".toCharArray();
System.out.println((Object) chars);
String text = new String(chars);
System.out.println(text);
}
}
On the other hand, java.io.Reader
doesn't have a read
method returning a char
- it has methods which either return a single character at a time, or (more usefully) accept a char
to fill with data, and return the amount of data read. This is actually what your sample code shows. You just need to use the char array and the number of characters read to create the new String
. For example:
char buffer = new char[4096];
int charsRead = reader.read(buffer);
String text = new String(buffer, 0, charsRead);
However, note that it may not return all the data in one go. You could read it line by line using BufferedReader
, or loop to fetch all of the information. Guava contains useful code in its CharStreams
class. For example:
String allText = CharStreams.toString(reader);
or
List<String> lines = CharStreams.readLines(reader);
Jon, my mistake about the method, I forgot it returns an int and takes a char. Printing out the char array looks like an encoding problem. It might even be that the company messed up their implementation, or I have configured it wrong, a la @amadeus's post
– Tom
Dec 30 '11 at 23:04
@Tom: If it's an encoding problem, that could only be because the implementation is messed up - if you're given aReader
, you don't need to take care of the encoding at all. Are you able to give any details of the library?
– Jon Skeet
Dec 30 '11 at 23:35
add a comment |
What meaningless chars does it give. Probably null chars, because you don't read all the chars from the reader, but at most 2048 chars, and you ignore the returned value from the read method (which tell you how many chars were actually read.
If you want to read the whole thing into a String, you'll have to loop until the returned value is negative, and append the chars read at each iteration (from 0 to numChars) to a StringBuilder.
StringBuilder builder = new StringBuilder();
int numChars;
while ((numChars = reader.read(cbuf)) >= 0) {
builder.append(cbuf, 0, numChars);
}
String s = builder.toString();
Example is missing the definition of thecbuf
array. This is the most efficient solution IMO.
– Ben Hutchison
Aug 28 '14 at 2:38
add a comment |
Wrap it in something more useful, like a StringReader or a BufferedReader:
http://docs.oracle.com/javase/6/docs/api/
.
add a comment |
Since the file is a text file create a BufferedReader
from your Reader
and read it line by line - that should help make more sense of it.
add a comment |
As an alternative you can read a string from a java.io.Reader
using java.util.Scanner
using try with resources which should automatically close the reader.
Here is an example:
Reader in = ...
try (Scanner scanner = new Scanner(in).useDelimiter("\Z")) {
String text = scanner.next();
... // Do something with text
}
In this situation the call to scanner.next()
will read all characters, because the delimiter is the end of file.
The following one liner will also read the whole text but will not close the reader:
String text = new Scanner(in).useDelimiter("\Z").next();
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8683943%2fgetting-meaningful-text-from-java-io-reader%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
BufferedReader in = new BufferedReader(retrievedFile);
String line = null;
StringBuilder rslt = new StringBuilder();
while ((line = in.readLine()) != null) {
rslt.append(line);
}
System.out.println(rslt.toString());
Don't typecast the Reader to any class because you don't know the real type of it.
Instead, use BufferedReader and pass Reader into it. And BufferedReader take any subclass of java.io.Reader as the argument so it is save to use it.
Worked perfectly in my scenario.
– Haseeb Wali
May 22 '13 at 10:25
add a comment |
Try this:
BufferedReader in = new BufferedReader(retrievedFile);
String line = null;
StringBuilder rslt = new StringBuilder();
while ((line = in.readLine()) != null) {
rslt.append(line);
}
System.out.println(rslt.toString());
Don't typecast the Reader to any class because you don't know the real type of it.
Instead, use BufferedReader and pass Reader into it. And BufferedReader take any subclass of java.io.Reader as the argument so it is save to use it.
Worked perfectly in my scenario.
– Haseeb Wali
May 22 '13 at 10:25
add a comment |
Try this:
BufferedReader in = new BufferedReader(retrievedFile);
String line = null;
StringBuilder rslt = new StringBuilder();
while ((line = in.readLine()) != null) {
rslt.append(line);
}
System.out.println(rslt.toString());
Don't typecast the Reader to any class because you don't know the real type of it.
Instead, use BufferedReader and pass Reader into it. And BufferedReader take any subclass of java.io.Reader as the argument so it is save to use it.
Try this:
BufferedReader in = new BufferedReader(retrievedFile);
String line = null;
StringBuilder rslt = new StringBuilder();
while ((line = in.readLine()) != null) {
rslt.append(line);
}
System.out.println(rslt.toString());
Don't typecast the Reader to any class because you don't know the real type of it.
Instead, use BufferedReader and pass Reader into it. And BufferedReader take any subclass of java.io.Reader as the argument so it is save to use it.
answered Dec 30 '11 at 21:14
evanwongevanwong
3,98622443
3,98622443
Worked perfectly in my scenario.
– Haseeb Wali
May 22 '13 at 10:25
add a comment |
Worked perfectly in my scenario.
– Haseeb Wali
May 22 '13 at 10:25
Worked perfectly in my scenario.
– Haseeb Wali
May 22 '13 at 10:25
Worked perfectly in my scenario.
– Haseeb Wali
May 22 '13 at 10:25
add a comment |
Printing out the char
itself will probably give you something like:
[C@1c8825a5
That's just the normal output of calling toString
on a char
array in Java. It sounds like you want to convert it into a String
, which you can do with a String(char)
constructor. Here's some sample code:
public class Test {
public static void main(String args) {
char chars = "hello".toCharArray();
System.out.println((Object) chars);
String text = new String(chars);
System.out.println(text);
}
}
On the other hand, java.io.Reader
doesn't have a read
method returning a char
- it has methods which either return a single character at a time, or (more usefully) accept a char
to fill with data, and return the amount of data read. This is actually what your sample code shows. You just need to use the char array and the number of characters read to create the new String
. For example:
char buffer = new char[4096];
int charsRead = reader.read(buffer);
String text = new String(buffer, 0, charsRead);
However, note that it may not return all the data in one go. You could read it line by line using BufferedReader
, or loop to fetch all of the information. Guava contains useful code in its CharStreams
class. For example:
String allText = CharStreams.toString(reader);
or
List<String> lines = CharStreams.readLines(reader);
Jon, my mistake about the method, I forgot it returns an int and takes a char. Printing out the char array looks like an encoding problem. It might even be that the company messed up their implementation, or I have configured it wrong, a la @amadeus's post
– Tom
Dec 30 '11 at 23:04
@Tom: If it's an encoding problem, that could only be because the implementation is messed up - if you're given aReader
, you don't need to take care of the encoding at all. Are you able to give any details of the library?
– Jon Skeet
Dec 30 '11 at 23:35
add a comment |
Printing out the char
itself will probably give you something like:
[C@1c8825a5
That's just the normal output of calling toString
on a char
array in Java. It sounds like you want to convert it into a String
, which you can do with a String(char)
constructor. Here's some sample code:
public class Test {
public static void main(String args) {
char chars = "hello".toCharArray();
System.out.println((Object) chars);
String text = new String(chars);
System.out.println(text);
}
}
On the other hand, java.io.Reader
doesn't have a read
method returning a char
- it has methods which either return a single character at a time, or (more usefully) accept a char
to fill with data, and return the amount of data read. This is actually what your sample code shows. You just need to use the char array and the number of characters read to create the new String
. For example:
char buffer = new char[4096];
int charsRead = reader.read(buffer);
String text = new String(buffer, 0, charsRead);
However, note that it may not return all the data in one go. You could read it line by line using BufferedReader
, or loop to fetch all of the information. Guava contains useful code in its CharStreams
class. For example:
String allText = CharStreams.toString(reader);
or
List<String> lines = CharStreams.readLines(reader);
Jon, my mistake about the method, I forgot it returns an int and takes a char. Printing out the char array looks like an encoding problem. It might even be that the company messed up their implementation, or I have configured it wrong, a la @amadeus's post
– Tom
Dec 30 '11 at 23:04
@Tom: If it's an encoding problem, that could only be because the implementation is messed up - if you're given aReader
, you don't need to take care of the encoding at all. Are you able to give any details of the library?
– Jon Skeet
Dec 30 '11 at 23:35
add a comment |
Printing out the char
itself will probably give you something like:
[C@1c8825a5
That's just the normal output of calling toString
on a char
array in Java. It sounds like you want to convert it into a String
, which you can do with a String(char)
constructor. Here's some sample code:
public class Test {
public static void main(String args) {
char chars = "hello".toCharArray();
System.out.println((Object) chars);
String text = new String(chars);
System.out.println(text);
}
}
On the other hand, java.io.Reader
doesn't have a read
method returning a char
- it has methods which either return a single character at a time, or (more usefully) accept a char
to fill with data, and return the amount of data read. This is actually what your sample code shows. You just need to use the char array and the number of characters read to create the new String
. For example:
char buffer = new char[4096];
int charsRead = reader.read(buffer);
String text = new String(buffer, 0, charsRead);
However, note that it may not return all the data in one go. You could read it line by line using BufferedReader
, or loop to fetch all of the information. Guava contains useful code in its CharStreams
class. For example:
String allText = CharStreams.toString(reader);
or
List<String> lines = CharStreams.readLines(reader);
Printing out the char
itself will probably give you something like:
[C@1c8825a5
That's just the normal output of calling toString
on a char
array in Java. It sounds like you want to convert it into a String
, which you can do with a String(char)
constructor. Here's some sample code:
public class Test {
public static void main(String args) {
char chars = "hello".toCharArray();
System.out.println((Object) chars);
String text = new String(chars);
System.out.println(text);
}
}
On the other hand, java.io.Reader
doesn't have a read
method returning a char
- it has methods which either return a single character at a time, or (more usefully) accept a char
to fill with data, and return the amount of data read. This is actually what your sample code shows. You just need to use the char array and the number of characters read to create the new String
. For example:
char buffer = new char[4096];
int charsRead = reader.read(buffer);
String text = new String(buffer, 0, charsRead);
However, note that it may not return all the data in one go. You could read it line by line using BufferedReader
, or loop to fetch all of the information. Guava contains useful code in its CharStreams
class. For example:
String allText = CharStreams.toString(reader);
or
List<String> lines = CharStreams.readLines(reader);
answered Dec 30 '11 at 21:03
Jon SkeetJon Skeet
1085k68679258428
1085k68679258428
Jon, my mistake about the method, I forgot it returns an int and takes a char. Printing out the char array looks like an encoding problem. It might even be that the company messed up their implementation, or I have configured it wrong, a la @amadeus's post
– Tom
Dec 30 '11 at 23:04
@Tom: If it's an encoding problem, that could only be because the implementation is messed up - if you're given aReader
, you don't need to take care of the encoding at all. Are you able to give any details of the library?
– Jon Skeet
Dec 30 '11 at 23:35
add a comment |
Jon, my mistake about the method, I forgot it returns an int and takes a char. Printing out the char array looks like an encoding problem. It might even be that the company messed up their implementation, or I have configured it wrong, a la @amadeus's post
– Tom
Dec 30 '11 at 23:04
@Tom: If it's an encoding problem, that could only be because the implementation is messed up - if you're given aReader
, you don't need to take care of the encoding at all. Are you able to give any details of the library?
– Jon Skeet
Dec 30 '11 at 23:35
Jon, my mistake about the method, I forgot it returns an int and takes a char. Printing out the char array looks like an encoding problem. It might even be that the company messed up their implementation, or I have configured it wrong, a la @amadeus's post
– Tom
Dec 30 '11 at 23:04
Jon, my mistake about the method, I forgot it returns an int and takes a char. Printing out the char array looks like an encoding problem. It might even be that the company messed up their implementation, or I have configured it wrong, a la @amadeus's post
– Tom
Dec 30 '11 at 23:04
@Tom: If it's an encoding problem, that could only be because the implementation is messed up - if you're given a
Reader
, you don't need to take care of the encoding at all. Are you able to give any details of the library?– Jon Skeet
Dec 30 '11 at 23:35
@Tom: If it's an encoding problem, that could only be because the implementation is messed up - if you're given a
Reader
, you don't need to take care of the encoding at all. Are you able to give any details of the library?– Jon Skeet
Dec 30 '11 at 23:35
add a comment |
What meaningless chars does it give. Probably null chars, because you don't read all the chars from the reader, but at most 2048 chars, and you ignore the returned value from the read method (which tell you how many chars were actually read.
If you want to read the whole thing into a String, you'll have to loop until the returned value is negative, and append the chars read at each iteration (from 0 to numChars) to a StringBuilder.
StringBuilder builder = new StringBuilder();
int numChars;
while ((numChars = reader.read(cbuf)) >= 0) {
builder.append(cbuf, 0, numChars);
}
String s = builder.toString();
Example is missing the definition of thecbuf
array. This is the most efficient solution IMO.
– Ben Hutchison
Aug 28 '14 at 2:38
add a comment |
What meaningless chars does it give. Probably null chars, because you don't read all the chars from the reader, but at most 2048 chars, and you ignore the returned value from the read method (which tell you how many chars were actually read.
If you want to read the whole thing into a String, you'll have to loop until the returned value is negative, and append the chars read at each iteration (from 0 to numChars) to a StringBuilder.
StringBuilder builder = new StringBuilder();
int numChars;
while ((numChars = reader.read(cbuf)) >= 0) {
builder.append(cbuf, 0, numChars);
}
String s = builder.toString();
Example is missing the definition of thecbuf
array. This is the most efficient solution IMO.
– Ben Hutchison
Aug 28 '14 at 2:38
add a comment |
What meaningless chars does it give. Probably null chars, because you don't read all the chars from the reader, but at most 2048 chars, and you ignore the returned value from the read method (which tell you how many chars were actually read.
If you want to read the whole thing into a String, you'll have to loop until the returned value is negative, and append the chars read at each iteration (from 0 to numChars) to a StringBuilder.
StringBuilder builder = new StringBuilder();
int numChars;
while ((numChars = reader.read(cbuf)) >= 0) {
builder.append(cbuf, 0, numChars);
}
String s = builder.toString();
What meaningless chars does it give. Probably null chars, because you don't read all the chars from the reader, but at most 2048 chars, and you ignore the returned value from the read method (which tell you how many chars were actually read.
If you want to read the whole thing into a String, you'll have to loop until the returned value is negative, and append the chars read at each iteration (from 0 to numChars) to a StringBuilder.
StringBuilder builder = new StringBuilder();
int numChars;
while ((numChars = reader.read(cbuf)) >= 0) {
builder.append(cbuf, 0, numChars);
}
String s = builder.toString();
answered Dec 30 '11 at 20:54
JB NizetJB Nizet
539k568741004
539k568741004
Example is missing the definition of thecbuf
array. This is the most efficient solution IMO.
– Ben Hutchison
Aug 28 '14 at 2:38
add a comment |
Example is missing the definition of thecbuf
array. This is the most efficient solution IMO.
– Ben Hutchison
Aug 28 '14 at 2:38
Example is missing the definition of the
cbuf
array. This is the most efficient solution IMO.– Ben Hutchison
Aug 28 '14 at 2:38
Example is missing the definition of the
cbuf
array. This is the most efficient solution IMO.– Ben Hutchison
Aug 28 '14 at 2:38
add a comment |
Wrap it in something more useful, like a StringReader or a BufferedReader:
http://docs.oracle.com/javase/6/docs/api/
.
add a comment |
Wrap it in something more useful, like a StringReader or a BufferedReader:
http://docs.oracle.com/javase/6/docs/api/
.
add a comment |
Wrap it in something more useful, like a StringReader or a BufferedReader:
http://docs.oracle.com/javase/6/docs/api/
.
Wrap it in something more useful, like a StringReader or a BufferedReader:
http://docs.oracle.com/javase/6/docs/api/
.
answered Dec 30 '11 at 20:50
duffymoduffymo
270k33316505
270k33316505
add a comment |
add a comment |
Since the file is a text file create a BufferedReader
from your Reader
and read it line by line - that should help make more sense of it.
add a comment |
Since the file is a text file create a BufferedReader
from your Reader
and read it line by line - that should help make more sense of it.
add a comment |
Since the file is a text file create a BufferedReader
from your Reader
and read it line by line - that should help make more sense of it.
Since the file is a text file create a BufferedReader
from your Reader
and read it line by line - that should help make more sense of it.
answered Dec 30 '11 at 20:50
PaulPaul
14.8k106086
14.8k106086
add a comment |
add a comment |
As an alternative you can read a string from a java.io.Reader
using java.util.Scanner
using try with resources which should automatically close the reader.
Here is an example:
Reader in = ...
try (Scanner scanner = new Scanner(in).useDelimiter("\Z")) {
String text = scanner.next();
... // Do something with text
}
In this situation the call to scanner.next()
will read all characters, because the delimiter is the end of file.
The following one liner will also read the whole text but will not close the reader:
String text = new Scanner(in).useDelimiter("\Z").next();
add a comment |
As an alternative you can read a string from a java.io.Reader
using java.util.Scanner
using try with resources which should automatically close the reader.
Here is an example:
Reader in = ...
try (Scanner scanner = new Scanner(in).useDelimiter("\Z")) {
String text = scanner.next();
... // Do something with text
}
In this situation the call to scanner.next()
will read all characters, because the delimiter is the end of file.
The following one liner will also read the whole text but will not close the reader:
String text = new Scanner(in).useDelimiter("\Z").next();
add a comment |
As an alternative you can read a string from a java.io.Reader
using java.util.Scanner
using try with resources which should automatically close the reader.
Here is an example:
Reader in = ...
try (Scanner scanner = new Scanner(in).useDelimiter("\Z")) {
String text = scanner.next();
... // Do something with text
}
In this situation the call to scanner.next()
will read all characters, because the delimiter is the end of file.
The following one liner will also read the whole text but will not close the reader:
String text = new Scanner(in).useDelimiter("\Z").next();
As an alternative you can read a string from a java.io.Reader
using java.util.Scanner
using try with resources which should automatically close the reader.
Here is an example:
Reader in = ...
try (Scanner scanner = new Scanner(in).useDelimiter("\Z")) {
String text = scanner.next();
... // Do something with text
}
In this situation the call to scanner.next()
will read all characters, because the delimiter is the end of file.
The following one liner will also read the whole text but will not close the reader:
String text = new Scanner(in).useDelimiter("\Z").next();
edited Nov 14 '18 at 11:54
answered Nov 13 '18 at 14:19
gil.fernandesgil.fernandes
5,60121435
5,60121435
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f8683943%2fgetting-meaningful-text-from-java-io-reader%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you tried
BufferedReader
? There is no reason why it should not work...– fge
Dec 30 '11 at 20:49
If
System.out.print(cbuf[i])
gives you garbage for i=0, 1, 2.., then the other company's lib has a problem, or you did not configure it well.– Wolfgang Kuehn
Dec 30 '11 at 21:02