RandomAccessFile创建文件java.io.FileNotFoundException

来源:互联网 发布:js设置input文本颜色 编辑:程序博客网 时间:2024/06/07 21:28

今天突然发现经常用的一个下载文件的任务,测试那边环境报错。结果原因是:


 RandomAccessFile 不能创建多级目录,比如想要创建/data0/a/b.csv 必须先手动创建 /data0/a  文件夹.


RandomAccessFile类的常用的操作方法
1、public  RandomAccessFile(File file, String mode)throws FileNotFoundException  构造方法  接收File类的对象,指定操作路径,但是在设置时需要设置模式:"r": 只读、"w": 只写、"rw": 读写。
2、public RandomAccessFile(String name, String mode) throws FileNotFoundException 构造方法 不再使用File类对象表示文件,而是直接输入了一个固定的文件路径。
3、public void close() throws IOException   关闭操作
4、public int read(byte[] b) throws IOException 将内容读取到一个byte数组之中
5、public final byte readByte()  throws IOException 读取一个字节
6、public final int readInt()  throws IOException从文件中读取整型数据。
7、public void seek(long pos) throws IOException 设置读指针的位置。
8、public final void writeBytes(String s) throws IOException 将一个字符串写入到文件之中,按字节的方式处理。
9、public final void writeInt(int v) throws IOException 将一个int型数据写入文件,长度为4位。
10、public int skipBytes(int n) throws IOException 指针跳过多少个字节。
构造:public RandomAccessFile(File file, String mode)  throws FileNotFoundException
实例化此类的时候需要传递File类,告诉程序应该操作的是哪个文件,之后有一个模式,文件的打开模式,常用的两种模式:
r:读模式
w:只写
rw:读写,如果使用此模式,如果此文件不存在,则会自动创建。


原创粉丝点击