打印流:
有两个类:PrintStream,PrintWriter类,两个类的方法一致,区别在于构造器
PrintStream:构造方法:接收File类型,接收字符串文件名,接收字节输出流(OutputStream)
PringWriter:构造方法:接收File类型,接收字符串文件名,接收字节输出流(OutputStream),接收字符输出流(Writer)
为其他流添加功能,可以方便地打印各种数据值,不同的是:它永远不会抛出IO异常
方法:
package demo; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) throws IOException { function1(); function2(); function3(); } public static void function1() throws FileNotFoundException { File file = new File("d:\1.txt"); PrintWriter pw = new PrintWriter(file); pw.println(100);// 写入的不是d,而是100,原样打印 pw.write(100);// 写入的是d pw.flush(); pw.close(); } public static void function2() throws FileNotFoundException { FileOutputStream fos1 = new FileOutputStream("d:\2.txt"); PrintWriter pw1 = new PrintWriter(fos1); pw1.println("打印流"); pw1.flush(); pw1.close(); } public static void function3() throws IOException { FileWriter fw1 = new FileWriter("d:\4.txt"); PrintWriter pw1 = new PrintWriter(fw1); pw1.println("打印流"); pw1.flush(); pw1.close(); } }
打印流自动刷新:
package demo; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; public class PrintWriterDemo { public static void main(String[] args) throws IOException { function1(); } public static void function1() throws FileNotFoundException { FileOutputStream fos1 = new FileOutputStream("d:\1.txt"); PrintWriter pw1 = new PrintWriter(fos1, true); // 第二个参数是否自动书刷新,如果是,不需要写flush方法 pw1.println("I"); pw1.println("Love"); pw1.println("You"); pw1.close(); } }
打印流复制文本文件:
package demo; import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Copy { public static void main(String[] args) throws IOException { BufferedReader bfr1 = new BufferedReader(new FileReader("d:\1.txt")); PrintWriter pw1 = new PrintWriter(new FileWriter("d:\2.txt"), true); String line = null; while ((line = bfr1.readLine()) != null) { pw1.println(line); } pw1.close(); bfr1.close(); } }
最后简单写下工具类,可以大幅度降低代码量:
apache的commons工具类:
官网下载,复制到当前工程下的新建lib文件夹,右键build path即可
几个功能强大的常用方法:
package demo; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; public class CommonsDemo { public static void main(String[] args) throws IOException { function1();//文件名操作 function2();//文件操作 } public static void function1(){ String name = FilenameUtils.getExtension("a.java"); System.out.println(name);//输出:java String filename = FilenameUtils.getName("d:\b.java"); System.out.println(filename);//输出:b.java boolean a = FilenameUtils.isExtension("c.java", "java"); System.out.println(a);//输出true,判断文件后缀的方法 } public static void function2() throws IOException{ //读取文本文件的内容 String s1 = FileUtils.readFileToString(new File("d:\1.txt")); System.out.println(s1); //写文本文件 FileUtils.writeStringToFile(new File("d:\b.txt"), "java"); //这里就创建了一个文本文件,并写入字符串java //复制文件(不限于文本) FileUtils.copyFile(new File("d:\1.txt"), new File("d:\11.txt")); //复制文件夹 FileUtils.copyDirectoryToDirectory(new File("f:\new"), new File("d:\new")); } }