开发者

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?


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

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

  • IOUtils.toString(InputStream,Charset) from Apache Commons-IO
  • Files.toString(File,Charset) from Google Guava.


Use File.getAbsolutePath().


check this link Abstarct path to String as path

check the method getPath()


The simplest way to get file path as a string is as below code:

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


If you want to load the contents of the file into a String, using google guava it's a case of

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

see the Javadoc.

Of course this does require you to know the character set of the file. Files contain bytes, strings are characters, and there always end up being bugs when converting between the two if you don't actually know what the bytes in your file mean.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