打印流(PrintWriter )

PrintWriter
package cn.lijun.demo1; import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter; // 打印流 PrintStream PrintWriter
// 特点 不负责数据源 只负责数据目的 不会抛IOException 可能会抛出其他异常
public class Demo1PrintWrite {
public static void main(String[] args) throws Exception {
fun4();
}
//打印流可以开启自动刷新功能 输出的数据必须是流对象 Outputem Writer
// 必须调用print 方法
public static void fun4() throws FileNotFoundException{
FileOutputStream fos = new FileOutputStream("c:\\ll.txt");
PrintWriter pr = new PrintWriter(fos,true);
pr.print("zhang");
pr.print("zhang");
pr.print("zhang");
pr.close();
}
//打印流输出目的 是流对象
public static void fun3() throws FileNotFoundException{
PrintWriter p= new PrintWriter("c:\\6.txt");
PrintWriter p1= new PrintWriter(p);
p1.println("打印流");
p1.close(); } //打印流 输出目的 String 文件名
public static void fun2() throws FileNotFoundException{
PrintWriter p = new PrintWriter("c:\\3.txt");
p.println(333);
p.println(333);
p.close();
}
/*向File对象的数据目的写入数据*/
public static void fun() throws Exception{
File file = new File("c:\\2.txt");
PrintWriter p = new PrintWriter(file);
p.print(true);
p.print(100);
p.close();
}
}
//打印流复制文件
package cn.lijun.demo1; import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; public class Demo2PrintReder {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new FileReader("c:\\ll.txt"));
PrintWriter p= new PrintWriter(new FileWriter("d:\\ll.txt"),true);
String line= null;
while((line=bf.readLine())!=null){
p.print(line);
}
p.close();
bf.close();
}
}

 
上一篇:saltstack基础知识


下一篇:Navicat for MySQL出现1030-Got error 28 from storage engine错误