File Deliverable (Package java.io)
For the operation of the class file.
Common methods: 1 Constructors:. Public File ( String pathname); // incoming file path and file name;
2.public boolean isFile (); // determine whether the incoming path of the file;
3.public boolean createNewFile () throws IOException; // create a file, if the file exists creation failed, and IO streams are not the same;
4. public Boolean the isDirectory (); // determines whether the incoming path to the folder;
5. public boolean mkdirs (); // create multi-level directory, if the directory exists creation fails;
6. public boolean the Delete (); // delete failed return false;
7. public void deleteOnExit (); // delete the specified file when the program exits;
8. The public Boolean EXISTS (); // determine the file or folder exists;
Package second.study; Import java.io.BufferedWriter; Import java.io.File; Import java.io.FileWriter; Import java.io.IOException; Import of java.util.ArrayList; Import java.util.List; / ** * the absolute path of the folder stored in the specified file * * @param args * / public class the Test { public static void main (String [] args) throws IOException { file the dir = new new file ( "F.: \\ \\ JAVA_study Code " ); List <File> List =new new the ArrayList <File> (); IF (! dir.exists ()) { the throw new new a RuntimeException ( "folder does not exist" ); } file2List (the dir, List); File desFile = new new File (the dir, "dirname.txt " ); list2File (List, desFile.toString ()); } / ** * the content folder stored in the development of the collection * * @param dir * @param List * / public static void file2List (file dir, List <File> List) { File [] Files= Dir.listFiles (); for (File File: Files) { IF (file.isDirectory ()) { file2List (File, List); } the else { List.add (File); } } } / ** * list the files stored in the specified file * @param List * @param desString * / public static void list2File (List <file> List, String desString) { BufferedWriter, BUFW = null ; the try { BUFW= new BufferedWriter(new FileWriter(desString)); for (File file : list) { bufw.write(file.getAbsolutePath()); bufw.newLine(); bufw.flush(); } } catch (Exception ex) { throw new RuntimeException("目的地文件创建错误"); } finally { try { if (bufw != null) bufw.close(); } catch(Exception EX) { the throw new new a RuntimeException ( "file close destination error" ); } } } }
Object stream (ObjectInputStream, ObjectOutputStream)
That is the object persistence
ObjectInputStream
Main method: 1 constructor: public the ObjectInputStream ( the InputStream in) throws IOException ; // stream associated with the object file;
2. public Final Object readObject () throws IOException, ClassNotFoundException; // read the object file;
ObjectOutputStream
Main method: 1 constructor: public the ObjectOutputStream ( the OutputStream OUT) throws IOException ; // the stream associated with the object file;
2. public Final void the writeObject ( Object obj) throws IOException ; // object is written to a file;
Package Penalty for second.study; Import the java.io.Serializable; public class the Person the implements Serializable { // current understanding: generally should not be covered, but will not cover the eclipse yellow exclamation mark. Private static Final Long serialVersionUID = 1L ; Private String name; // if you do not want the value of other sequences can be used transient keyword. transient Private int Age; // now serialization methods zone data up? static String Country = "CN" ; public the Person (String name, int Age, String Country) { Super(); this.name = name; this.age = age; Person.country = country; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + ", country=" + country + "]"; } }
package second.study; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class Test { public static void main(String[] args) throws Exception { writeObj(); readObj(); } public static void writeObj() throws Exception { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("obj.txt")); oos.writeObject(new Person("Zhangsan", 20, "kr")); oos.close(); } public static void readObj() throws Exception { ObjectInputStream oin = new ObjectInputStream(new FileInputStream("obj.txt")); Person p = (Person)oin.readObject(); oin.close(); System.out.println(p); } }
RandomAccessFile
This class is not a subclass of IO system, but directly inherit the Object class. But it is a member of the IO package, because it has to read and write methods. An array inside the package, can be operated by means by elements of the array for obtaining the getFilePointer position of the pointer () method , and can use the seek () method sets the position of the pointer .
Main methods: 1 Constructors:. Public RandomAccessFile ( File File, String the MODE) throws FileNotFoundException ; // can only manipulate files, mode: "r", " rw", "rws", "rwd".
2.构造函数:public RandomAccessFile(String name, String mode) throws FileNotFoundException;
3. public Long the getFilePointer () throws IOException ; // Get pointer location
4. public void Seek ( Long POS) throws IOException ; // set the position of the pointer
5. The public int the skipBytes ( int n-) throws IOException ; // number of bytes to skip, returns the number of bytes actually skipped
DataInputStream(java.io)
For operating the basic data types. The basic data types can be directly written to the file without having to convert the basic data types into bytes.