#file reader

1 messages ยท Page 1 of 1 (latest)

velvet harness
#

art.txt (The system cannot find the file specified)

livid salmonBOT
#

<@&987246964494204979> please have a look, thanks.

livid salmonBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

velvet harness
#

this is my only file

tired parcel
velvet harness
#

last time it was here

tired parcel
#

yes

velvet harness
tired parcel
#

the file needs to be above src

velvet harness
#

you mean out the folder?

tired parcel
#

yes

#

it's literraly the same problem as before

#

with poem.txt

#

it should be outside src

#

that's all

velvet harness
#

now i have it only in out but it still won t work

velvet harness
proper hollow
#

it needs to be above source

#

now u got it in out\something

velvet harness
#

placed it here now like the last time

tired parcel
#

yes

proper hollow
#

try now

velvet harness
#

but could you guys explain why it won t work cuz i still think it is kinda annoying ๐Ÿ˜…

lone frost
#

what won't work?

proper hollow
#

your 0 location for file is in untitiled

tired parcel
lone frost
#

and NIO

velvet harness
proper hollow
#

and incase its a school traject, keep those names in mind but follow the traject

velvet harness
livid salmonBOT
#

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
velvet harness
proper hollow
#

then use the suggestions, your doing old style

tired parcel
velvet harness
velvet harness
proper hollow
#

use it the way alathreon wrote

#

your using try catch, that's not the same

velvet harness
lone frost
#

compare the placement of the reader declaration

#

in both cases

proper hollow
#

try(FileReader reader = new FileReader("art.txt")) < = even brackets ๐Ÿ˜‰

velvet harness
#

dunno how to close it ^| that way

lone frost
#

there is no need to add the <=

#

and you don't need to close it when using try-with-resources

velvet harness
#

you mean reader.close is useless in this case=

#

?

proper hollow
#

scroll up a bit and read the text the bot pasted

velvet harness
#

sorry just kinda confused and wanna skip as close to the tutorial as i can

proper hollow
#

that's one way of defining where the file is located

velvet harness
#

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

proper hollow
#

u can always place an absolute location , for learning purposes

#

(just make sure u use a forward slash instead of a backward slash)

oblique forge
#

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

velvet harness
#

\ this also fine why does it matter?

#

\

#

\

oblique forge
#

What do you mean?

velvet harness
#

double lol

oblique forge
#

Use a code block

#

\ is the escape character in Discord (and Java too, and many other programs & languages)

velvet harness
oblique forge
#

You could use / instead

velvet harness
velvet harness
oblique forge
#

It doesn't matter which you use

#

Java requires 2 back-slashes because a back-slash is the escape character

velvet harness
#

but tbh i still don t understand why it can t find it in

#

src i mean

proper hollow
#

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

oblique forge
proper hollow
#

yes

velvet harness
#

thx for response went too bed ๐Ÿ˜… gonna close this thread