#Would this way of writing to a file work?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
Here, what's the use of BufferWriter? I was under the impression that just the Outputstreamwriter would be enough? Isn't this like a writer of its own which does the same thing? Can anyone confirm that to me?
Isn't it redundant to add BufferedWriter, in other words?
File IO in Java should be done preferably with NIO (Java 7+), revolving around the classes Files and Path; and not with the old interface File, BufferedReader, FileReader and similar.
NIO is simple to use. The path to a file is represented using the Path class:
Path path = Path.of("myFile.txt");
All file operations can be found in the Files class:
// Reading
List<String> allLines = Files.readAllLines(path);
// or as a single string
String content = Files.readString(path);
// or with a stream
try (Stream<String> stream = Files.lines(path)) {
stream.forEach(System.out::println);
}
// Writing
Files.write(path, lines);
// or as a single string
Files.writeString(path, "hello world");
// or with extra options
Files.writeString(path, "hello world",
StandardOpenOption.WRITE,
StandardOpenOption.CREATE,
StandardOpenOption.APPEND);
If you need more control over the process, you can fallback to the old interface, but prefer using the bridge methods from NIO (Files.newBufferedReader, Files.newInputStream, path.toFile() and similar) to benefit from advantages such as correct encoding and better error detection.
Here is a simple example of how to read a file line-wise with the old interface
try (BufferedReader br = Files.newBufferedReader(path)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
it is way more verbose than NIO but it gives more control.
You must not forget to close file handles, even in all exceptional cases. Closing a handle manually is very hard, which is why you should always use try-with-resources for this to let Java automatically close the handle for you:
try (SomeResource resource = ...) {
...
} // Automatically closed here, even in exceptional cases
@next warren Unfortunately, I have to understand specifically what I'm given, regardless of my own personnal preferences
BuffereWriter is a decorator class for any other Writer which has an internal buffer
The reason it is used is because otherwise every write operation would incur an IO operation
With a buffer, their amount is reduced and you write more data at once
Since they only happen when the buffer is full
Sorry, can you refomulate this for someone with less than 1 year of programming experience
A buffer I know is a sort of memory holder
temporary memory holder
oh
fewer times file writing - fewer slow
said this way, it makes sense now. The very act of writing takes time
is this right
Yes, it essentially makes no difference whether it's a byte or 4 kilobytes
Not on this scale, at least
It's the file access itself which is slow
you mean, it would still take take the same amount of time regardless of what we write
Yes
And by writing bigger chunks you make fewer file accesses
BufferWriter is what is the "buffer" which makes writing each character less frequent by holding several of them at the same time and shipping them off to the stream?
Ok, I get it now, I assume it's the same way when we do reading from a file, it takes time as well
you mean to close the stream?
Closing as well, but I mean flushing
what is flushing
ok, i think closing will be enoughin my case lol
Flushing is writing any remaining data that's in the buffer
it would yes, flushing is useless when you are closing
it would be enough
closing it would flush everything left
Ok, thanks for confirming it, btw, what was just said for writing holds for reading as well right, the very act of reading takes time etc
Although in theory, would it be possible to just write directy without the Writer? As in, you would get no problem I assume? (outside of slowness)
Yeah
Yeah
Just know the implications
Note that this is a general rule about computers, io/memory access costs time, tho the time can be different depending of the type of memory/io, and so, there is a lot of caches or buffers to fix that
Yes, I learned a bit about buffers
some basic stuff
ok, thanks for confirming it. I jsut remembered that Outputstreamwriter does character by character, as opposed to whole lines
so I can imagine it would be quite problematic