What IO streams

OI flow

A, File class

1. Definition: File abstract class file name and directory path representation.

2, API commonly used methods and constructors: (1), File (File parent, String child) Creates a new File instance from a parent and a child abstract pathname path name string. (2), File (String pathname ) Creates a new File instance by converting the given pathname string into an abstract pathname. ( 3), File (String parent, String child) Creates a new File instance from a parent pathname string and a child pathname string. ( 4), boolean canWrite () to test whether the application can modify the file denoted by this abstract pathname. If a file read-only, it can not be modified ; ( 5), boolean createNewFile () if and only if a file with this abstract pathname specified name does not exist, inseparably create a new empty file ; ( 6), the Delete boolean () deleted ; ( 7), boolean eXISTS () tests if this abstract pathname of the file or directory indicated whether or not there ; ( 8), file getAbsoluteFile () returns the absolute pathname form of this abstract pathname. ( 9), String getAbsolutePath () returns the absolute pathname string of this abstract pathname. ( 10), String getName () Returns the name of the file or directory denoted by this abstract pathname. (11), String getParent () Returns the abstract pathname of the parent directory pathname string; if this pathname does not name a parent directory, or null. ( 12), File getParentFile () Returns the abstract pathname of this abstract pathname of the parent directory; if this pathname does not name a parent directory, null is returned. ( 13), boolean isAbsolute () to test whether this abstract pathname is absolute pathname. ( If 14), boolean isDirectory () Tests if this abstract pathname of the file is a directory ; ( 15) boolean isFile () Tests whether this abstract pathname of the file represented is a standard file ; ( 16), boolean isHidden () tests whether this abstract pathname of the file is a hidden file. ( 17), Long lastModified () Returns the abstract pathname of the file was last modified. ( 18 is), Long length () returns the length of the file thus represented by an abstract pathname. ( 19), boolean mkdir () creates this abstract pathname specified directory. ( 20), boolean mkdirs () to create this abstract pathname of the directory, including any necessary but nonexistent parent directories. ( 21), boolean renameTo (File dest) rename the file pathname of this abstract.

3, specific examples:

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Arrays;

import java.util.Date;

public class FileDemo01 {

public static void main(String[] args) throws IOException {

File(File parent, String child)

          According to create a new File instance parent abstract pathname and a child pathname string.

   File(String pathname)

          To create a new conversion by the given pathname string into an abstract pathname File instance.

   File(String parent, String child)

          According to create a new File instance parent pathname string and a child pathname string.

File file1=new File("D:\\AAA");  //"D:/AAA/haha.txt"

System.out.println(file1);

File file2=new File("D://","AAA//hehe.txt");

System.out.println(file2);

File file3=new File(file1,"haha.txt");

System.out.println(file3);

//1.boolean canWrite () to test whether the application can modify the file denoted by this abstract pathname. If a file read-only, it can not be modified

System.out.println ( "canWrite ():" + file3.canWrite ());

//2.boolean setReadOnly()

System.out.println("setReadOnly():"+file3.setReadOnly());

System.out.println ( "canWrite ():" + file3.canWrite ());

//3.boolean createNewFile () if and only if there is no file has the specified name this abstract pathname, inseparably create a new empty file.

// If the path to the file does not exist, error can not find the path specified, if the path exists to create a new file

System.out.println("createNewFile():"+file2.createNewFile());

// 4. Boolean delete () deleted

System.out.println("delete():"+file2.delete());

System.out.println("delete():"+file1.delete());

//5.boolean exists () Tests if this abstract pathname of the file or directory representation exists.

System.out.println("exists():"+file3.exists());

System.out.println("exists():"+file2.exists());

 * 6.File getAbsoluteFile()

          Returns the absolute pathname form of this abstract pathname.

 String getAbsolutePath()

          Returns the absolute pathname of this abstract path name string.

File file4=new File("hengheng.txt");

System.out.println("getAbsoluteFile():"+file4.getAbsoluteFile());

System.out.println("getAbsolutePath():"+file4.getAbsolutePath());

System.out.println(new File("D:").getTotalSpace());

System.out.println(new File("D:").getFreeSpace());

//7.String getName () Returns the name of the file or directory denoted by this abstract pathname.

System.out.println("getName():"+file3.getName());

System.out.println("getName():"+file1.getName());

 

 * 8.String getParent()

          Returns the abstract pathname of the parent directory pathname string; if this pathname does not name a parent directory, it returns null.

   File getParentFile()

          Returns the abstract pathname of this abstract pathname of the parent directory; if this pathname does not name a parent directory, it returns null.

System.out.println("getParent():"+file3.getParent());

System.out.println("getParentFile():"+file1.getParentFile());

//9.boolean isAbsolute () to test whether this abstract pathname is absolute pathname.

System.out.println("isAbsolute():"+file3.isAbsolute());

System.out.println("isAbsolute():"+file4.isAbsolute());

 * 10.boolean isDirectory()

          Tests whether this abstract pathname of the file is a directory.

  boolean isFile()

          Tests whether this abstract pathname of the file represented is a standard file.

  boolean isHidden()

          Tests whether this abstract pathname of the file is a hidden file.

System.out.println("isDirectory():"+file1.isDirectory());

System.out.println("isFile():"+file1.isFile());

//11.long lastModified () returns the file represented by this abstract pathname was last modified.

System.out.println("lastModified():"+new SimpleDateFormat().format(new Date(new File("D:/test.txt").lastModified())));

//12.long length () returns the length of the file pathname of this abstract.

System.out.println("length():"+file3.length());

 * 12.String[] list()

    File[] listFiles()

String[] strs=file1.list();

System.out.println(Arrays.toString(strs));

System.out.println(Arrays.toString(file1.listFiles()));

 * 13.boolean mkdir()

          Create this abstract pathname specified directory.

  boolean mkdirs()

          Create this abstract pathname of the directory, including any necessary but nonexistent parent directories.

File file6=new File("D:/DDD/EEE");

File file7=new File("D:/CCCCC");

File file8=new File("E:/CCCCC");

System.out.println("mkdir():"+file6.mkdir());

System.out.println("mkdir():"+file6.mkdirs());

// boolean renameTo (File dest) rename the file pathname of this abstract.

System.out.println("renameTo():"+file7.renameTo(file8));;

}

}

