I. Comprehensive exercises-List of file listings
Gets the file (including subdirectories) of the specified extension under the specified directory, and writes the absolute path of the files to a text file. Which is to create a list of files with the specified extension.
1. Deep traversal
2. Container with filter
3. Writing files
Import Java.util.list;import Java.io.*;import Java.util.arraylist;public class Main {public static void main (string[] args) throws IOException {file dir = new file ("D:\\java");//Specify source file FilenameFilter filter = new FilenameFilter () {//filter public Boolean Accept (File dir, String name) {return Name.endswith (". Java");}}; list<file> list = new arraylist<file> ();//Container GetFile (dir, filter, list);//Get the file that matches the condition files Dfile = new (dir, "Javalist.txt"); WriteToFile (list, dfile);//Write}/* * * */public static void GetFile (File dir, filenamefilter filter,list<file> list) Throws ioexception{file[] files = dir.listfiles (); for (File file:files) {if (File.isdirectory ()) {GetFile (File,filter, list);} Else{if (Filter.accept (dir, File.getname ())) {List.add (file);}}} public static void WriteToFile (list<file> list,file destfile) {BufferedWriter bw = null;try {bw = new BufferedWriter ( New FileWriter (DestFile)); for (File file:list) {bw.write (File.getabsolutepath ()); Bw.newline (); Bw.flush ();}} catch (exception e) {throw new RuntimeException ("Write Failed");} Finally{if (bw!=null) {try {bw.close ()} catch (Exception E2) {//Todo:handle Exceptionthrow new RuntimeException ("Shutdown failed") ;}}}}}
second, other classes in the IO package
Print Flow: PrintWriter, PrintStream: can manipulate input stream and file directly
Sequence Flow: Sequenceinputstream: Merging multiple streams
Operands: ObjectInputStream,ObjectOutputStream: The object to be manipulated needs to implement serializable (tag interface)
Print Flow
BYTE print stream: PrintStream
1. Provides a printing method to print multiple data type values and maintain the representation of the data
2. Never throw IOException
constructor that receives three types of values
1. String path
2.File objects
3. Byte output stream
public static void Main (string[] args) throws IOException {PrintStream PS = new PrintStream ("A.txt");//Specify Write file//ps.write (9 8);//save is B, write only binary minimum 8-bit ps.print (98);//Turn data into a string, print it as-is, and overwrite the original data ps.close () for each print;}
character Print stream: PrintWriter
constructor that receives four types of values
1. String path
2.File objects
3. Byte output stream
4. Character output stream
public static void Main (string[] args) throws IOException {BufferedReader br = new BufferedReader (New InputStreamReader (Sy stem.in)//printwriter pw = new PrintWriter (system.out,true) printwriter pw = new PrintWriter (New FileWriter ("Text.txt") ), true);//Auto-refresh, and only the output stream will automatically refresh string line = Null;while ((Line=br.readline ())!=null) {if (' over '. Equals (line)} break; Pw.println (line);} Pw.close (); Br.close ();}
Sequence Flow
SequenceInputStream
Represents a logical concatenation of other input streams. It starts with an ordered collection of input streams and reads from the first input stream until the end of the file is reached, then reads from the second input stream, and so on, until the end of the file containing the last input stream is reached.
Combine multiple sources, synthesize one source
Construction Method:
SequenceInputStream(Enumeration<? extendsInputStream> e)
By remembering the parameters to initialize the newly created SequenceInputStream , the parameter must be a type parameter that generates the run-time type as an InputStream object Enumeration . |
SequenceInputStream(InputStream s1,InputStream s2)
By remembering these two parameters to initialize the newly created SequenceInputStream (the two parameters will be read sequentially, read first s1 , and then read s2 ) to provide SequenceInputStream the bytes read from this. |
public static void Main (string[] args) throws IOException {//merge a.txt,b.txt,c.txt data into one file// The Sequenceinputstream constructor has two, which demonstrates enumerating//vector<fileinputstream> VEC = new vector<fileinputstream> ();//// Vec.add (New FileInputStream ("A.txt")),//vec.add (New FileInputStream ("B.txt")),//vec.add (New FileInputStream (" C.txt "));//enumeration<fileinputstream> en = vec.elements (); arraylist<fileinputstream> ar = new arraylist<fileinputstream> (),//ar is not enumerated, but there is an iterator ar.add (new FileInputStream ("A.txt")), Ar.add (New FileInputStream ("B.txt")), Ar.add (New FileInputStream ("C.txt")); Enumeration<fileinputstream> en = collections.enumeration (AR);//The principle is as follows/*final iterator< FileInputStream> it = Ar.iterator (); Enumeration<fileinputstream> en = new enumeration<fileinputstream> () {//Implement Enumerationpublic Boolean hasMoreElements () {return It.hasnext ();} Public FileInputStream nextelement () {return It.next ();}}; */sequenceinputstream sis = new Sequenceinputstream (en);//Merge FileOutputStream FOS = new FileOutputStream ("D.txt"); byte[] by = new Byte[1024];int len = 0;while ((len = Sis.read (by))!=-1) {Fos.write (by) 0,len);} Fos.close (); Sis.close ();}
Java Learning lesson 54th-io Stream (eight) print flow & sequence Flow