JAVA IO File Properties

Source: Internet
Author: User
Tags list of attributes

File : Document class, Action folder, file property information. Create object Example "file F1=new file (" C:\\abc\\a.txt "); The string can be a variable file F2 = new file ("C:\\abc", "A.XTX"), a directory name and a file name  file d = "New File" ("C:\\abc") file F3 = New file (d, "a.txt"); Variable form.   Directory delimiter cross-platform form file F4 = new file ("C:" +file.separator+ "abc" +file.separator+ "Abc.txt"; Common methods for  file class: 1, creating     Bollean creatnewfile (); Creates a file at the specified location and does not create if the file already exists. Returns false, unlike the output stream, where a file is created as soon as the output stream is established. and will be overwritten.     mkdir ();//Create a directory, you can create only one level directory     mkdirs (); You can create multiple and directory 2, delete     Boolean delate ();    void Dealteonexit ();//delete when exiting. 3, judging     Boolean canexcute ();//Whether it can be performed     CanRead ()     CanWrite ()     exists ()     Isfile ()//Determine if a file is a file or directory, you must first determine whether the file object encapsulates the existence of the content, through exists () .c    Ishidden ()     Isdirectory ()     Isabsolute ()     int compareTo ()  4, get file information     string     GetName ()     GetPath ()     getParent ()     long lastmodified (); Last modified time     LO Ng Lenth ()     Boolean Renameto (file)//Move file 5, other     list drive letter     File [] f =file.listroots ();      F or (File p:f)       {      &NBSP;SOP (p);     }     String [] Li St ();//returns the file or folder name in the specified directory         file filter     string[] List (filenamefilter f);// Returns an array of file names in the specified format    file f1=new file ("C:\\users\\zx\\desktop");  String name [] =f1.list (new FilenameFilter ()   {   public Boolean accept (file dir,string N)//dir and name is the directory of the file object and all file names    {& nbsp   Return N.endswith (". O");   } });  sop (name.length);      file[] Listfiles (); same as above, but returns an array of file objects. You can also use filters.    Properties is a subclass of Hashtable  it has the characteristics of the map collection, and the stored key-value pairs are all strings, no generics requiredis a collection container that combines IO technology in the collection. the characteristics of the object: can be used in the form of key-value pairs of configuration files, you can manipulate the hard disk key-value pairs.  String GetProperty (String key)//Return value Object SetProperty (string key,string value)//Modify value/Set Value list (PrintStream out)// Assigns a list of attributes to the output stream. Load (reader Reader)//combine with Stream store (outputstream,string annotation information) set<string> stringpropertynames ()//Return key set Print Flow printstream printwriter  Byte print stream and character print stream. is a subclass of stream and writer. The PrintStream constructor can receive parameter types: 1,file object File2, String path String3, character count out stream OutputStream PrintWriter can accept various streams. 1,file object File2, string path, file name path. 3, character number out stream OutputStream4, one more character output stream Writer Main method, in addition to the OutputStream method, there are printprintln methods, and can guarantee the original nature of the data Merge Flow Sequenceinputstream, direct example Application   cut stream split, cut a file into three files for example, there are no new objects.     For example, list all directories: RecursiveImport Java.io.*;import java.util.*;  public class Test {public static void main (string[] args) throws IOException {file f= new file ("e:\\ Baidu Cloud Sync Disk"); Showdir (f);  public static void Showdir (file dir) {file [] files = Dir.listfiles ();   for (int x=0;x<files.length;x++) {if (Files[x].isdirectory ()) {Showdir (files[x]);   } else {SOP (files[x]); }}} public static void sop (Object o) {System.out.println (o);}} store all. java file directories in a file  import java.io.*;import java.util.*;Public class Test { Public static void Main (string[] args) throws IOException {file f= New file ("e:\\ Baidu Cloud Synchronization Disk");FileWriter fw = new FileWriter ("C:\\users\\zx\\desktop\\file.txt");bufferedwriter bw = new BufferedWriter (FW);Showdir (F,BW);bw.close ();SOP ("End"); } Public static void Showdir (File dir,bufferedwriter bw) throws IOException {File [] files = Dir.listfiles ();for (int x=0;x<files.length;x++)  {if (files[x].isdirectory ())   {Showdir (FILES[X],BW);   }Else   {if (files[x].tostring (). EndsWith (". Java"))    {Bw.write (files[x].tostring ());SOP (Files[x].tostring ());bw.newline ();Bw.flush ();    }               }  } } Public static void sop (Object o) {System.out.println (o); } }  Properties Application  Import Java.io.*;import Java.util.*; public class Test {  public static void main (string[] args) throws IO exception {  Properties pop = new properties ();  pop.load (New FileReader ("c:\\users\\zx\\desktop\\ File.txt ")); Load information from file   SOP (POP);  pop.setproperty ("Zhanghaijun", "88"); Modify the Map collection   Pop.store (New FileOutputStream ("C:\\users\\zx\\desktop\\file.txt"), null); Writes a new collection to a file   sop (POP);  } public static void sop (Object o)  {  System.out.println (o);  } }   limit the number of program usage. Import Java.io.*;import Java.util.*; public class Test {  public static void main (string[] args) throws IO exception {  Properties pop = new properties ();  pop.load (New FileReader ("c:\\users\\zx\\desktop\\ File.txt "));   String s =pop.getproperty (" Times ");  int a = Integer.parseint (s);  if (a>=10)    sop ("Not available");   else   {    SOP ("+a+" is already used);    pop. SetProperty ("Times", a+1+ "");    Pop.store (New FileOutputStream ("C:\\users\\zx\\desktop\\file.txt"), NULL);   }   } public static void sop (Object o)  {  System.out.println (o);  }}  PrintWriter Example ApplicationImport Java.io.*;import java.util.*; public class Test {public static void main (string[] args) throws IOException {BufferedReader buf = new BufferedReader (  New InputStreamReader (system.in)); PrintWriter out = new PrintWriter (System.out, true);  If set to True, it is automatically refreshed.  String line = null;   while (line = Buf.readline ())!=null) {out.println (line);  Out.flush (); }} public static void sop (Object o) {System.out.println (o);}} Sequenceinputstream Merging Streams Example ApplicationImport Java.io.*;import Java.util.*; public class Test {  public static void main (string[] args) throws IO exception {  FileInputStream f1 = new FileInputStream ("C:\\users\\zx\\desktop\\1.txt");  FileInputStream F2 = new FileInputStream ("C:\\users\\zx\\desktop\\2.txt");  fileinputstream F3 = new FileInputStream ("C:\\users\\zx\\desktop\\3.txt");  fileoutputstream f4 = new FileOutputStream ("C:\\Users\\zx\\ Desktop\\4.txt ");  vector<fileinputstream> v = new vector<fileinputstream> ();  v.add (F1);   V.add (F2);  V.add (F3);   enumeration<fileinputstream> en = v.elements ();  Sequenceinputstream sis = new Sequenceinputstream (en);  byte [] by =new byte [1024];  int len;  while (len= Sis.read (by)!=-1)   {   f4.write (By,0,len);   f4.flush ();    }  Sis.close ();  f4.close ()   } public static void sop (Object o)  {  System.ouT.println (o);  }}

JAVA IO File Properties

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.