Two, IO streams

1. Features: the IO: Objective: to read and write the contents of the file stream : flow series data, information transmission FIFO manner, conduit source destination data -> A procedure for the division of the center read write

Classification stream : flow division according to: the input of the output stream; press operation sub-unit : byte stream, a character stream; by function : node stream, the stream function

Between the classification is consistent think of , and do not conflict

Byte stream : a panacea *****: Node stream  

 nputStream input stream of bytes This abstract class is a superclass of all the input stream of bytes.

FileInputStream obtains input bytes from a file system in a file.

Byte output stream OutputStream

2. Example:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

public class IODemo02 {

public static void main(String[] args) throws IOException {

//FileInputStream(File file)

// 1. Establish contacts

File file=new File("D:/AAA/haha.txt");

// 2. Select the stream

InputStream is=new FileInputStream(file);

// 3 reads

// read one byte of data, or -1 if the end has been reached

int num=is.read();

// 4. Data Processing

System.out.println((char)num);

System.out.println((char)(is.read()));

System.out.println((char)(is.read()));

System.out.println((char)(is.read()));

System.out.println((char)(is.read()));

System.out.println((char)(is.read()));

System.out.println(is.read());  //-1

// 5. Close

is.close();

}

}

3. Points to note: pay attention to guide package (Click the mouse on the corresponding word on it), clear output and input, be sure to turn off after the stream runs out

4, file copy: (1) Points to note: destination data source. Close the stream : first open the close, after opening the first close.

( 2), Example:

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

public class CopyFile06 {

public static void main(String[] args) {

// 1. Establish contacts

File src=new File("D:/test.txt");

File dest=new File("E:/test.txt");

// 2. Select the stream

InputStream is=null;

OutputStream out=null;

try {

is=new FileInputStream(src);

out=new FileOutputStream(dest);

// 3. Operation

byte[] car=new byte[1024];

int len ​​= -1;

while((len=is.read(car))!=-1){

out.write(car, 0, len);

}

// 4. Brush

out.flush();

 

} catch (FileNotFoundException e) {

e.printStackTrace ();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace ();

} finally{

// 5. Close

if(out!=null){

try {

out.close();

} catch (IOException e) {

e.printStackTrace ();

}

}

if(is!=null){

try {

is.close();

} catch (IOException e) {

e.printStackTrace ();

}

}

}

}

}

( 3) Note: establishing a flow of a polymorphic way to build; a clear distinction between the data source and destination.

5, the character stream: plain text only read function: Node Stream Reader FileReader character input stream file input stream of characters; Writer character output stream FileWriter

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Reader;

import java.io.Writer;

public class CharDemo01 {

public static void main(String[] args) throws IOException {

// 1. Select Flow

Reader read=new FileReader("D:/test.txt");

Writer write=new FileWriter("E:/hehe.txt");

2 // Read

char[] car=new char[1024];

int len;

/*int num=read.read();

System.out.println((char)num);

System.out.println((char)read.read());*/

while((len=read.read(car))!=-1){

write.write(car, 0, len);

}

// 3. Brush

write.flush();

// 3. Close

write.close();

read.close();

}

}

6, the character the buffer flow   can not occur polymorphism , because of the new method. BufferedReader character stream input buffer, the new method of the readLine () to read a line of characters output buffer BufferedWriter stream, new methods  newLine () to write newline

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.Reader;

import java.io.Writer;

public class BufferedDemo02 {

public static void main(String[] args) throws IOException {

// 1. Select Flow

BufferedReader read=new BufferedReader(new FileReader("D:/test.txt"));

BufferedWriter write=new BufferedWriter(new FileWriter("E:/hehe.txt"));

2 // Read

// String str=read.readLine();

String s=null;

while((s=read.readLine())!=null){

write.write(s);

write.newLine();

}

// 3. Brush

write.flush();

// 4. Close

write.close();

read.close();

}

Guess you like

Origin www.cnblogs.com/ruanjianwei/p/11781164.html