Javase Study notes: Chapter 11th Data Flow

Source: Internet
Author: User

First, the file class, where the package java.io
File F=new files ("e:\\ tool \\FeiQ\\1.txt");//create three different ways
File F1=new file ("e:\\ tool \\FeiQ\\", "1.txt");
File F2=new file ("e:\\ tool \\FeiQ");
File F3=new file ("F:\\t1\\t2\\t3");

Boolean b=f.isfile ();//Determine if the file
System.out.println (b);
Boolean b1=f2.isdirectory ();//Determine if the directory
System.out.println (B1);
Long L=f1.length (); Length of file, unit: bytes
System.out.println (l);
Long l1=f1.lastmodified ();//Returns the number of milliseconds from 1970-1-1
Date d=new date (L1);
SimpleDateFormat sdf=new SimpleDateFormat ();
Sdf.applypattern ("Yyyy-mm-dd HH:mm:ss sss");
String S=sdf.format (d);
System.out.println (s);//Print Last modified time
Boolean B2=f1.ishidden ();//Determine if hidden
Boolean b3=f1.canwrite ();//Determine if writable
Boolean b4=f1.exists (); Determine if a file or directory exists
if (F3.getparentfile ()!=null) {
System.out.println (F3.getpath ());//print path
}
Create a Directory
System.out.println (F3.mkdirs ());
System.out.println (F1.getname ());//Print file name
Print all files in the directory
File [] Files=f2.listfiles ([filter]);
for (int i=0;i<files.length;i++) {
System.out.println (Files[i]);
}

}
The flow is a set of ordered, the beginning and the end of the byte set, is the computer data transmission in general or abstract.
BYTE-stream: Stream called bytes by transmission unit
Character stream: Streams that are characters in the transferred units are called character streams
According to the direction of transmission: input stream and output stream, the program reads data from data stream in the data source called the input stream, writes the data from the current program to the stream to send the information to the destination end
Subdivided into: Byte input stream, byte output stream character input stream character output stream
1, InputStream byte input stream, where the package java.io
FileInputStream is a subclass of InputStream
2, OutputStream byte output stream, where the package java.io
FileOutputStream is a subclass of OutputStream
If the file does not exist when the file is written, a file is created automatically but the directory is not created automatically
Write line break with \ r \n,linux under \ n

Eg: copying files to another disk
public static void Main (string[] args) {
/*
* Read the fly-Autumn file
*/

FileInputStream In=null;
FileOutputStream Out=null;
try {
Create a file input stream, write the file to the string s
in = new FileInputStream ("C:\\Program Files\\feiq\\feiqcfg.xml");
Creates a file output stream that outputs the string s to the D-disk a.txt,true for Append output
Out=new FileOutputStream ("D:\\a.txt", false);
Byte[] b=new byte[1024];//Create 1024 bytes
int len=0;

while ((Len=in.read (b))!=-1) {
Out.write (b, 0, Len);
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
finally{
if (in!=null) {
try {
In.close ();//Close input stream
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (out!=null) {
try {
Out.close ();//Close output stream
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}

}
}

Three, character input stream FileReader FileReader is a subclass of InputStreamReader
Character output stream FileWriter FileWriter is a subclass of OutputStreamWriter
File Source=new file ("D:\\a.txt");
File Dest=new file ("E:\\cc.txt");
FileReader In=null;
FileWriter Out=null;
try {
In=new FileReader (source);
Out=new FileWriter (dest);
Char [] chs=new char[1024];
int len=0;
while ((Len=in.read (CHS))!=-1) {

Out.write (CHS, 0, Len);

}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
finally{
if (in!=null) {
try {
In.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
if (out!=null) {
try {
Out.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
The above four streams are node streams (with throttling)
Four, compressed output stream Zipoutputstream

File Source=new file ("D:\\a.txt");
File Dest=new file ("D:\\1.rar");
InputStream in=new FileInputStream (source);
Zipoutputstream zout=new Zipoutputstream (New FileOutputStream (dest));
ZipEntry zip=new ZipEntry (Source.getname ());//define ZIP entry
Zout.putnextentry (Zip);//Put in the next entry
int tem=0;
while ((Tem=in.read ())!=-1) {
Zout.write (TEM);
}
In.close ();
Zout.close ();
Five, filter flow
To add some functions to the node stream, it is necessary to use the node flow
Filteroutputstream
1, Bufferedoutputstream is the subclass of Filteroutputstream, implement the cache function
File File=new file ("D:\\a.txt");
OutputStream out=new FileOutputStream (file,true);
Bufferedoutputstream bf=new Bufferedoutputstream (out);
String s= "I like Java11";
byte [] b= s.getbytes ();
Bf.write (b);
Bf.flush ();
Bf.close ();

2, the ReadLine method of BufferedReader
Reader re=new FileReader ("D:\\a.txt");
BufferedReader br=new BufferedReader (re);
String s= br.readline ();
System.out.println (s);
The conversion stream converts characters into byte streams or converts bytes into character streams called conversion streams InputStreamReader
InputStreamReader in=new InputStreamReader (New FileInputStream ("D:\\a.txt")); Defining the Transformation Stream
Char c[] =new char[1024];
int len= In.read (c);
System.out.println (New String (c));
InputStreamReader in1=new InputStreamReader (system.in);//convert character flow to byte stream
BufferedReader bf=new BufferedReader (in1);//ReadLine method using the buffer stream
String s1= bf.readline ();
System.out.println (S1);

Vii. Object Flow
ObjectOutputStream Object Output stream
Method with WriteObject
ObjectInputStream Object Input Stream
Method with ReadObject

Job: 1, read a picture, the actual number of bytes read out of print, and then print how many times
2. Copy a picture to another disk
3. Copy oversized files to a secondary disk and add percentages

Javase Study notes: Chapter 11th Data Flow

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.