Reputation: 755
BufferedReader.readLine() removes EOL characters automatically, and I cannot simply do a readLine() and then tack a "\r" on the end of it. I tried
InputStream myFile = new FileInputStream("C:\\test.txt");
StringBuilder sb = new StringBuilder();
int i;
while((i = myFile.read()) != -1)
{
char ch = (char) i;
sb.append(ch);
}
System.out.println(sb);
but the "char ch = (char) i" loses byte data because ints are 4 bytes while chars are 2 bytes.
I repeat, I cannot do something like
sb.append(ch+"\r");
because some files that this generic code will read will include the CR and others will not.
From java.nio.*, Files.readAllBytes(Path path) seem like an option. But I am unfamiliar with it and cannot tell if it returns EOL characters or not based off the Javadoc
Upvotes: 0
Views: 669
Reputation: 63945
You ideally don't touch the bytes. E.g.
public static String fromFile(File file, Charset charset) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
StringWriter out = new StringWriter();
char[] cbuf = new char[8192];
int read;
while ((read = reader.read(cbuf)) != -1) {
out.write(cbuf, 0, read);
}
return out.toString();
}
}
Converts everything straight into a single String
. Converting byte
to char
is indeed dangerous and you should not try to do that yourself unless you know it's only ascii. Let the builtin charsets do that. It's tricky enough to use the right one already.
Files.readAllBytes()
does return EOL characters as it works on bytes and does not try to interpret what those bytes mean.
public static String fromPath(Path path, Charset charset) throws IOException {
byte[] bytes = Files.readAllBytes(path);
return new String(bytes, 0, bytes.length, charset);
}
is the equivalent using the nio methods. Call with Paths.get("myfile.txt")
instead of with new File("myfile.txt")
.
Upvotes: 3