欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java IO类操作Print与BufferedReader

程序员文章站 2022-05-03 23:41:49
PrintStreamPrintStream p = new PrintStream("F:\\1.txt"); p.println("嘿嘿"); p.close();结果:PrintWriterPrintWriter p2 = new PrintWriter("F:\\1.txt"); p2.println("呵呵"); p2.flush(); p2.close();结果字符流转换为字节流FileOutp...

PrintStream

PrintStream p = new PrintStream("F:\\1.txt"); p.println("嘿嘿"); p.close(); 

结果:
Java IO类操作Print与BufferedReader

PrintWriter

PrintWriter p2 = new PrintWriter("F:\\1.txt"); p2.println("呵呵"); p2.flush(); p2.close(); 

结果
Java IO类操作Print与BufferedReader

字符流转换为字节流

FileOutputStream fos = new FileOutputStream("f:\\1.txt"); PrintWriter pw = new PrintWriter(fos); pw.println("123456"); pw.flush(); pw.close(); 

结果:
Java IO类操作Print与BufferedReader

BufferedReader

缓存读取流:将字符输入流 转为带有缓存 可以一次读取一行的缓存读取流

FileReader fr = new FileReader("F:\\1.txt"); BufferedReader br = new BufferedReader(fr); String test = br.readLine(); System.out.println(test); br.close(); 

结果:
Java IO类操作Print与BufferedReader
注意:读到结尾会返回null

异常日志收集

package zuoye; import jdk.internal.org.objectweb.asm.tree.TryCatchBlockNode; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.text.SimpleDateFormat; import java.util.Date; public class Demo12 { public static void main(String[] args) throws FileNotFoundException { //收集异常信息 try { String s = null; s.toString(); }catch (Exception e){ PrintWriter pw = new PrintWriter("c://bug.txt"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); pw.print(format.format(new Date())); e.printStackTrace(pw); pw.close(); } } } 

本文地址:https://blog.csdn.net/qq_30245525/article/details/108040986

相关标签: IO java