Implementing object-Oriented Programming with Java (10)

Source: Internet
Author: User

Look at the properties of the file:
System.out.println (F.getname ());//File name

System.out.println (F.length ());//byte size

System.out.println (F.getabsolutepath ());//absolute path

System.out.println (F.canread ());//Whether the file is readable

System.out.println (F.canwrite ());//whether writable

System.out.println (F.isdirectory ()); Whether it is a folder

File Related knowledge:

File F = new file ("D:/java/aaa.txt"); //Locate the target file

To create a file read object
Input byte stream for file
FileInputStream fis = new FileInputStream (f);

Read file
Byte[] B = new byte[(int) f.length ()];
If you read the last-1 means no more reading
General wording
int i = 0;
while ((i = Fis.read ())!=-1) {
System.out.println ((char) i);
//}

Advanced notation
Byte[] B = new byte[(int) f.length ()];
Reads all the contents of the file into a byte array.
Fis.read (b, 0, b.length);
for (int i = 0; i < b.length;i++) {
System.out.println ((char) b[i]);
}

Write
Output byte stream for file
FileOutputStream fos = new FileOutputStream ("C:/bbb.txt");
Input
Fos.write (b);

Fis.close ();
Empty the memory
Fos.flush ();
Fos.close ();

Wrap a byte stream into a character stream
CharSet American Character Set iso-8859-1
Give a character set of encoding format Chinese Utf-8
InputStreamReader ISR = new InputStreamReader (FIS, "utf-8");

Implementing object-Oriented Programming with Java (10)

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.