021.10 IO流 打印流
内容:PrintStream:字节流 PrintWriter:字符流
PrintStream
public static void main(String[] args) throws IOException { //创建对象 PrintStream ps = new PrintStream("myfile\\print.txt"); //写入整数 ps.write(355); //文件里面是c,只是写入整数的最低字节,也就是最后8位二进制 ps.print(355); //文件里面是355,写什么就是什么,原理就是把数组转成了字符串 ps.close(); }
PrintWriter
public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); String line = null; while((line = br.readLine()) != null){ if("//over".equals(line)){ break; } pw.println(line.toUpperCase()); pw.flush(); } }
//改变目的到文件中,让他自动刷新
pw = new PrintWriter(new BufferedWriter(new FileWriter("myfile\\printwriter.txt")),true);