written in the previous: Java input and output is more important, I did not want to tidy IO and multi-threaded, but in the back of a lot of things, often to read the resource files, this time will have to go back to tidy io. To play with the input and input of Java, you must first play a class that is familiar: File. Then there is the class and interface under the Java.io package. Java IO mainly consists of 2 streams of input and output, each input and output is divided into byte stream and stream of characters. It is worth noting and studying that the IO stream of Java uses an adorner design pattern, which divides the IO stream into the underlying node stream and the upper processing stream. In addition, there are 2 things to study: the serialization mechanism of 1,java objects, 2, the new Api,java NiO.
The file class is a java.io package that represents platform-independent files and directories, and note that file class operations can be used either as files or as directories, but the file class cannot access the contents of the files themselves. If you need to access the contents of the file itself, use the input and output streams. The specific method is not listed here, the use of the time to check the JDK is good. One thing to note: He has a static property separator. This property represents the system-related default name delimiter, which is represented as a string for convenience. Because the path delimiter for Windows uses a backslash (\), and a backslash in a Java program represents an escape character, you should use 2 backslashes if you need to include a backslash in a path in Windows, such as: c:\\java\\ LinkinPark.txt, or directly using slashes, Java supports the use of slashes as platform-independent path separators.
The specific demo code is as follows:
Import Java.io.file;public class Linkin{public static void Main (string[] args) throws exception{//creates a file object with the current path file F ile = new File ("."); /Get the file name directly, output a little System.out.println (File.getname ()),//Get the parent path of the relative path may be error, the following code output NULLSYSTEM.OUT.PRINTLN (File.getparent () );//Gets the absolute path System.out.println (File.getabsolutefile ());//Gets the upper-level path System.out.println (File.getabsolutefile (). GetParent ());//Create a temporary file under the current path files tmpfile = File.createtempfile ("AAA", ". txt", file);// Specifies that the file is deleted when the JVM exits Tmpfile.deleteonexit ();//Creates a new file with the current time of the system as a new file name NewFile = new files (system.currenttimemillis () + ""); System.out.println ("NewFile object exists:" + newfile.exists ());//To specify NewFile object to create a file Newfile.createnewfile ();// Create a directory with the NewFile object, because NewFile already exists,//So the following method returns False, that is, the directory cannot be created Newfile.mkdir ();//Use the list () method to list all files and paths under the current path string[] FileList = File.list (); System.out.println ("= = = All files and paths below the current path = = = = ="); for (String filename:filelist) {System.out.println (fileName);} The Listroots () static method lists all the disk root paths. File[] roots = File.listroots (); System.out.println ("= = = SystemThe root path is as follows = "); for (File root:roots) {System.out.println (root);}}}
The file class has 2 overloaded list methods, and returns an array of strings that specify the files and directories in the directory represented by this abstract path name. One can accept the FilenameFilter parameter, which is used to filter the file. The FilenameFilter interface only accept a method that tests whether the specified file should be included in a list of files.
The following code lists all *.java and folders under the current path.
Import Java.io.file;import java.io.filenamefilter;/** * * @version 1L * @author linkinpark * @since 2014-12-30 * @motto dream like fireworks heart like water, classmate youth not romance * @desc ^ Here is the typical command design mode. Java does not support passing a chunk of code, so use the accept to encapsulate a block of related filtering. */public class Linkin{public static void Main (string[] args) {File file = new file ("."); string[] NameList = file.list (New Myfilenamefilter ()); for (String name:namelist) {System.out.println (name);}}} Implement your own FilenameFilter implementation class class Myfilenamefilter implements Filenamefilter{public Boolean accept (File dir, String name) { Returns Truereturn Name.endswith (". Java") If the file name ends in. Java, or if the files correspond to a path. New File (name). Isdirectory ();}}
Input and output--file class