从文件中读取内容

小灰灰 2022-08-12 00:20 285阅读 0赞
  1. package com.zhiru;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. public class ReadFromFile {
  8. private String fName;
  9. ReadFromFile(String s) {
  10. fName = s;
  11. }
  12. public String read() throws IOException {
  13. if (fName != null) {
  14. File f = new File(fName);
  15. InputStream in = new FileInputStream(f);
  16. byte[] b = new byte[(int) f.length()];
  17. int len = in.read(b);
  18. in.close();
  19. return new String(b, 0, len);
  20. }
  21. return null;
  22. }
  23. }

发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读