Java IO stream technology-File, javaio

Source: Internet
Author: User

Java IO stream technology-File, javaio

IO stream technology

Concept: input-output:

Input: Read the file to the memory;

Output: write files from memory to other places

Role: mainly solves the problem of data transmission between devices.

File: The use of file classes is very important.

(1) file Construction Method

1 public static void test1 () {2 3 // three splicing paths 4 // 1. directly splice 5 File filePath = new File ("C: \ 123 \ aaa"); 6 System. out. println ("whether a folder exists --" + filePath. exists (); 7 8 // 2. splice the root path first. In the combined sub-path 9 File parentPath = new File ("C: \ 123 \ aaa"); 10 File childPath = new File (parentPath, "bb.txt"); 11 System. out. println ("whether a folder exists --" + childPath. exists (); 12 13 // 3. use commas to separate the root and sub-paths. 14 File filePath2 = new File ("C: \ 123 \ aaa", "bb.txt"); 15 System. out. println ("whether a file exists --" + filePath2.exists (); 16 17}

----------------------------------

(2) Basic Methods

1 public static void test2 () throws IOException {2 3 // 1 ,. getAbsolutePath () gets the local path 4 File file = new File (""); 5 System. out. println ("Local absolute path of the current java project:" + file. getAbsolutePath (); 6 System. out. println ("whether a folder exists --" + file. exists (); 7 8 9 // 2 ,. mkdir () Create folder 10 File dirCreatePath = new File ("E: \ abc"); 11 System. out. println ("Whether the folder is successfully created --" + dirCreatePath. mkdir (); 12 13 File dirCreatePaths = new File ("E: \ abc \\ Ddd "); 14 System. out. println ("Whether the folder is successfully created --" + dirCreatePaths. mkdirs (); 15 16 // 3 ,. createNewFile (); specify the path to create a File, 17 File fileCreatePath = new File ("E: \ abc \ 123.txt"); 18 File fileCreatePath = new File (" E: \ abc \ 456.txt"); 19 System. out. println ("created successfully" + fileCreatePath. createNewFile (); 20 21 // 4, oldFileNamePath. renameTo (fileNewNamePath) Rename 22 // (1) Method 23 File fileName = new File ("E: \ abc \ 456.txt"); 24 if (fil EName. exists () {25 File fileNewName = new File ("E: \ abc \ 666.txt"); 26 System. out. println ("Whether the rename is successful --" + fileName. renameTo (fileNewName); 27} else {28 System. out. println ("Renaming failed! "); 29} 30 31 // (2) method 2 32 File oldFile = new File (" E: \ abc \ 666.txt"); 33 System. out. println ("Whether the rename is successful --" + oldFile. renameTo (new File ("E: \ abc \ 888.txt"); 34 35 // (3), change the storage folder (or storage disk) 36 File oldFile = new File ("E: \ abc \ 888.txt"); 37 System. out. println ("Whether the storage folder is replaced successfully --" + oldFile. renameTo (new File ("E: \ abc \ ddd \ 888.txt"); 38 39 40 // 5, (1) delete (); delete a File or an empty folder 41 File fileDelete = new File ("E: \ abc \ 123.txt" ); 42 System. out. println ("whether the deletion is successful --" + fileDelete. delete (); 43 44 // (2) deleteOnExit (); delete 45 File fileDeleteOnExit = new File ("E: \ abc \ ddd \ 888.txt"); 46 System. out. println ("whether the deletion is successful --" + fileDeleteOnExit. deleteOnExit (); 47 48 for (int I = 0; I <= 10000; I ++) {49 if (I = 10000) {// run deleteOnExit () 50 System after jvm exits. out. println ("jvm has exited! "); 51} 52} 53 54}

----------------------------------

(3) Judgment Method

1 // method 2 // isFile () determines whether a file is a file (Yes, true is returned) 3 // isDirectory () determines whether a folder is a folder (Yes, true is returned) 4 // isHidden () determines whether a file or folder is hidden (Yes, true is returned) 5 // isAbsolute () determines whether the path is absolute (Yes, true is returned) 6 7 public static void test3 () {8 File file = new File ("E: \ abc \ ggg \ hhh.txt "); // File 9 File file2 = new File ("E: \ abc \ ggg"); 10 if (file2.exists () {11 System. out. println ("Is it a file? "+ File. isFile (); 12 System. out. println (" 2 is a folder? "+ File2.isDirectory (); 13 System. out. println (" 2 is a hidden file? "+ File2.isHidden (); 14 System. out. println (" is the absolute path? "+ File. isAbsolute (); 15 System. out. println (" 2 is the absolute path? "+ File2.isAbsolute (); 16} 17}

----------------------------------

(4) Obtaining Method

1 // obtain method 2 // getName () obtain the file name (name after the last Separator of the path) 3 // getPath () Obtain Path 4 // length () get File Size 5 // getParent () Get the parent class path of the file or folder 6 // getAbsolutePath () Get the absolute path 7 // lastModified () get the last modified timestamp of the File (in milliseconds) 8 9 public static void test4 () {10 file File = new File ("E: \ abc \ ggg \ hhh.txt "); 11 if (file. exists () {12 System. out. println ("file name --" + file. getName (); 13 System. out. println ("file path --" + file. getPath (); 14 System. out. println ("absolute file path --" + file. getAbsolutePath (); 15 System. out. println ("file size --" + file. length (); 16 System. out. println ("parent directory of the file --" + file. getParent (); 17 System. out. println ("last modification time of the file path (MS) --" + file. lastModified (); 18 19 // convert millisecond to date 20 long time = file. lastModified (); 21 Date date = new Date (time); 22 SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); 23 System. out. println ("last modification time of the file path (MS) --" + dateFormat. format (date); 24 25} else {26 System. out. println ("nonexistent"); 27} 28 29}

-----------------------------------

(5) File Operations

1 // folder operation 2 static int sumByte = 0; 3 public static void test5 () {4 5 // [1] gets the drive letter and returns an array (new is not required) 6 File [] fileRoots = File. listRoots (); 7 for (File fileRoot: fileRoots) {8 System. out. println ("Local disk:" + fileRoot); 9} 10 11 // [2] Get the array of all files in the folder 12 // (1) create path 13 File filePath = new File ("E: \ IT Learning Outline"); 14 15 // (2) put the files and folders in the path into the File class array. 16 File [] files = filePath. listFiles (); 17 18 System. out. println ("IT outline:"); 19 // (3) Traverse the File array 20 for (File file1Child: files) {21 22 // (4). If it is determined as a folder, print it directly, if it is a file, print the name and calculate the size of 23 if (file1Child. isDirectory () {24 System. out. println ("\ n --" + file1Child. getName (); 25 26 // (5) nested traversal. [You can also use recursive calling to traverse files or folders at the next level at a layer.] 27 File [] file2 = file1Child. listFiles (); 28 for (File file2Child: file2) {29 if (file2Child. isDirectory () {30 31 System. out. println ("--" + file2Child. getName (); 32 33 // (6) 34 File [] file3 = file2Child. listFiles (); 35 for (File file3Child: file3) {36 if (file3Child. isDirectory () {37 System. out. println ("--" + file3Child. getName (); 38 39} else {40 System. out. println ("-->" + file3Child. getName () + "\ t" + file3Child. length () + "bt"); 41 sumByte + = file3Child. length (); 42} 43 44} 45} else {46 System. out. println ("-->" + file2Child. getName () + "\ t" + file2Child. length () + "bt"); 47 sumByte + = file2Child. length (); 48} 49 50} 51 52} else {53 System. out. println ("-->" + file1Child. getName () + "\ t" + file1Child. length () + "bt"); 54 sumByte + = file1Child. length (); 55} 56} 57 58 System. out. println ("total file capacity:" + sumByte); 59 60} 61 62 63 // [3] You need to define an interface class 64 public static void test6 () {65 File file = new File ("E: \ IT Learning Outline"); 66 System. out. println (file. exists (); 67 String [] strings = file. list (new MyFilter (); 68 69 for (String string: strings) {70 System. out. println (string ); 71} 72} 73 74 // find the File filter 75 // class MyFilter implements FilenameFilter {76 // 77 // @ Override78 // public boolean accept (File dir, String name) {79 // TODO Auto-generated method stub80 // 81 // set the filtering idea (Here we compare the suffix Java) 82 // (1) method 1 83 /// name = name. substring (name. lastIndexOf (". "); 84 // return ". java ". equals (name); 85 // 86 // (2) method 2 87 // return name. contains ("java"); 88 // 89 // (3) method 3 90 // return name. endsWith ("java"); 91 //} 92 //} 93

Momo said:

Data is permanently stored locally, and stored to a disk as a file

About the directory delimiter:

Directory Separator on Windows: \

On Linux: "/"

Note: On windows, "\" and "/" can both be used as Directory separators,

If "\" is used, the directory separator is "\".

If you use "/", the separator is "/".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.