read complete file without using loop in java

后端 未结 5 1869
长情又很酷
长情又很酷 2020-11-28 05:05

Possible Duplicate:
How to create a Java String from the contents of a file
Whole text file to a String in Java

5条回答
  •  [愿得一人]
    2020-11-28 05:39

    If the file is small, you can read the whole data once:

    File file = new File("a.txt");
    FileInputStream fis = new FileInputStream(file);
    byte[] data = new byte[(int) file.length()];
    fis.read(data);
    fis.close();
    
    String str = new String(data, "UTF-8");
    

提交回复
热议问题