Java IO Operations Instance

Source: Internet
Author: User
Tags java web lenovo

I like to put other people write good articles on their blog, for fear that others delete ... But the draft does not forget to dig well people, I reproduced the article will definitely write the source, I hope you will be reproduced in the time to add ...

Article reprint address: http://www.cnblogs.com/rollenholt/archive/2011/09/11/2173787.html


Case 1 Create a new file?

1 2 3 4 5 6 7 8 9 10 11 Import java.io.*;          Class hello{public static void Main (string[] args) {file f= new file ("D:\\hello.txt");          try {f.createnewfile ();          catch (Exception e) {e.printstacktrace (); }      } }

"Run Results":

After the program runs, there will be a file named Hello.txt under D disk.

Two constants for the case 2 file class?

1 2 3 4 5 6 7 Import java.io.*;          Class hello{public static void Main (string[] args) {System.out.println (file.separator);      System.out.println (File.pathseparator); } }

"Run Results":

\

;

Here a few more: some students may think, I directly under Windows to use \ for segmentation not. Of course it's OK. But it's not on Linux. So, to make our code cross-platform, more robust, so we all use these two constants, in fact, more than a few lines. Oh

Now we use the constants in the file class to rewrite the above code:?

1 2 3 4 5 6 7 8 9 10 11-12 Import java.io.*;          Class hello{public static void Main (string[] args) {String filename= "D:" +file.separator+ "Hello.txt";          File f= new file (FileName);          try {f.createnewfile ();          catch (Exception e) {e.printstacktrace (); }      } }

You see, not much to write it, hehe. Therefore, it is recommended that you use constants in the file class.

Delete a file ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14-15 16 /** * Delete a file */import java.io.*;          Class hello{public static void Main (string[] args) {String filename= "D:" +file.separator+ "Hello.txt";          File f= new file (FileName);          if (f.exists ()) {f.delete ();          else {System.out.println ("file does not exist"); }                } }

Create a folder ?

1 2 3 4 5 6 7 8 9 10 11 /** * Create a folder * * */import java.io.*;          Class hello{public static void Main (string[] args) {String filename= "D:" +file.separator+ "Hello";          File f= new file (FileName);      F.mkdir (); } }

"Run Results":

A Hello folder under D disk

lists all files (including hidden files) for the specified directory: ?

1 2 3 4 5 6 7 8 9 10 11 12 13-14 /** * Use list to list all files in the specified directory */import java.io.*;          Class hello{public static void Main (string[] args) {String filename= "D:" +file.separator;          File f= new file (FileName);          String[] Str=f.list ();          for (int i = 0; i < str.length i++) {System.out.println (str[i]); }      } }

"Run Results":

$RECYCLE. BIN

360

360Downloads

360Rec

360SoftMove

Config.msi

Da

Downloads

Driversbackup

Eclipse

Java Web Integration development and project combat

Lenovo

MSOCache

Program

Program Files

Python

Recygler. {8F92DA15-A229-A4D5-B5CE-5280C8B89C19}

System Volume Information

Tomcat6

Var

Vod_cache_data

Create a new Folder

(Your running result should be different from this, hehe)

But a string array is returned using the list. And the list is not the full path, and if you want to list the full path, you need to use Listfiles. He returned an array of file

lists all files (including hidden files) for the specified directory: ?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 /** * Use Listfiles to list all files of the specified directory * listfiles output is full path */import java.io.*;          Class hello{public static void Main (string[] args) {String filename= "D:" +file.separator;          File f= new file (FileName);          File[] Str=f.listfiles ();          for (int i = 0; i < str.length i++) {System.out.println (str[i]); }      } }

"Run Results":

D:\ $RECYCLE. BIN

D:\360

D:\360Downloads

D:\360Rec

D:\360SoftMove

D:\Config.Msi

D:\da

D:\Downloads

D:\DriversBackup

D:\eclipse

D:\java Web integration development and project combat

D:\Lenovo

D:\MSOCache

D:\Program

D:\Program Files

D:\python

D:\RECYGLER. {8F92DA15-A229-A4D5-B5CE-5280C8B89C19}

D:\System Volume Information

D:\Tomcat6

D:\var

D:\vod_cache_data

D:\ new Folder

By comparison you can specify that using Listfiles is more convenient,

 

determine if a specified path is a directory ?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 /** * Use Isdirectory to determine whether a specified path is a directory */import java.io.*;          Class hello{public static void Main (string[] args) {String filename= "D:" +file.separator;          File f= new file (FileName);          if (F.isdirectory ()) {System.out.println ("YES");          else {System.out.println ("NO"); }      } }

"Run result": YES

 

Search the entire contents of a specified directory

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 /** * List All contents of the specified directory * */import java.io.*;          Class hello{public static void Main (string[] args) {String filename= "D:" +file.separator;          File f= new file (FileName);      Print (f); public static void print (File f) {if (f!= null) {&NB
Related Article

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.