将键盘输入的数据保存到文件中:OutputStream
1. 程序如下:
package com.xuzhiwen.io1; import java.io.FileOutputStream; import java.io.IOException; public class FileOutputStreamTest { public static void main(String[] args) throws IOException { System.out.println("please input :"); byte b[] = new byte[1024]; int count = System.in.read(b);
//如果不存在test.txt则会创建该文件,如果存在则覆盖 FileOutputStream out = new FileOutputStream("test.txt"); out.write(b, 0, count); out.close(); System.out.println("save success"); } }
2.运行程序,输入:hello world
3.查看结果: