Search code examples
javafile

Converting an abstract File to String in Java


How would I go about converting an abstract File path (of File type) into a String type?


Solution

  • File.getPath() will give you the path as a String.

    Edit:

    The modern way to get the contents of the file as a String is Files.readString(path, charset), introduced in Java 11.


    If you want the contents of the file, use either of