Java Basics-Input/output (i)

Source: Internet
Author: User

Overview

java io through the java.io package is the class and interface to support, under the Java.io package mainly includes input, output two IO streams, each input, output stream can be divided into two categories of byte stream and character stream. where the byte stream handles input and output operations in bytes, and the character stream is processed by characters to handle input and output operations.

A. File class

The file class can use a path string of files to create a file instance, which can be either an absolute or a relative path. By default, the system always interprets relative paths based on the user's working path, which is specified by the system property "Userdir".

Once the file object has been created, it can be accessed by invoking the method of the file object, which provides many ways to manipulate files and directories, as in the following common methods:

1. Created: Boolean createnewfile ();//creates a file at the specified location, if the file already exists, is not created, and returns false. Unlike the output stream, it will overwrite if the file already exists. boolean mkdir ();//Create a folder, you can create only one level folder2. Delete: Boolean delete ();//deletes a file or directory. The file exists, returns True, the file does not exist or is being executed, and returns false.       voidDeleteonexit ();//Delete the specified file when the program exits3. Judge: Boolean CanExecute ();//whether it is an executable fileBoolean exists ();//whether the file existsboolean isfile ();//whether it is a fileboolean isdirectory ();//whether it is a folderboolean Ishidden ();//is a hidden fileboolean Isabsolute ();//whether the file is an absolute path

Here is a piece of code that tests the function of the file class in a few simple method classes

Import java.io.*; Public classfiletest{ Public Static voidMain (string[] args) throws IOException {//creates a file object with the current pathFile File =NewFile ("."); //get the file name directly, output a bitSystem. out. println (File.getname ()); //getting the parent path of a relative path can be an error, the following code outputs NULLSystem. out. println (File.getparent ()); //Get absolute pathSystem. out. println (File.getabsolutefile ()); //get top level pathSystem. out. println (File.getabsolutefile (). GetParent ()); //Create a temporary file under the current pathFile tmpfile = File.createtempfile ("AAA",". txt", file); //Specifies that the file is deleted when the JVM exitsTmpfile.deleteonexit (); //Create a new file with the current time of the system as a new file nameFile NewFile =NewFile (System.currenttimemillis () +""); System. out. println ("whether the NewFile object exists:"+newfile.exists ()); //to specify a NewFile object to create a fileNewfile.createnewfile (); //Create a directory with the NewFile object, because NewFile already exists,//so the following method returns false, which means that the directory cannot be createdNewfile.mkdir (); //use the list () method to list all files and paths under the current pathstring[] FileList =file.list (); System. out. println ("= = = = = All files and paths below the current path = = =");  for(String filename:filelist) {System. out. println (FileName); }        //the Listroots () static method lists all the disk root paths. File[] Roots =file.listroots (); System. out. println ("= = = System All root paths are as follows");  for(File root:roots) {System. out. println (root); }    }}

The result of the operation is:

. NULL D:\j2se1\testWorkspace\Test1234\. D:\j2se1\testWorkspace\Test1234newFile object exists:false= = All files and paths below the current path are as follows. Classpath.project.settings1427952328698aaa2416973888792423250.txtbinsrc= = System all root paths are as follows =C:d: E:f:g:\

Java Basics-Input/output (i)

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.