JavaIO stream (1): basic concepts and basic concepts of javaio stream

Source: Internet
Author: User

JavaIO stream (1): basic concepts and basic concepts of javaio stream

I. input and output streams

Input stream: when the program is running, you may need to read the required data from external storage media or other programs and use the input stream.

Output stream: After a program processes data, it may write the processing result to a permanent storage media or send it to another program, using the output stream.



Note:

① All input streams are subclasses of the abstract class InputStream (byte input stream) or the abstract class Reader (character input stream;

② All output streams are subclasses of the abstract class OutputStream (byte output stream) or the abstract class Writer (character output stream.


Ii. File



2. Create three construction methods for the File object

PublicFile (String pathName); instantiate a File object through a specified path

File (String filename );

File (String directoryPath, String filename );

File (File dir, String filename );

 

Case 1:


<Span style = "font-size: 14px;"> package work; import java. io. file; import java. io. IOException; public classDemo {public static void main (String [] args) {Filefile = newFile ("D: \ IO.txt "); // delete an existing file. if (file. exists () {file. delete (); System. out. println ("successfully deleted");} // create the file you need. try {file. createNewFile ();} catch (IOException e) {e. printStackTrace () ;}}</span>



 

Case 2:

<span style="font-size:14px;">package work;import java.io.File;public classDemo2 {   public static void main(String[] args) {      Filefile=newFile("D:\\cs");      String[] s=null;      if(file.isDirectory())      {         s=file.list();         for(int i=0;i<s.length;i++)         {            System.out.println(s[i]);         }      }   }}</span>



3. Common Methods for getting File information in the File class

 

Public String getName ()

Public boolean canRead ()

Public boolean canWrite ()

Public boolean exists ()

Public long length ()

Public String getAbsolutePath ()

Public String getParent ()

Public boolean isFile () determines that the file is not a directory

Public boolean isDirectory () determines whether the file path is a folder

Public boolean isHidden ()

Public boolean delete ();

Public long lastModified () determines the last modification time of the object

Public boolean mkdir () returns true if the directory is successfully created. If the directory already exists, false is returned.

 

If the File object is a directory:

Public String [] list () returns all files in the directory in String format

Public boolean mkdir (); Create a folder

Public File [] listFiles () returns all files in the directory as a File object

Public String [] list (FilenameFilter obj) returns all objects of the specified file type in the directory.

Public File [] listFile (FilenameFilter obj) is returned as a File object

FilenameFilter is an interface with a method

 

Public boolean accept (File dir, String name );

When the File object dirFile calls the list method, it must pass a common method to implement the FilenameFilter interface File class to obtain File information.

 

Public String getName ()

Public boolean canRead ()

Public boolean canWrite ()

Public boolean exists ()

Public long length ()

Public String getAbsolutePath ()

Public String getParent ()

Public boolean isFile () determines that the file is not a directory

Public boolean isDirectory () determines whether the file path is a folder

Public boolean isHidden ()

Public boolean delete ();

Public long lastModified () determines the last modification time of the object

Public boolean mkdir () returns true if the directory is successfully created. If the directory already exists, false is returned.

 

If the File object is a directory:

Public String [] list () returns all files in the directory in String format

Public boolean mkdir (); Create a folder

Public File [] listFiles () returns all files in the directory as a File object

Public String [] list (FilenameFilter obj) returns all objects of the specified file type in the directory.

Public File [] listFile (FilenameFilter obj) is returned as a File object

FilenameFilter is an interface with a method

Public boolean accept (File dir, String name );

When the File object dirFile calls the list method, it must pass a FilenameFilter interface to the method.



Case: list the names of the. java files in the current directory


<Span style = "font-size: 14px;"> package com. cloud. day1; import java. io. file; import java. io. filenameFilter; public class Demo1 {public static void main (String [] args) {File dirFile = new File (". "); FileAccept fileAccept = new FileAccept (); fileAccept. setExtendName ("java"); String filename [] = dirFile. list (fileAccept); for (String name: filename) {System. out. println (name) ;}}// test whether the specified file should be included in a file list. Class FileAccept implements FilenameFilter {private String extendName; public void setExtendName (String s) {extendName = "." + s ;}// dir-directory of the file to be found. // Name-file name. Public boolean accept (File dir, String name) {return name. endsWith (extendName) ;}</span>

3. File Creation and Deletion



File file = new File ("C: \ myletter.txt ");

 

If no letter.txt file exists in the C: myletterdirectory, the file will call

 

Public boolean createNewFile ();

 

If yes, you can call public boolean delete () to delete the current file.


Copyright statement: original post of the blogger. For details, refer to the source. Http://blog.csdn.net/dzy21


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.