Test1.java 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package com.sf._02_字节流输出流;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.util.Scanner;
  6. public class Test1 {
  7. /**
  8. * 要求在控制台当中录入你的
  9. * 姓名: fanjialong
  10. * 年龄: xx
  11. * 身高: xx
  12. * 体重: xx
  13. * 注意: 每录入完信息就要信息追加到文件中
  14. */
  15. public static void main(String[] args) throws IOException {
  16. // 创建文件删除流写内容
  17. FileOutputStream fileOutputStream = new FileOutputStream(new File("D:\\a.txt"),true);
  18. Scanner scanner = new Scanner(System.in);
  19. System.out.println("请录入你的姓名");
  20. String name = scanner.next();
  21. // 写入姓名
  22. fileOutputStream.write(("姓名:"+name).getBytes());
  23. // 写换行符
  24. fileOutputStream.write(System.lineSeparator().getBytes());
  25. System.out.println("请录入你的年龄");
  26. String age = scanner.next();
  27. // 写入姓名
  28. fileOutputStream.write(("年龄:"+age).getBytes());
  29. // 写换行符
  30. fileOutputStream.write(System.lineSeparator().getBytes());
  31. System.out.println("请录入你的身高");
  32. String height = scanner.next();
  33. // 写入姓名
  34. fileOutputStream.write(("身高:"+height).getBytes());
  35. // 写换行符
  36. fileOutputStream.write(System.lineSeparator().getBytes());
  37. System.out.println("请录入你的体重");
  38. String weight = scanner.next();
  39. // 写入姓名
  40. fileOutputStream.write(("体重:"+weight).getBytes());
  41. // 写换行符
  42. fileOutputStream.write(System.lineSeparator().getBytes());
  43. // 关闭资源
  44. fileOutputStream.close();
  45. }
  46. }