Java IO and javaio

Source: Internet
Author: User

Java IO and javaio

Understanding of input and output:

Input and Output, taking the program as the reference point, external data enters the program and is completed through the input stream. The program sends data to an external device through the output stream.

File Io operations

// Obtain the object
File file =NewFile ("D:/a.txt");
File. getName () -- String // file name
File. getPath () -- String // path
File. getAbsolutePath () -- String // full path
File. getParent () -- String // folder
File. exists () -- Boolean // whether the existence is true or false
File. isFile () -- Boolean // whether it is a file
File. isDirectory () -- Boolean // is it a directory?
File. isAbsolute () -- is Boolean a full path?
File. canRead () -- Boolean // readable?
File. canWrite () -- Boolean // writable?
File. isHidden () -- Boolean // is it hidden?
File. length () -- long // size
System.Out. Println (NewDate (file. lastModified (); // The last modification Date.

Return Value: Tue Feb 07 11:15:09 CST 2017

Create and delete files:

File. createNewFile () -- Boolean; // directly create a file without overwriting the original file

File. delete () -- Boolean; // delete an object

All files in the iterative output file:

 

1 public class PrintFiles {2 String str = ""; 3 public void Get (File dir) {4 System. out. println (str + dir. getName (); 5 str + = "|"; 6 print (dir, str); 7} 8 public void print (File dir, String str) {9 File [] files = dir. listFiles (); 10 for (File file: files) {11 if (file. isFile () {12 System. out. println (str + file. getName (); 13} 14 if (file. isDirectory () {15 Get (file); 16} 17} 18} 19 20 public static void main (String [] args) {21 PrintFiles p = new PrintFiles (); 22 File file = new File ("C: \ Users \ xuezhenderson ong \ Desktop \ new folder (2)"); 23 p. get (file); 24} 25 26}

 

Find a specific file:

 

1 File dir = new File ("D:"); 2 File [] files = dir. listFiles (new FileFilter () {// find a specific File 3 @ Override 4 public boolean accept (File pathname) {5 return pathname. getName (). endsWith ("txt"); endwith indicates suffix 6} 7}); 8 for (File file: 9 files) {10 System. out. println (file); 11} 12 File [] files = dir. listFiles (new FilenameFilter () {13 @ Override14 public boolean accept (File dir, String name) {// path of the dir File 15 // name File Name 16 return! Name. endsWith ("a"); 17} 18 });

 

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.