Re-stepping on Java Road _day20 (recursive, IO stream)

Source: Internet
Author: User

1: Recursion (understanding)


(1) The phenomenon of invoking the method itself in the method definition
Example: Old monks tell stories to young monks, we learn to program
(2) the notice of recursion;
A: To have an exit, or a death recursion
B: Not too many times, or memory overflow
C: Construction method cannot be used recursively
(3) Cases of recursion:
A: recursion to find factorial
B: the rabbit Problem
C: Recursive output file absolute path for all specified suffix names under the specified directory
D: Recursively delete directories with content (use caution)

2:io Flow (Master)


(1) IO for the operation of data transfer between devices
(2) Classification:
A: Flow Direction
Input stream Read Data
Output stream writes out data
B: Data type
BYTE stream
BYTE input stream
BYTE output stream
Character Stream
Character input stream
Character output stream
Attention:
A: If we do not specify according to what points, by default according to the data type points.
B: It is recommended to use a byte stream unless the file is opened in a Windows-brought notepad that we can read.
(3) FileOutputStream Write Data
A: Operation procedure
A: Create a byte output stream object
B: Call the Write () method
C: Releasing Resources

B: The code reflects:
FileOutputStream fos = new FileOutputStream ("Fos.txt");

Fos.write ("Hello". GetBytes ());

Fos.close ();

C: Issues to be aware of?
A: How many things do you do to create a byte output stream object?

* A: Call the system function to create the file (if the file does not exist, it will be created, and the file class is just an abstract instance of the path)
* B: Create Fos Object
* C: Point the Fos object to this file
B: Why close ()?

* A: Let the stream object become garbage so that it can be recycled by the garbage collector
* B: Notify the system to release resources related to the file
C: How to implement the data line wrapping?

Just now we saw that there is a write text file Open is OK, through the Windows bring the one that does not, why?
Because different systems are not the same for different line-break symbol recognition?
windows:\r\n
linux:\n
Mac:\r
Some of the more common advanced notebooks are the ones that can recognize any line break symbol.
D: How to implement the data append write?

The use of the constructor with the second argument is true: publicFileOutputStream(File File, Boolean append)
(4) FileInputStream reading data
A: Operation procedure
A: Create a byte input stream object
B: Call the Read () method
C: Releasing Resources

B: The code reflects:
FileInputStream fis = new FileInputStream ("Fos.txt");

Mode 1
int by = 0;
while ((By=fis.read ())!=-1) {
System.out.print ((char) by);
}

Mode 2
byte[] bys = new byte[1024];
int len = 0;
while ((Len=fis.read (bys))!=-1) {
System.out.print (New String (Bys,0,len));
}

Fis.close ();
(5) Case: 2 Kinds of implementations
A: Copy A text file
B: Copy Picture
C: Copy Video
(6) Byte buffer stream
A:bufferedoutputstream
B:bufferedinputstream
(7) Case: 4 Kinds of implementations
A: Copy A text file
B: Copy Picture
C: Copy Video

3: Self-taught character stream
IO Stream classification
BYTE stream:
InputStream
FileInputStream
Bufferedinputstream
OutputStream
FileOutputStream
Bufferedoutputstream

Character Stream:
Reader
FileReader
BufferedReader
Writer
FileWriter
BufferedWriter

Standard four byte input and output stream notation:

public class Demo01 {

public static void Main (string[] args) {
Date start = new Date ();
METHOD01 ("01.avi", "123.avi");//time is too long to stand
METHOD02 ("01.avi", "123.avi");//60 ms
METHOD03 ("01.avi", "123.avi");//cost: 543 ms
METHOD04 ("01.avi", "123.avi");//Cost: 24 ms
Date end = new Date ();
Long time = End.gettime ()-start.gettime ();
System.out.println ("Cost:" +time+ "milliseconds");
}

byte stream input and output: Single bytes
public static void Method01 (String inputfile, string outputfile) {
FileInputStream FIS = null;
FileOutputStream fos = null;
try {
FIS = new FileInputStream (inputfile);
FOS = new FileOutputStream (outputfile);
int by = 0;
while (by = Fis.read ())! =-1) {
Fos.write (by);
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (FIS! = null) {
try {
Fis.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
if (fos! = null) {
try {
Fos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

BYTE stream Input Output: array of bytes
public static void Method02 (String inputfile, string outputfile) {
FileInputStream FIS = null;
FileOutputStream fos = null;
try {
FIS = new FileInputStream (inputfile);
FOS = new FileOutputStream (outputfile);
byte[] array = new byte[1024];
int len = 0;
while (len = fis.read (array, 0, array.length))! =-1) {
Fos.write (array, 0, Len);
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (FIS! = null) {
try {
Fis.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
if (fos! = null) {
try {
Fos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

byte stream input and output: efficient single bytes
public static void Method03 (String inputfile, string outputfile) {
Bufferedinputstream bis = null;
Bufferedoutputstream BOS = NULL;
try {
bis = new Bufferedinputstream (new FileInputStream (Inputfile));
BOS = new Bufferedoutputstream (new FileOutputStream (outputfile));
int by = 0;
while (by = Bis.read ())! =-1) {
Bos.write (by);
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (bis! = null) {
try {
Bis.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
if (BOS! = NULL) {
try {
Bos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}

byte-stream input and output: An efficient array of bytes
public static void Method04 (String inputfile, string outputfile) {
Bufferedinputstream bis = null;
Bufferedoutputstream BOS = NULL;
try {
bis = new Bufferedinputstream (new FileInputStream (Inputfile));
BOS = new Bufferedoutputstream (new FileOutputStream (outputfile));
byte[] array = new byte[1024];
int len = 0;
while (len = bis.read (array, 0, array.length))! =-1) {
Bos.write (array, 0, Len);
}
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (bis! = null) {
try {
Bis.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
if (BOS! = NULL) {
try {
Bos.close ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}
}
}

Re-stepping on Java Road _day20 (recursive, 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.