输入、输出流

java.io包(I/O流库)中提供大量的流类

所有输入流都是抽象类InputStream(字节输入流)或抽象类Reader(字符输入流)的子类

所有输出流都是抽象类OutputStream(字节输出流)或抽象类Writer(字符输出流)的子类

File类:

//File类对象主要用来获取文件本身的一些信息,如文件所在目录、文件的长度或文件读写权限等

//File类不涉及对文件的读写操作

构造方法:

File(String filename)//默认file与程序在同一目录;

File(String directoryPath,String filename)

File(File dir,String filename)

filename是文件名,directortPath是文件的路径,dir为一个目录;

Java中File类的常用方法:

①获取文件本身信息

1.public String getName()

获取文件的名字;

2.public boolean canRead()

判断文件是否是可读的;

3.public boolean canWrite()

判断文件是否可被写入;

4.public boolean exists()

判断文件是否存在;

5.public long length()

获取文件的长度(单位是字节);

6.public String getAbsolutePath()

获取文件的绝对路径;

7.public String getParent()

获取文件的父目录;

8.public boolean isFile()

判断文件是否是一个普通文件,而不是目录;

9.public boolean isDirectory()

判断文件是否是一个目录;

10.public boolean isHidden()

判断文件是否是隐藏文件;

11.public long lastModified()

获取文件最后修改的时间(时间是从1970年午夜至文件最后修改时刻的毫秒数);

②获取目录本身信息

12.public boolean mkdir()

创建一个目录,成功返回true,否则返回false;

13.public String[] list()

用字符串形式返回目录下的全部文件;

14.public String[] list(FilenameFilter obj)

用字符串形式返回目录下的指定类型的所有文件;

15.public File[] listFiles()

用File对象形式返回目录下的所有文件;

16.public File[] listFiles(FilenameFilter obj)

用File对象形式返回目录下的指定类型的所有文件;

/*参数FilenameFilter是一个接口,该接口有一个方法:

public boolean accept(File dir,String name);*/

import java.io.*;
public class FileAccept implements FilenameFilter{
    private String extendName;
    public void setExtendName(String s){
        extendName="."+s;
    }
    public boolean accept(File dir,String name){    //重写接口的方法
        return name.endsWith(extendName);
    }
}
public class Main{
    public staitc void main(String args[]){
        File dirFile=new File(".");
        FileAccept fileAccept=new FileAccept();
        fileAccept.setExtendName("java"); 
        String fileName[]=dirFile.list(fileAccept);
    }
}

运行可执行文件:

java.lang.Runtime类使用该类的getRuntime()静态方法创建对象

方法:

exec(String command)

打开本地机器上的可执行文件或执行一个操作(如记事本程序和浏览器);

//通过file.getAbsolutePath()来获取绝对路径

(一)文件字节流(FileInputStream类和FileOutputStream类):

 //注意:顺序读取和写文件

//缺点:以字节为单位处理数据不能很好的操作Unicode字符,因为汉字占用2个字节,使用字节流会出现"乱码"现象

文件字节输入流(FileInputStream类):

构造方法:

FileInputStream(String name) throws IOException

使用给定的文件名name创建FileInputStream流;

FileInputStream(File file) throws IOException

使用File对象创建FileInputSteam流;

输入流方法:

int read()

从源(name或file)中读取单个字节的数据,返回字节值(0~255之间的一个整数),未读出字节返回-1;

int read(byte b[])

从源中试图读取b.length个字节到字节数组b中,返回实际读取的字节数目,未到达文件的末尾返回-1;

int read(byte b[],int off,int len)

从源中试图读取len个字节到字节数组b中的off指定的位置开始存放,返回实际读取的字节数目,未到文件的末尾返回-1;

close()

关闭流;

文件字节输出流(FileOutputStream类):

构造方法:

FileOutputStream(String name) throws IOException

FileOutputStream(String name,boolean append) throws IOException

FileOutputStream(File file) throws IOException

FileOutputStream(File file,boolean append) throws IOException

//append默认取值true,不刷新所指向的文件;

输出流方法:

void write()

向目的地写入单个字节;

void read(byte b[])

向目的地写入一个字节数组;

void read(byte b[],int off,int len)

给定字节数组中起始于off处取len个字节写到目的地;

close()

(二)文件字符输入流(FileReader类)和文件字符输出流(FileWriter类)

//特点:可更好识别Unicode字符,汉字等

构造方法:

FileReader(String filename) throws IOException

FileReader(File filename) throws IOException

FileWriter(String name) throws IOException

FileWriter(String name,boolean append) throws IOException

FileWriter(File file) throws IOException

FileWriter(File file,boolean append) throws IOException

输入流方法:

read()

close()

使用字符数组读取数据,以字符为基本单位;

输出流方法:

write()

close()

(三)缓冲流(BufferedReader类和BufferedWriter类)

//特点:以行来读写文件

构造方法:

BufferedReader(Reader in) throws IOException

BufferedWriter(Writer out) throws IOException

//可向参数in和out传递Reader子类对象(如FileReader的实例)和Writer子类对象(如FileWriter的实例)

//注意:缓冲流作为上层流,字符流作为底层流,只需要关闭上层流,底层流会自动关闭

输入流方法:

readLine()

读取文本行;

close()

输出流方法:

write(String s,int off,int len)

newLine()

写入一个回行符;

flush()

刷新缓存;

close()

(四)随机流(RandomAccessFile类)

//特点:可自行定位读写位置

//注意:随机流既不是InputStream和Reader的子类,也不是OutputStream和Writer的子类

//随机流不刷新文件

构造方法:

RandomAccessFile(String name,String mode)

参数name作为源,也作为目的地,参数mode取r(只读)或rw(可读写);

RandomAccessFile(File file,String mode)

方法:

seek(long a)

定位读写位置,参数a确定读写位置距离文件开头的字节个数;

getFilePointer()

获取流的当前读写位置;

length()

获取文件的长度;

read()

write()

close()

//更多方法另行查询

(五)数组流(字节数组流ByteArrayInputStream、ByteArrayOutputStream类和字符数组流CharArrayReader和CharArrayWriter类)

//特点:可用数组进行文件的操作

字节数组流的构造方法:

ByteArrayInputStream(byte[] buf)

ByteArrayInputStream(byte[] buf,int offset,int length)

ByteArrayOutputStream()

缓冲区大小默认为32字节,容量自动增加;

ByteArrayOutputStream(int size)

缓冲区大小为size个字节,容量自动增加;

输入流方法:

public int read()

顺序从源中读出一个字节,返回读出的字节值;

public int read(byte[] b,int off,int len)

返回实际读出的字节个数,未读出返回-1;

close()

输出流方法:

public void write(int b)

向缓冲区写入一个字节;

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

public byte[] toByteArray()

返回输出流写入到缓冲区的全部字节;

close()

(六)数据流(DataInputStream类和DataOutputStream类)

//特点:可以以各种风格读取文件,不必关心数值的字节

构造方法:

DataInputStream(InputStream in)

DataOutputStream(OutputStream out)

//方法自行查询

(七)对象流(ObjectInputStream类和ObjectOutputStream类)

构造方法:

ObjectInputStream(InputStream in)

ObjectOutputStream(OutputStream out)

方法:

readObject()

writeObject(Serializable ob)

参数Serializable是一个没有方法的接口,实现该接口的对象则为序列化的对象;

close()

猜你喜欢

转载自www.cnblogs.com/rongbin/p/9376959.html
今日推荐