zoukankan      html  css  js  c++  java
  • 打印流(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();
        }
    }
    
    
    
     
  • 相关阅读:
    mysql的sql性能分析器
    Maven(一)
    SVN使用(二)
    SVN使用(一)
    php smarty section使用
    php smarty foreach循环注意
    PHP unlink() 函数
    PHP file_exists() 函数
    PHP realpath() 函数
    PHP dirname() 函数
  • 原文地址:https://www.cnblogs.com/qurui1998/p/10610116.html
Copyright © 2011-2022 走看看