(Copyright statement: self-created works, please do not forward! Otherwise, legal responsibility will be pursued.
JAVAIO operation plan
Knowledge points this season
1. File class
2. RandomAccess class
File class
All IO operations in JAVA are fully stored in the java.io package.
File is only one operation related to the file itself.
It is necessary to take a complete approach in using the structured methods of the File class.
Request 1: You can create a new file on the F drive: the demo.txt file.
publicbooleancreateNewFile() throwsIOException
Take a look at the effect:
We found that the system prompts illegal escape characters. When learning regular expressions, "\" indicates escape. We need to enter two "\\", namely "f:\\demo.txt"
Require try/catch management
Now I found the file came out~
If we want to create the same file now, we will find that false is displayed. Since the file already exists, we cannot create the same file again~
Can I delete the demo.txt file?
publicbooleandelete()
ask:
If the file exists, delete the file, if the file does not exist, create the file.
Determine whether the file exists: publicbooleanexists()
be careful:
If the program is written in the above format, there will be a limitation:
·windows:\
·linux:/
Several delimiters are provided in the File class
publicstaticfinalStringseparator
publicStringgetPath()
Determine if the given path is a directory: publicbooleanisDirectory()
It shows that f:\demo.txt is not a directory~
One more look:
It shows that F:\TL-WN310GTL-WN350G is the directory ha~
This method can be used to determine whether a given path is a directory or not~
The request can list comprehensive files in the specified directory.
·Return the full way: publicStringlist();
Return the full File object: publicFilelistFiles();
1. Use string array operations
The files and directories in the F:\data directory can be listed normally~
2. Use the File class target array
Using the File class can open a way~
Thinking questions:
Constantly determine whether the given path is a directory or not, and if it is a directory, it will continue to be listed downwards. This kind of title can only be terminated by recursive operation.
Take a look at the effect:
Note: recursive operations may cause memory leaks~~~
I/O method
read and write process
RandomAccessFile
Suppose three sets of data are stored in a file:
AA80
BB90
CC99
RandomAccessFile class
publicRandomAccessFile(Filefile,Stringmode) throwsFileNotFoundException
publicRandomAccessFile(Filefile,--gt; the instantiation target of the File class
Stringmode)--gt; mode
throwsFileNotFoundException
There are two main modes in use today:
r: read only
w: write only
rw: read and write --gt; if there is no file, it will be created automatically when it is written.
publicfinalvoidwriteBytes(Strings) throwsIOException
publicfinalvoidwriteInt(intv) throwsIOException
Take a look at the effect:
Now we can inject data, let's read it out below~
In fact, when reading, it can only be read by byte, and the byte can only be read out one by one.
If you finish reading the data of the first self, then the pointer is before the second self. If you continue to read down, the data of the second self will be read. Let's test it~~
If we don't want to read the data of the first self, but directly read the data of the second self, see below~~
publicfinalbytereadByte() throwsIOException
publicintskipBytes(intn) throwsIOException
publicvoidseek(longpos) throwsIOException
Look at the effect, directly read the second self data ha~
If you want to read the third self data, you will skip 16 bytes~raf1.skipBytes(16);
Now we have been reading down, do we want to make the pointer read data back?
Take a look at the effect:
Although this class can implement read and write operations more sensitively, it is also faulty, because it is necessary to know the length of its hops.
Data reading classes used in real development: InputStream, OutputStream, Reader, Writer, we will find out next season~~~~
################################################## ############)