第十周课程总结

一.File类的基本操作介绍

1.File类的构造方法:

public File(String pathname)     //实例化File类的时候,必须设置好路径。

 

 

2.File类中的主要方法和常量

方法或常量 类型 描述
public static final String pathSeparator 常量 表示路径的分隔符(windows是:“;”)
public static final String separator 常量 表示路径的分隔符(windows是:"\")
public File(String pathname) 构造 创建File类对象,传入完整路径
public File(File parent,String child) 构造 根据指定的父路径创建子文件
public boolean creatNewFile() throws IOException 普通 创建新文件
public boolean delete() 普通 删除文件
public boolean exists() 普通 判断文件是否存在
public boolean isDirectory() 普通 判断给定的路径是否是一个目录
public long length() 普通 返回文件的大小
piblic String[] list() 普通 列出指定目录的全部内容,只是列出了名称
public String[] listFiles() 普通 列出指定目录的全部内容,只是列出了名称
public boolean mkdir() 普通 创建一个目录
public boolean mkdirs() 普通 创建一个多级目录
public boolean renameTO(File dest) 普通 为已有的文件重新命名
public long lastModified() 普通 取得文件的最后一次修改日期时间
public File getparentFile() 普通 取得当前路径的父路径

 

 

 

 

 

 

 

 

 

 

 

二.RandomAccessFile类

方法 类型 描述

public RandomAccessFile(File file,String mode)

throws FileNotFoundException

 构造

接受File对象,指定操作路径,但是在设置时需要设置模式,“r”只为读,

“w”只为写,“rw”为读写

public RandomAccessFile(String name,String mode)throws

FilesNOtFoundException

构造 不再使用File类对象表示文件,而是直接输入一个固定的文件路径
public void close() throws IOException 普通 关闭操作
public int read (byte[] b)throws IOException 普通 将内容读取到一个byte数组之中
public final int readInt() throws IOException 普通 从文件中读取整型数据
public final byte readByte() throws IOException 普通 读取一个字节
public void seek(long pos)throws IOException 普通 设置读指针的位置
public fina void writeBytes(String s)throws IOException 普通 将一个字符串写入到文件中,按字节的方式处理
public fina void writeInt(int v) throws IOException 普通 将一个Int型数据写入文件,长度为4位
public int skipByte(int n)throws IOException 普通 指针跳过多少个字节

 

 

 

 

 

 

 

 

 

需要注意的是,如果使用了rw的方式声明RandomAccessFile对象时,要写入的文件不存在。系统将自动进行创建。

 

三.字节流与字符流

一.操作主要方法

(1)使用File类打开一个文件

(2)通过字节流或字符流的子类指定输出的位置

(3)进行读/写操作

(4)关闭输入/输出

 

outputString类常用方法

方法或常量 类型 描述
public void close() throws IOException 普通 关闭输出流
public void flush() throws IOException 普通 刷新缓存区
public void write(byte[] b)throws IOException 普通 将一个byte数组写入数据流

public void write(byte[] b,int off,int len throws)

IOException

普通 j将一个指定范围的byte数组写入数据流
public abstract void write(int b)throws IOException  普通 将一个字节流数据写数据流

 

 

 

 

 

 

InputStream类常用方法

方法或常量 类型 描述
public void available() throws IOException 普通 可以取得输入文件的大小
public void close() throws IOException 普通 关闭输入流
public abstract int read() throws IOException 普通 读取内容,以数字的方式读取
public int read(byte[] b)throws IOException 普通 将内容读到byte数组之中,同时返回读入的个数

 

 

 

 

write类的操作方法

方法或常量 类型 描述
public abstract void close() throws IOException 普通 关闭输出流
public void write (String str)throws IOException 普通 将字符串输出
public void write (char[] cbuf)throws IOException 普通 将字符数组输出
public abstract void flush() throws IOException 普通 强制性清空缓存

 

 

 

 

Reader类的常用方法

方法或常量 类型 描述
public abstract void close() throws IOException 普通 关闭输出流
public int read() throws IOException 普通 读取单个字符
public int read(char[] cbuf)throws IOException 普通 将内容读到字符数组之中,返回读入的长度

 

 

 

 

posted @ 2019-11-01 13:56  你听说过妖刀吗  阅读(110)  评论(1编辑  收藏  举报