找回密码
 立即注册
theFilethrowsIOExceptio文件 | 企业管理 2022-09-23 94 0star收藏 版权: . 保留作者信息 . 禁止商业使用 . 禁止修改作品
版权声明:自创著作,谢绝转发!否则将追究法律责任。
JAVAIO操作方案
本季知识点
1、File类
2、RandomAccess类
File类
在JAVA中所有的IO操作全面存放在java.io包中。
File是仅有一个与文件自身相关的操作。
在使用File类的结构方法处有必要参加一个完整的途径。
请求一:可以在F盘上建立一个新的文件:demo.txt文件。
publicbooleancreateNewFile()throwsIOException
看下作用:
我们发现系统提示不合法转义字符,在前面学习正则时“\”表明转义,我们需求输入两个“\\”即“f:\\demo.txt”
需求进行try/catch管理
如今发现文件出来了哈~
假如如今再想创建同一个文件,我们会发现显示false,由于文件现已存在,不能再创建同一文件哈~
能否删除去demo.txt的文件呢?
publicbooleandelete()
请求:
假如文件存在则删除去文件,假如文件不存在,则创建文件。
判别文件是不是存在:publicbooleanexists()
注意点:
假如程序按以上格式写的话,则会有一个限制:
·windows:\
·linux:/
File类中提供了几切割符
publicstaticfinalStringseparator
publicStringgetPath()
判别给定的途径是不是是一个目录:publicbooleanisDirectory()
显示f:\demo.txt不是目录哈~
再来看一个:
显示F:\TL-WN310GTL-WN350G是目录哈~
通过这个方法可以判别给定的途径是不是是目录哈~
请求可以列出指定目录下的全面文件。
·回来完整途径:publicStringlist();
·回来全面的File目标:publicFilelistFiles();
1、使用字符串数组操作
可以正常列出F:\资料目录下的文件和目录哈~
2、使用File类目标数组
使用File类可以开出途径哈~
思考题:
不断的判别给定的途径是不是是目录,假如是目录,则持续向下列出,此种标题只能使用递归操作终结。
看下作用:
注意:递归操作可能会致使内存泄露问题~~~
输入输出方法
读写进程
RandomAccessFile
假定一个文件中存放了三组数据:
AA80
BB90
CC99
RandomAccessFile类
publicRandomAccessFile(Filefile,Stringmode)throwsFileNotFoundException
publicRandomAccessFile(Filefile,--gt;File类的实例化目标
Stringmode)--gt;模式
throwsFileNotFoundException
模式如今主要用两种:
·r:只读
·w:只写
·rw:读写--gt;假如没有文件,写时,会自动创建。
publicfinalvoidwriteBytes(Strings)throwsIOException
publicfinalvoidwriteInt(intv)throwsIOException
看下作用:
如今我们可以注入数据了,下面我们将其读出来哈~
实际上读的时候只能按byte读出,byte只能一个一个的往外读。
假如读完第一自己的数据,则此时指针在第二自己之前,假如持续往下读则会读取第二自己的数据,我们测试一下哈~~
假如我们想不读取第一自己的数据,直接读取第二自己的数据,看下面哈~~
publicfinalbytereadByte()throwsIOException
publicintskipBytes(intn)throwsIOException
publicvoidseek(longpos)throwsIOException
看下作用,直接读取第二自己数据了哈~
假如要读取第三自己数据即是跳过16个字节哈~raf1.skipBytes(16);
如今我们是一直往下读哈,我们要使指针往回读取数据呢?
看下作用:
尽管此类可以比较灵敏的实现读写操作,但是也是有故障的,由于有必要认识的其跳的长度。
真实开发中使用的数据读取类:InputStream、OutputStream、Reader、Writer,下季再会分晓哈~~~~
##############################################################

(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~~~~
################################################## ############)

[下载]10472346012.rar




上一篇:EJB3实例教材
下一篇:[零基础学JAVA]Java SE应用部分-28.Java IO操作(02)