1. @SuppressWarnings("all")
    2. public class ByteStreamMax {
    3. FileOutputStream fileOutputStream = null;
    4. FileInputStream fileInputStream = null;
    5. //打印文件内容
    6. public void inPut() throws IOException {
    7. File file = new File("D:\\IdeaProjects\\javase\\src\\test.txt");
    8. fileInputStream = new FileInputStream(file);
    9. byte[] bytes = new byte[1024];
    10. String str = "";
    11. while (fileInputStream.read(bytes) != -1) {
    12. str = new String(bytes, "utf-8");
    13. System.out.print(str);
    14. }
    15. System.out.println("\n");
    16. fileInputStream.close();
    17. }
    18. //修改文件内容
    19. public void outPut() throws IOException {
    20. File file1 = new File("D:\\IdeaProjects\\javase\\src\\test.txt");
    21. File file2 = new File("D:\\IdeaProjects\\javase\\src\\IOStream2");
    22. //读取file2的内容
    23. fileInputStream = new FileInputStream(file2);
    24. //将file2的内容写入到file1中
    25. fileOutputStream = new FileOutputStream(file1);
    26. byte[] bytes = new byte[1024];
    27. int len = 0;
    28. while ((len = fileInputStream.read(bytes)) != -1) {
    29. fileOutputStream.write(bytes, 0, len);
    30. }
    31. fileInputStream.close();
    32. fileOutputStream.flush();
    33. fileOutputStream.close();
    34. }
    35. public static void main(String[] args) throws IOException {
    36. ByteStreamMax byteStreamMax = new ByteStreamMax();
    37. //打印文件内容
    38. byteStreamMax.inPut();
    39. //修改文件内容
    40. byteStreamMax.outPut();
    41. //再次打印文件内容,检测是否已修改
    42. byteStreamMax.inPut();
    43. }
    44. }