文件File:
File类只用于表示文件的信息,不能对文件的内容进行访问。
构造方法:File(String pathname) 文本分隔符File.separator
File(File parent, String child)
常用方法:isFile()
long length()
boolean exists()
boolean createNewFile()
boolean delete() 需要注意的是,若此File对象所表示的是一个目录时,在删除时需要保证此为空目录才可以成功删除(目录中没有其他的目录或文件)
isDirectory() 判断是否是一个目录
boolean mkdir()
boolean mkdirs()
File[] listFiles()
FileFilter接口:用于抽象路径名的过滤器
listFiles(FileFilter)方法用于返回满足过滤器要求的子项。
File[] list = dir.listFiles(new FileFilter(){
public boolean accept(File pathname) {
retrun pathname.getName().startsWith(".");
}
}
)
RandomAccessFile:
用于对文件进行随机的访问操作,包括读和写。该类的读写是基于指针的操作。
构造方法:RandomAccessFile(File file, String mode)
RandomAccessFile(String filename, String mode)其中mode表示访问模式,"r'表示只读, “rw”表示读写
常用的方法:
void write(int d) 写入一个int类型的“低8位”
int read() 读取一个byte填充到int的低8位,高24为0
void write(byte[] d) 向文件中写入一组字节
void write(byte[] d, int offset, int len)
int read(byte[] b) 返回值为实际读取到的字节量
void close()
long getFilePointer()
void seek(long pos) 将指针位置移动到pos位,从0开始指针位置,文件开始位置
int skipBytes(int n)
IO流:
inputStream是所有字节输入流的父类
常用方法:int read() int read(byte[] d)
outputStream是所有字符输出流的父类
常用方法:void write(int d) void write(byte[] b)
FileOutputStream文件输出流
构造方法:FileOutputStream(String filename) FileOutputStream(File file) FileOutputStream(File file, boolean append) FileOutputStream(String filename,boolean append)
FileInputStream文件输入流
构造方法:FileInputStream(File file) FileInputStream(String filename)
常用方法:int read() 若返回-1则表示已经读取到文件结尾了。
文件复制:
<pre name="code" class="java">FileInputStream fis = new FileInputStream("t1.txt");
FileOutputStream fos = new FileOutputStream("t2.txt");
iint len = -1;
byte[] b = new byte[10];
while((len=fis.read(b))!=-1) {
fos.write(b,0,len);
}
fis.close();
fos.close();
<pre name="code" class="java">FileInputStream fis = new FileInputStream("t1.txt");
FileOutputStream fos = new FileOutputStream("t2.txt");
int d = -1;while((d=fis.read())!=-1) { fos.write(d);}fis.close();fos.close()
缓冲流:
BufferOutputStream
BufferInputStream
FileInputStream fis = new FileInputStream("t1.txt");
FileOutputStream fos = new FileOutputStream("t2.txt");
BufferInputStream bis = new BufferInputStream(fis);
BufferOutputStream bos = new BufferOutputStream(fos);
int d = -1;
while((d=bis.read())!=1) {
bos.write(d);
}
bis.close();
bos.close();
对象流:
ObjectOutputStream void writeObject(Object o) 将一个对象序列化后写入
ObjectInputStream Object readObject() 反序列化转换为对应的对象
Serializable接口,该接口提供一个常量seriaVersionUID
transient关键字,进行减负
字符流:以字符为单位读取数据
Reader int read() int read(char[] chs) 返回实际读取到的字符量
Writer void write(int c) 低16位表示的字符 void write(char[] chs) void write(String str) void write(char[] chs, int offset, int len)
字符转换流:
InputStreamReader(InputStream in, String charsetName) 可以设置编码
OutputStreamWriter(OutputStream out, String charsetName) 设置编码
常用方法:read() write(String str)
PrintWriter:
构造方法:PrintWirte(File file)
PrintWirte(String fileName)
PrintWirte(OutputStream out)
PrintWirte(OutputStream out, boolean autoFlush)
PrintWirte(Writer write)
PrintWirte(Writer write , boolean autoFlush)
常用方法:println(String str) close()
BufferReader:
构造方法:
BufferedReader(Reader reader)
常用方法:
String readLine() 读到文件末尾则为Null
异常:
Throwable类是所有异常的根基类,Exception和Error是其派生的两个子类。
常见的RuntimeException
IllegalArgumentException NullPointerException ArrayIndexOutOfBoundsException ClassCaseException NumberFormatException