MU Lesson Network _ File Transfer base--java IO stream

Source: Internet
Author: User
Tags create directory

Fadf

1th. Encoding of the document

1-1 encoding of the file (15:07)

2nd. Use of the File class

2-1 File Class Common API introduction (10:50)

Import Java.io.file;import Java.io.ioexception;public class Filedemo {public static void main (string[] args) {File File = New File ("E:\\helloworld\\java"); System.out.println (File.exists ());//Determine if the file/folder exists if (!file.exists ()) {file.mkdirs ();//Create Directory} else {file.delete ();} is a directory System.out.println (File.isdirectory ());//is a file System.out.println (File.isfile ()); File File2 = new file ("E:\\helloworld\\1.txt"), if (File2.exists ()) {try {file2.createnewfile ();//Create Files} catch ( IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} else {file2.delete ();} System.out.println (file);//file.tostring () content System.out.println (File.getabsolutepath ()); System.out.println (File.getname ()); System.out.println (File.getparent ());}}

2-2 traversing the directory (10:26)

Import Java.io.file;import java.io.ioexception;//lists some common actions for file such as filtering, traversing, and so on. Class FILEUTILS1 {/* * Lists all files under the established directory (including their subdirectories) * /public static void ListDirectory (File dir) throws IOException {if (!dir.exists ()) {throw new IllegalArgumentException ("Mesh "+ dir +" does not exist ");} if (!dir.isdirectory ()) {throw new IllegalArgumentException (dir + "not Directory");} string[] filenames = dir.list ();//Returns the name of the immediate child of the string array, not including the contents under subdirectories for (string string:filenames) {System.out.println (dir + "\ \ "+ string);}}} Class FILEUTILS2 {/* * Lists all files under the established directory (including its subdirectories) */public static void ListDirectory (file dir) throws IOException {if (!dir.exis TS ()) {throw new IllegalArgumentException ("directory:" + dir + "does not exist");} if (!dir.isdirectory ()) {throw new IllegalArgumentException (dir + "not Directory");} If you want to iterate through the contents of the subdirectory, you need to construct the file object to do the recursive operation, file provides a direct return to the file object apifile[] files = dir.listfiles ();//Returns a direct subdirectory (file) of the object if (Files! = Null && files.length > 0) {for (file file:files) {if (File.isdirectory ()) {listdirectory (file);} else {System . out.println (file);}}}} PubliC class Fileutiltest {public static void main (string[] args) throws IOException {fileutils2.listdirectory (New File ("e:\\k MS "));}}

Chapter 3rd use of Randomaccessfile class

3-1 Randomaccessfile Basic operation (17:16)

Import Java.io.file;import java.io.ioexception;import java.io.randomaccessfile;import java.util.Arrays;/* * The Java.io.File class is used to represent files (directories) * The file class is used only to represent the file (directory) information (name, size, etc.) and cannot be used for access to the contents of the file * * Randomaccessfile Java provides access to the contents of the file, both read and write files. * Randomaccessfile supports random access to files and can access any location of the file. * * (1) Java file model * file on hard disk is a byte byte stored, is a collection of data * * (2) Open file * There are two modes "RW" (Read and write) "R" (read-only) * Randomaccessfile raf=new Randoma Ccessfile (file, "RW"); * file pointer, open file when pointer at the beginning pointer=0; * * (3) write the file * raf.write (int); write only one byte (the last 8 bits), and the pointer points to the next position, ready to write again * * (4) Read the file * int b=raf.read (); Read a byte * * (5) file read and write must be closed after completion (Ora CLE official description) */public class Rafdemo {public static void main (string[] args) throws IOException {file demo = new File ("demo"); F (!demo.exists ()) {demo.mkdirs ();} File File = new file (demo, "Raf.dat"), if (!file.exists ()) {file.createnewfile ();} Randomaccessfile RAF = new Randomaccessfile (file, "RW");//Pointer position System.out.println (Raf.getfilepointer ()); Raf.write (' A ');//write only one byte of System.out.println (Raf.getfilepointer ()); Raf.write (' B '); int i = 0X7FFFFfff;//Write method can be written only one byte at a time, if you want to write I will have to write 4 times raf.write (i >>> 24);//high 8-bit raf.write (i >>>); Raf.write (i >>> 8); Raf.write (i); System.out.println (Raf.getfilepointer ());//You can write a intraf.writeint (i) directly; String s = "medium"; byte[] GBK = s.getbytes ("GBK"); Raf.write (GBK); System.out.println (Raf.length ());//Read the file, you must move the pointer to the head raf.seek (0);//One-time read, read the contents of the file into a byte array byte[] buf = new byte[(int) Raf.length ()];raf.read (BUF); System.out.println (arrays.tostring (BUF)); string S1 = new String (buf, "GBK"); System.out.println (S1); for (byte b:buf) {System.out.println (integer.tohexstring (b & 0xff) + "");} Raf.close ();}}

Chapter 4th Byte Stream

4-1-byte stream file input stream FileInputStream-1 (15:09)

4-2-byte stream file input stream FileInputStream-2 (08:40)

4-3-byte stream file output stream FileOutputStream (13:24)

Data input and output stream of 4-4-byte stream (11:08)

4-5-byte buffered stream (17:54)

5th Chapter Character Stream

5-1-byte character conversion stream (18:09)

5-2 character stream file read and write streams (05:56)

Filter for 5-3 character streams (10:25)

The 6th chapter, serialization and deserialization of objects

6-1 serialization of basic operations (10:30)

6-2 Transient and ArrayList source analysis (12:41)

6-3 serialization of a neutron parent constructor problem (11:43)

Fasdfaf

MU Lesson Network _ File Transfer base--java 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.