I/O system
1: Stream:
(1) Whether the judgment is input or output; always stand on the position of the program;
(2) Determine whether the transmission is byte or character, thus determining the thickness of the pipe;
A byte pipeline can pass all the data, and the character pipeline is designed to pass text data (1 characters equals 2 bytes)
2:java Stream four Parent classes:
Classification of streams: character streams; byte stream (object flow)
byte stream (for output file, audio and video storage large output mode "2 output full type", try not to transfer text files,):
InputStream (input) determines that the input is a byte pipe, the word in front of the InputStream subclass as a node (file: Is the meaning of the document, as the data source output), by looking at the API documentation;
OutputStream (output) determines that the input is a byte pipe, the word in front of the OutputStream subclass as a node (file: Is the meaning of the document, as a data source input), by looking at the API documentation;
Character streams (for output text files, such as storing smaller output modes):
Reader (Input)
Writer (Output)
Input stream and output stream
Input: InputStream, Reader
Output: OutputStream, Writer
Provides a parent class that provides methods for all subclasses
As long as the input output, will accompany the exception to appear, not directly new, first equals NULL in the new pipeline, determine the two ends of the pipeline, determine the first to do close the pipe (nurturance habit),
Direction of Flow:
public static void Main (String [] args) {
A copy of the file, which is the manual code that may appear in the interview!
Function: Copy D:/test.avi to F:/wudi.avi
As long as the input and output are likely to have a compile-time exception
FileInputStream FIS = null;
FileOutputStream fos = null;
try{
1. Build Pipelines
FIS = new FileInputStream ("D:/test.avi");
FOS = new FileInputStream ("F:/wudi.avi");
2. Operation Piping
Method One: The output time is too long
int b =0;//read a Byte, why use an int to pick it up?
while ((b = fis.read ())!=-1) {
Fos.write (b);
}
Method Two: Will make the output file bytes larger
Byte[] b = new byte [1024];//[1024] can increase, increase output, but the range as far as possible not to exceed the mega-level! (Tested on production test machine)
1Byte = 8bit; 1KB = 1024B; 1MB = 1024KB;
int length = 0;//record number of valid bytes read
while (length = Fis.read (b))! =-1) {//-1 Determines whether to read to the end of the file
Fos.write (b,0,length);
Fos.fiush ();//force the contents of the buffer to be brushed out
}
} catch (FileNotFoundException e) {
E.printstacktrace
} catch (IOException e) {
E.printstacktrace
} finally{
3. Close the pipe
if (FIS! = null) {
Fis.close ();
} catch (IOException e) {
E.printstacktrace
}
}
if (fos! = null) {
try{
Fos.close ();
} catch (IOException e) {
E.printstacktrace
}
}
3: Serialization/deserialization of objects
(Learn the basics of distributed Applications "cross-file Operations"):
Serialization: The object in memory is output in the form of a binary stream;
Deserialization: Converts the input binary object stream into an object in memory; (the second way to produce an object: deserialization; "The first: new;")
"Emphasizes the output of objects in binary form, but does not determine where the output is"
(Interface chapter) identifies the interface "Serializable": allows the implementation of his class to perform an operation, often without any method;
Serializable interface should be implemented
Docking of Pipelines: objects
serializable--Serializable interface
The third item in the JavaBean specification: the serializable interface should be implemented!
Let Studentbean implements Serializable (IO) Every compilation of each class should be regenerated;
Attributes that are decorated with the transient keyword do not participate in serialization;
public static void Main (String [] args) {
Object serialization--outputs the object in the form of a binary stream;
Studentbean sb = new Studentbean ("Zhang3", 24,true);
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try{
Oos = new ObjectOutputStream (New FileOutputStream ("Student.data"));//do not close the pipe;
Oos.writeobject (SB)
FOS = new FileOutputStream ("Student.data"); ("Student.data") the relative path of the file;
Oos = new ObjectOutputStream (FOS);
} catch (FileNotFoundException e) {
E.printstacktrace
} catch (IOException e) {
E.printstacktrace
} finally{
if (oos! = null) {
Oos.close ();
} catch (IOException e) {
E.printstacktrace
}
}
Object deserialization-Converts the input two mechanism stream into an in-memory object
Deserialization is the second way to produce objects in Java
Studentbean SB = null;
ObjectInputStream ois = null;
try{
OIS = new ObjectInputStream (New FileInputStream ("Student.data"));
SB = (Studentbean) ois.readobject ();
} catch (FileNotFoundException e) {
E.printstacktrace
} catch (IOException e) {
E.printstacktrace
} finally{
if (ois! = null) {
Ois.close ();
} catch (IOException e) {
E.printstacktrace
}
}
System.out.println (Sb.getname);
}
File class (Files Class):
can represent a file or folder (a folder is also a file)
public static void Main (string[] args) {
File class--to represent the operating system's files or folder objects
File File = new file ("F:/wudi.avi");
File dir = new file ("F:/ppt");
Common Methods for File objects
String path1 = File.getabsolutepath ();//Get absolute path
Stringpath2 =file.getpath ();//Get relative path
Long space =file.length ();//Get File size
Long time = file.lastmodified ();//Last Modified
SYSTEM.OUT.PRINTLN (path);
System.out.println (space);
System.out.println (new Date);
System.out.println (File.ishidden ());//Whether it is a hidden file
System.out.println (File.isfile ());//Whether it is a file (true Yes, Flo No)
System.out.println (Dir.isdirectory ());//Whether it is a folder
File Delimiter (file.pathseparator)
String Path = "D:" +file.pathseparator + "FFDFD" + file.pathseparator + "Ffafa";
Path = "D:" + system.getproperty ("file.separator") + "FDFDDFD" + system.getproperty ("file.separator") + "FDFDFD";
SYSTEM.OUT.PRINTLN (path);
Common Methods for Folder objects
System.out.println ("***************************************");//Split
string[] Subfilenames = Dir.list ();//The name of all sub-files or subfolders below the folder
for (String subfilename:subfilenames) {
System.out.ptintln (Subfilename ());
}
File[]subfiles = Dir.listfiles ();//File object to all sub-files or folders below the folder
for (File subfile:subfiles) {
System.out.ptintln (Subfile.getname ());
}
System.out.println ("***************************************");//Split
Interview body: to any folder, print out all the sub-files under the folder name (no matter how many layers) "use recursion"
String InputDir = Joptionpane. Showinputdialog ("Please enter not a folder. ”);
Showallsubfile (New File (InputDir));
public static void Showallsubfile (File die){
if (Dir.isdirectory ()) {
file[] Subfiles = Dir.listfiles ();
for (File subfile:subfiles) {
if (Subfile.isdirectory ()) {
Showallsubfile (Subfile);
}else {
System.out.println (Subfile.getabsolutepath ());
}
}
}else{
Joptionpane.showmessagedialog (NULL, "You are not entering a folder.) ”);
}
}
}
I/O system (input/output)