#file reader
1 messages ยท Page 1 of 1 (latest)
<@&987246964494204979> 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.
same as before, not src but the directory above
last time it was here
yes
the file needs to be above src
you mean out the folder?
yes
it's literraly the same problem as before
with poem.txt
it should be outside src
that's all
now i have it only in out but it still won t work
so what is the problem with this cuz it is outside src
yes
try now
but could you guys explain why it won t work cuz i still think it is kinda annoying ๐
what won't work?
wdym, it work here no ?
your 0 location for file is in untitiled
Also please use a try-with-resources
and NIO
wdym by that tought you meant my question had to be more detailed
and incase its a school traject, keep those names in mind but follow the traject
whats that?
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
just learning no schoolproject
then use the suggestions, your doing old style
try(FileReader reader = new FileReader("art.txt")) {
...
}
so just 1 more )๐
ig so just watching some yt
but were does the ) get compensated?
try(FileReader reader = new FileReader("art.txt")) < = even brackets ๐
dunno how to close it ^| that way
there is no need to add the <=
and you don't need to close it when using try-with-resources
scroll up a bit and read the text the bot pasted
sorry just kinda confused and wanna skip as close to the tutorial as i can
that's one way of defining where the file is located
yeah but that is alr ok in my file right?
but if i wanna creat a txt file in intellij i have to be carefull that i don t place it in a folder untitled
u can always place an absolute location , for learning purposes
(just make sure u use a forward slash instead of a backward slash)
You just have to make sure the path you use matches where the file is located
You can have the file be in whatever folder you want. You just have to make sure the path you use in code matches the path of where the file is
What do you mean?
double lol
Use a code block
\ is the escape character in Discord (and Java too, and many other programs & languages)
You could use / instead
like this also fine you can do both ways
why?
It doesn't matter which you use
Java requires 2 back-slashes because a back-slash is the escape character
used \ anyway from python ๐
but tbh i still don t understand why it can t find it in
src i mean
cause src is not the base folder
your start folder is the folder above src
basicly your root
if u tell it to use /src/file.something it will use that
if u use file.something without path, it will look in your project folder
The "working directory" of a project is the project folder itself. Relative paths start at the working directory
yes
thx for response went too bed ๐ gonna close this thread