Anonymous asked in Computers & InternetProgramming & Design · 1 month ago

What is the best way to read a file and store its contents in Java?


I am trying to read in a text file and store it in a string. 

1 Answer

  • 1 month ago

    If you're talking about a named file in a file system, that file already stores its contents.  That's what a file system does.

    If you want to copy the contents into another file, the best method is probably to use the Copy method in the java.nio.Files class.  If you haven't learned about NIO and NIO2, the best way to copy a file using classic Java classes is to read the original with a object and write the copy using a object.  Don't use any of the Reader or Writer classes unless you know for certain that the file is a text file, containing only ASCII or Unicode characters.

    If you have another idea about what "store its contents" means to you, then you might want to add details to your question saying what that is.

Still have questions? Get your answers by asking now.