sentence
stringlengths
1
1.38k
label
stringclasses
3 values
That code will normalize line breaks, which may or may not be what you really want to do.
o
Here's an alternative which doesn't do that, and which is (IMO) simpler to understand than the NIO code (although it still uses java.nio.charset.Charset): CODESNIPPET_JAVA1 .
p
Which one is "that" code?
o
The code in the question.
o
No real need to close the BufferedReader/InputStreamReader as they're only wrapping the stream +1 !!
o
!.
o
Forgive me for reviving a comment this old, but did you mean to pass in a String object called "file", or should that be a File object instead?
o
@BryanLarson: Either's fine.
o
CODESNIPPET_JAVA1 .
o
....... CODESNIPPET_JAVA2 .
o
Or even more simple: new String(Files.readAllBytes(FileSystems.getDefault().getPath( filename)));.
p
or new String(Files.readAllBytes(Paths.get(filename))); :-).
o
Well played, and to save the next guy the Googling, [Paths is apparently 1.7+]( URL_http://docs.oracle.com/javase/7/docs/api/java/nio/file/Paths.html ) as is [FileSystems]( URL_http://docs.oracle.com/javase/7/docs/api/java/nio/file/FileSys tems.html).
p
(Dang it!
o
).
o
It;s a shame this answer doesn't have more votes.
n
I was looking for the quickest and simplest way to get a text file into a String.
p
This is it and if I didn't scroll down and down and down, I would have missed it.
o
The OP should consider accepting this answer to move it to the top.
o
If it's a text file why not use apache commons-io?
o
URL_http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html [ URL_http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html ] It has the following method CODESNIPPET_JAVA1 .
o
If you want the lines as a list use CODESNIPPET_JAVA2 .
o
Java attempts to be extremely general and flexible in all it does.
p
As a result, something which is relatively simple in a scripting language (your code would be replaced with " CODETERM1 " in python) is a lot more complicated.
p
There doesn't seem to be any shorter way of doing it, except using an external library (like URL_http://stackoverflow.com/questions/326390 /is-there-an-alternative-to-this-way-of-read-file-to-a-string-in-java#326413 [Willi-aus-Rohr] mentioned).
o
Your options: Use an external library.
o
Copy this code into all your projects.
o
Create your own mini-library which contains functions you use often.
o
Your best bet is probably the 2nd one, as it has the least dependencies.
p
Yeap.
o
It makes the "high" level language take a different meaning.
o
Java is high level compared with C but low compared with Python or Ruby.
n
Agree that Java is long on high-level abstractions but short on convenience methods.
n
True, Java has an insane number of ways of dealing with Files and many of them seem complicated.
n
But this is fairly close to what we have in higher level languages: byte[] bytes Files.readAllBytes(someFile.toPath());.
o
CODESNIPPET_JAVA1 .
o
I think this has the inconvenience os using the platform default encoding.
n
+1 anyway :).
o
I seems to me that the finally block does not know variables defined in the try block.
o
javac 1.6.0_21 throws the error cannot find symbol.
o
To read a File as binary and convert at the end CODESNIPPET_JAVA1 .
o
There is a variation on the same theme that uses a for loop, instead of a while loop, to limit the scope of the line variable.
o
Whether it's "better" is a matter of personal taste.
o
CODESNIPPET_JAVA1 .
o
This will change the newlines to the default newline choise.
o
This may be desirable, or unintended.
p
Rolled back the edit to this answer because the point was to narrow the scope of the line variable.
o
The edit declared it twice, which would be a compile error.
o
Be aware when using CODETERM1 the returned integer does not have to represent the actual file size, but rather the guessed amount of bytes the system should be able to read from the stream without blocking IO.
o
A safe and simple way could look like this CODESNIPPET_JAVA1 .
p
It should be considered that this approach is suitable for multi-byte character encodings like UTF-8.
o
This code may give unpredictable results.
o
According to the [documentation](htt p://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#available%28%29 ) of the available() method, there is no guarantee that the end of file is reached in the event that the method returns 0.
o
In that case you might end up with an incomplete file.
o
What's worse, the number of bytes actually read can be smaller than the value returned by available(), in which case you get corrupted output.
o
A flexible solution using URL_http://commons.apache.org/io/apidocs/org/apache/commons/io/IOUtils.html [IOUtils] from Apache URL_http://commons.apache.org/io/ [commons-io] in combination with URL_http://docs.oracle.com/javase/6/docs/api/java/io/StringWriter.html [StringWriter] : CODESNIPPET_JAVA1 .
p
It works with any reader or input stream (not just with files), for example when reading from a URL.
p
One Line Solution CODESNIPPET_JAVA1 .
o
The original question was creating a single string, not a list of lines.
o
This one uses the method CODETERM1 .
o
CODESNIPPET_JAVA1 .
o
I cannot comment other entries yet, so I'll just leave it here.
o
One of best answers here ( URL_http://stackoverflow.com/a/326448/1521167 [ URL_http://stackoverflow.com/a/326448/1521167 ] ): CODESNIPPET_JAVA1 .
p
still has one flaw.
o
It always puts new line char in the end of string, which may cause some weirds bugs.
n
My suggestion is to change it to: CODESNIPPET_JAVA2 .
o
You could try: CODESNIPPET_JAVA1 .
o
I'm not sure what problems might occur with the bytes and character sets etc, but it works for me.
p
I have always wondered, is it possible that input.available() return less bytes count than those in the file; I guess with big files.
o
Probably.
o
Like I said, it works for me.
p
Perhaps there is a more suitable java.io.File method (something like getLength()?
o
) which could provide a more reliable value.
o
Given the doco for read(byte[]) this is very risky code.
o
What if the file is on a network share or SAN?
o
Then you might get an available count of less than the file size.
o
You need to use a readFull() method and File.length().
o
Like I said, it works for me and my purposes.
p
It is concise and I've yet to encounter a problem with it.
o
That said, there are probably a hundred better ways to do it (just, this method has less code).
o
Actually this is very similar the way the library posted by Willi aus Rohr.
o
Don't see why the downvote.
n
Problems with this code: 1) Stream is left open when there's an exception.
o
2) Use of available() to guess at file size (and assume it's constant).
o
3) Assumption that a single call to read() will read everything.
o
4) Use of platform-default character encoding.
o
In short, please don't do this :).
o
I agree with Jon on all points.
o
No offense intended, but this is like a checklist of what NOT to do.
o
Cry me a river, I'll do it this way all the time just to spite you all.
n
Joking aside, I've NEVER had a problem with it so I'm just going to wave my hands like I just don't care (because I don't).
o
Java GUI frameworks.
o
What to choose?
o
Swing, SWT, AWT, SwingX, JGoodies, JavaFX, Apache Pivot?
o
There is quite a lot of gui frameworks out there for java, but what is recognized as today's framework of choice?
o
The following is my understanding of the different frameworks, please correct me if im wrong.
n
This is a very loosely defined set of questions, but i still think its valuable for anyone thinking of creating rich gui applications.
o
URL_http://en.wikipedia.org/wiki/Java_AWT [AWT] Is the very foundation of swing, it performs well but is lacking in advanced components.
n
If you intend to create rich applications, AWT is probably not the way to go.
o
However for smaller gui applications that doesn't require rich user interfaces.
o