• 周二. 7 月 16th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

021.10 IO流 打印流

admin

11 月 28, 2021

内容: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);

发表回复