Java Foundation--io Stream

Source: Internet
Author: User
Tags file copy

IO stream
1.java.io.file
1. All classes and interfaces related to input and output are defined under Java.io
2.File is a class that can be used by the constructor to create its objects. This object corresponds to a file (. txt. avi. ppt. doc. mp3. jpg) or a file directory
The 3.File class object is platform agnostic (Java cross-platform feature)
The method in 4.File, only involves how to create, delete, rename and so on (surface Kung Fu), as long as the contents of the file is related to, files are powerless, must be completed by the IO stream
Objects of the 5.File class are often the parameters of the constructor of the concrete class of the IO stream


Access file name:
GetName ()
GetPath ()
Getabsolutefile ()
GetAbsolutePath ()
GetParent ()
Renameto (file newName)//Rename
Boolean b = File1.renameto (file2);//require 1.file1 file exists, file2 file does not exist. 2.file1 file2 type (file or file directory)

file detection
exists ()
CanWrite ()
CanRead ()
Isfil E ()
Isdirectory ()

Get general file Information
LastModified ()
Length ()

file operations related to /strong>
CreateNewFile ()
Delete ()

directory operations related
MkDir ()//Create a file directory only if the upper-level file directory exists     To return True
Mkdirs ()//Create a file directory, if the upper-level file directory does not exist, create a
list ()//return the contents of the current folder, is string[] form of string[] str = file1.list ();
Listfiles ()//returns the contents of the current folder, is file[] form, can continue to face the operation of the file file[] files = file1.listfiles ();

IO stream
 1. Classification of streams
By operating data units divided into: byte stream (8 bit), character stream (three bit) (processed text file)
The flow of data is divided into: input stream, output stream
The role of the stream is divided into: node stream (directly acting on the file), processing flow

2.IO System:
Abstract base class node stream (file stream) buffer stream (one of the processing flows)
InputStream FileInputStream Bufferedinputstream
OutputStream FileOutputStream Bufferedoutputstream
Reader FileReader BufferedReader
Writer FileWriter BufferedWriter

For non-text files (video, picture, audio), you can only use the byte stream to implement the file copy FileInputStream, FileOutputStream
For text files, you can also use only character streams FileReader, FileWriter

FileReader, FileWriter
When used, just read there char[] C = new char[20]; Other places are similar to FileInputStream, FileOutputStream

@Testpublic void testInputFileStream1 () {//2. Creates an object of the FileInputStream class FileInputStream Fis = null;try {//1. Create an object of the file class file File = New file ("Tests.txt"), FIS = new FileInputStream (file),//3. Calls the FileInputStream method, implements the Read/* public int read (byte[] b) * Reads a byte of a file and returns 1 */byte[] b = new Byte[5];int len;while (len = Fis.read (b))! =-1) {St) when executing to end of file  Ring str = new String (b, 0, Len);//note is not b.length, otherwise the last step will not overwrite the characters are output System.out.print (str);}} catch (IOException e) {e.printstacktrace ();} Finally {try {//4. Close the corresponding stream fis.close ();} catch (IOException e) {e.printstacktrace ()}}} It is more reasonable to handle an exception using the Try-catch method: Guaranteed flow Close operation must be performed @testpublic void Testinputfilestream () {//2. Create an object of the FileInputStream class FileInputStream Fis = null;try {//1. Create an object of the file class file File = New file ("Tests.txt"); fis = new Filein Putstream (file);//3. Call the FileInputStream method, implement the file read/* *read (): Reads a byte of the file, when executed to the end of the file, returns 1 */int b;while ((b = fis.read  ())! =-1) {System.out.print ((char) b);}} catch (IOException e) {e.printstacktrace ();} Finally {try {//4. Close the corresponding stream Fis.closE (); } catch (IOException e) {e.printstacktrace ();}}}

  

@Testpublic void Testfileoutputstream () {//1. Creates a file object that indicates the location to be written to//the physical file of the output may not exist, and is created automatically when it is not present during execution. If it exists, the original file will be overwritten with the filename "Love.txt" ("//2"); Create an object of FileOutputStream class. The object of file is passed as a parameter to the constructor of fileoutputstream fileoutputstream fos = null;try {fos = new FileOutputStream (file);//3. Call FileOutputStream method, write Operation//fos.write ("I love china!"); Fos.write (New String ("I Love you! I love China "). GetBytes ());} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} finally {if (FOS! = null) {try {//4. Close the corresponding file Fos.close ();} catch (IOException e) {e.printstacktrace ()}}}}

  

Reads a file from the hard disk and writes to another location (equivalent to copy) @Testpublic void Testcopyfile () {Long start = System.currenttimemillis (); string src = new string ("C:\\users\\hasee\\desktop\\qq20171106155140.png"); String dest = new String ("C:\\users\\hasee\\desktop\\4.jpg"); CopyFile (SRC, dest); Long end = System.currenttimemillis () ; System.out.println ("Time Spent:" + (End-start));} public void CopyFile (string src, string dest) {//file file1 = new File ("Love");//note suffix, with and without being different//file file2 = new File ("Love 2.txt ");//file file1 = new File (" C:\\users\\hasee\\desktop\\qq20171107180120.png ");//note suffix, with and without a different file File1 = new File (SRC); File File2 = new file (dest);//Two formats can be copied over fileinputstream FIS = null; FileOutputStream fos = null;try {fis = new FileInputStream (file1); fos = new FileOutputStream (file2); byte[] B = new Byte[5] ; int len;while (len = Fis.read (b))! =-1) {//fos.write (b); Two types of errors: Fos.write (b, 0, b.length), Fos.write (b, 0, Len);}} catch (Exception e) {e.printstacktrace ();} finally {if (FOS! = null) {try {fos.close ();} catch (IOException E) {E.printstacktrace ();} finally {if (FIS! = null) {try {fis.close ();} catch (IOException e) {e.printstacktrace ();}}}}}

  


Java Foundation--io Stream

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.