File type and random read and write

1. File type:

1. Function: View the files or directories on the file system, etc. You can only
                  create, delete files (directories) or view their properties, but you cannot modify the contents of the files.
2. Constructor:
1) File(String pathname):   
  pathname: The parameter is a path, specifying the path of the file or directory;
(this path does not necessarily exist on the hard disk)
  The relative location of this constructor is under the project name folder.      2) File(String parentName,String childName)     creates a new File instance     based on the parent pathname string and the child pathname string . 3. File filtering interface Concept : File filtering interface FileFilter, which provides a method: accept(File file), which can filter files. File file=new File(".");//Indicates the current directory where it is located. --Files and directories under the current project name. FileFilter filter=new FileFilter(){ /* * true: keep the file name we need * false: not keep the file * callback: internal logic calls this method */ public boolean accept(File file){














return file.getName().startsWith(".");
}
};
File[] subs=file.listFiles(filter);//
if(subs!=null&&subs.length>0){
for(File s:subs) {
System.out.println(s.getName());
}
}
  4. Path
1) The path separator of the file operating system is different.
For example:
    / File.separator under linux under 
    windows: constant, the system will return different separators according to different operations.         2) Relative path: relative to a directory.          . : Indicates the current directory where it is located, that is, the files and directories under the current project name      .. : Indicates returning to the previous directory 3) Absolute path: (not recommended)            The root directory is used as the reference, that is, it needs to start from the root directory to write.            windows: c:\a\a.txt             linux: /home/a.txt    5. Method 1) exists(): Determine whether the specified path exists on the hard disk;










2) listFiles(): Returns all files and directories in this directory; -- there can be filter parameters in it
3) createNewFile: createNewFile(): Create a file;
its role: When created, it may not be successful. When
the parent directory of the *file to be created does not exist, the creation fails.
* So this method will throw an exception.
4) isFile(): judges whether the specified file is a file;
5) mkdir(): the function of creating a folder; a layer of directories will be created
6) mkdirs(): a multi-layer directory will be created;
7) delete(): file Delete;
-- When the path specified by the constructor is a single layer:
  if it is a file, it will be deleted directly.
  If it is a directory, if the directory is empty, it can be deleted directly; if the
      directory is not empty, the deletion fails;
  --When the path specified by the constructor is multiple layers:   the last layer is a file, and only this file will be deleted   The last layer is a directory ,           when the directory is empty, only the next layer will be deleted;           when the directory is not empty, the deletion will fail; deletion logic: delete the innermost layer first, and then delete the upper layer after the inner layer is deleted.         8) getName(): The current file name.






2. Random read and write file types

1. RandomAccessFile(File pathName, String mode):
file: To specify the path or file name of a file;
mode: 'r'/'rw';
Its function is to modify (read, write) the contents of the file.
2. Constructor:
  RandomAccessFile(File file, String mode)
  RandomAccessFile(String filepath, String mode)
3. Common methods:
1) int read():
    Read a byte and put it into the lower eight bits of the return value. The return value is int type
2) write(int num):
     write out the lower eight bits of the binary of the parameter num.
Other methods:
   1)write(byte[] bytes)
   2)write(byte[] bytes,int startIndex,int length):
When operating on a file, there is a pointer at the bottom layer.
1) getFilePointer(): Get the position of the file pointer.
  2) seek(int index): Move the pointer to a certain position.   
  3) read(byte[] bytes): Store the read bytes (referring to the content previously written in the file) into the byte array, and the return value is
       the number of valid bytes read. When it returns -1, it means that the end of the file has been read.
  4) write(byte[] by): Write the contents of the byte array to the file.
The methods involved:
str.getBytes("utf-8"): Convert the string to the corresponding byte array according to the default character set.
String str1 = new String(by, "gbk"): by is a byte array, compiled according to gbk encoding.

Convert to the corresponding string.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325511699&siteId=291194637