FileReaderTest.java 798 B

1234567891011121314151617181920212223242526272829303132
  1. package course;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. /**
  6. * @author WanJl
  7. * @version 1.0
  8. * @title FileReaderTest
  9. * @description
  10. * @create 2026/8/7
  11. */
  12. public class FileReaderTest {
  13. public static void main(String[] args) throws IOException {
  14. FileReader fr=new FileReader("D:/abc.txt");
  15. //read () 读取一个字符
  16. // int ch;
  17. // while ((ch=fr.read())!=-1){
  18. // System.out.print((char) ch);
  19. // }
  20. //read (char[] c, int off, int len) 读取一部分字符数组
  21. char[] chars=new char[1024];
  22. int count;
  23. while ((count=fr.read(chars))!=-1){
  24. System.out.print(new String(chars,0,count));
  25. }
  26. //释放资源
  27. fr.close();
  28. }
  29. }