Print stream:
There are two classes: PrintStream and PrintWriter. The methods of the two classes are the same. The difference lies in the constructor
PrintStream: construction method: receive File type, receive string File name, receive byte output stream
PringWriter: construction method: receive File type, receive string File name, receive byte output stream, receive character output stream
Adding functionality to other streams makes it easy to print various data values, except that it never throws IO exceptions
method:
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);// Not written d,It's 100, print as is pw.write(100);// What is written is 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("Print stream"); pw1.flush(); pw1.close(); } public static void function3() throws IOException { FileWriter fw1 = new FileWriter("d:\\4.txt"); PrintWriter pw1 = new PrintWriter(fw1); pw1.println("Print stream"); pw1.flush(); pw1.close(); } }
Print stream auto refresh:
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); // The second parameter is auto Book refresh. If it is, no need to write flush Method pw1.println("I"); pw1.println("Love"); pw1.println("You"); pw1.close(); } }
Print stream copy text file:
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(); } }
Finally, simply write down the tool class, which can greatly reduce the code amount:
apache's commons tool class:
Download it on the official website, copy it to the new lib folder under the current project, and right-click build path
Several powerful common methods:
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();//Filename operation function2();//File operation } public static void function1(){ String name = FilenameUtils.getExtension("a.java"); System.out.println(name);//Output: java String filename = FilenameUtils.getName("d:\\b.java"); System.out.println(filename);//Output: b.java boolean a = FilenameUtils.isExtension("c.java", "java"); System.out.println(a);//output true,The method of judging the file suffix } public static void function2() throws IOException{ //Read the contents of a text file String s1 = FileUtils.readFileToString(new File("d:\\1.txt")); System.out.println(s1); //Write text file FileUtils.writeStringToFile(new File("d:\\b.txt"), "java"); //Here you create a text file and write a string java //Copy file (not limited to text) FileUtils.copyFile(new File("d:\\1.txt"), new File("d:\\11.txt")); //Copy folder FileUtils.copyDirectoryToDirectory(new File("f:\\new"), new File("d:\\new")); } }