Ava learns the flow of notes, the operation of files
For some basic knowledge, there is no longer much explanation,
Simple file Query filtering operation
Package Com.wfu.ch08;import Java.io.file;import Java.io.filenamefilter;public class Test1 {public static V OID Main (string[] args) {file File=new file ("F:\\2017-2018-1\\javase");/////Get the directory, all the names of the files or directories consisting of an array of strings// String[] List=file.list ()////Traverse list//for (String str:list) {//System.out.println (str);//}// File[] List=file.listfiles ()////Traversal file array list//for (file f:list) {//if (F.isdirectory ()) {// System.out.println ("Directory:" +f.getname ());//}//else{//System.out.println ("File:" +f.getname ( );//}//}//referencing the external class implements the Filter function Myfilter filter=new myfilter (); String[] List=file.list (filter); for (String str:list) {System.out.println (str); }//File File=new file ("D:\\test.txt");//System.out.println ("Is it a directory?") +file.isdirectory ());//System.out.println ("File Size" +file.length ());//System.out.println ("File name: "+file.getname ());//System.out.println (" File path "+file.getpath ());//Date Date=new date (file.lastmodified ());// System.out.println ("Last Modified:" +date);//System.out.println ("Is it a file?") "+file.isfile ()); }}//the external class to implement filtering, because FilenameFilter is not implemented class, so we have to write our own class Myfilter implements filenamefilter{@Override public boolean AC Cept (File Dr, String name) {//TODO auto-generated Method stub if (Name.endswith (". Doc") | | Name.endswith (". docx")) {return true; } else{return false; } } }
Examples of internal classes
package com.wfu.ch08;import java.io.File;import java.io.FilenameFilter;public class Test2 { public static void main(String[] args) { File file=new File("F:\\2017-2018-1\\javase"); Test2 t=new Test2(); Test2.MyFilter filter=t.new MyFilter(); String[] list=file.list(filter); for(String s:list){ System.out.println(s); } } //这个类写在了类 Test2 中,所以是内部类,在调用内部类是一定要声明对象啊啊啊啊啊 class MyFilter implements FilenameFilter{ @Override public boolean accept(File arg0, String arg1) { // TODO Auto-generated method stub if(arg1.endsWith(".doc")||arg1.endsWith(".docx")){ return true; } else{ return false; } } }}
Anonymous class examples
package com.wfu.ch08;import java.io.File;import java.io.FilenameFilter;public class Test3 { public static void main(String[] args) { File file=new File("F:\\2017-2018-1\\javase"); //这里的没有写类的名字,而是直接写的实现方法,这样就是匿名的内部类 String[] list=file.list(new FilenameFilter(){//这个地方本来是不能直接使用接口的,但是,因为下面的实现类也写上了,所也这里实质山是调用的实现类,而不是接口 @Override public boolean accept(File arg0, String arg1) { // TODO Auto-generated method stub if(arg1.endsWith(".doc")||arg1.endsWith(".docx")){ return true; } else{ return false; } } }); for(String s:list){ System.out.println(s); } }}
Simple example of flow and file operation
Package Wfu;import Java.io.fileinputstream;import Java.io.fileoutputstream;import java.io.ioexception;import Java.io.inputstream;import Java.io.outputstream;public class Test3 {public static void main (string[] args) throws Ioex ception {FileOutputStream out = new FileOutputStream ("D:\\test2.txt"); String str = "This is a bug"; Out.write (Str.getbytes ());//write character array//out.close (); FileInputStream input = new FileInputStream ("D:\\test2.txt"); Byte[] B = new byte[1024]; Input.read (b); System.out.println (New String (b)); Input.close (); FileOutputStream Outto = new FileOutputStream ("F:\\test2.txt"); Out.write (Str.getbytes ()); Out.close (); //using the word stream will appear in Chinese garbled////So if you have Chinese words or use a character stream better//FileInputStream input = new FileInputStream ("D:\\tes T.txt ");//int x = Input.read ();//do{//System.out.print ((char) x);// x= input.read ();//}while (X!=-1);////Close stream//Input.close ();//////////Create file Word Section input stream//InputStream input = new FileInputStream ("D:\\test.txt");//////////////////////////////byte[] B = new 1024];//Input.read (b);//System.out.println (new string (b));//Convert to string////Close//Input.close ();// Create a file output stream//OutputStream out = new FileOutputStream ("D:\\test1.txt");//The file does not exist automatically creates one, if any, executes this file//Out.write (b);// Out.close (); } }
Java Learning notes, file operations