Convert abstract file to string in Java

How do I switch to converting an abstract file path (file type) to a string type?

+3
source share
5 answers

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

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

+7
source
+4
source
+1

:

String FilepathAsString = fileobject.getAbsolutePath();
Import required : java.io.File;     
+1

, google guava,

  String st = Files.toString(file, Charsets.UTF-8);

. Javadoc.

Of course, this requires that you know the character set of the file. Files contain bytes, strings are characters, and there are always errors when converting between them, unless you really know what the bytes in your file mean.

0
source

All Articles