输入流
字节输入流(FileInputStream):
字节输入流主要用于读取文件中的数据的时候用,比较适合读取字符之类的时候用,但是没怎么有效率,不太适合用于读取汉字。
构造方法: FileInputStream(String name/File file);
例: FileInputStream file = new FileInputStream(“aaa.txt”);
读出数据方法: int read();
int read(byte b[]);
int read(byte b[],int off,int len);
以byte b[]的方式读取文件,将读取到的字节存放到字节数组b[]中。
字节输入流中read()方法读取到文件末尾时返回-1。
字符输入流(FileReader):
字符输入流也用于读取数据,适合于读取汉字之类的数据,效率也不是特别高。
构造方法: FileReader(String name/File file);
例: FileReader fr = new FileReader(“hahah.txt”);
读取数据的方法: int read();
int read(char b[]);
int read(char b[],int off,int len);
以char b[]的方式读取文件,将读取到的字符存放到字符数组b[]中。
字符输入流中read()方法读取到文件的末尾时返回-1。
缓冲输入流(BufferedReader):
缓冲输入流增强了读取文件的能力,适用于按行读取文件。
构造方法: BufferedReader(Reader in);
例: FileReader fr = new FileReader(“haha.txt”);
BufferedReader br = new BufferedReader(fr);
读取数据的方法: public String readLine();
例: String s = br.readerLine();
读取文件的一行,赋给字符串s。
以String s的方式读取文件,将读取到的字符串放到字符串s中。
缓冲输入流中read()方法读取到文件的末尾是返回null。
数据输入流(DataInputStream):
数据输入流的好处在于读取数值时,不必再关心这个数值应当是多少个字节。
构造方法: DataInputStream(InputStream in);
例: FileInputStream fis = new FileInputStream(“haha.txt”);
DataInputStream dis = new DataInputStream(fis);
读取文件的方法:
对象输入流(ObjectInputStream):
对象输入流是对一个类进行读取的操作,所以,所操作的类必须实现Serializable接口。
构造方法: ObjectInputStream(InputStream in);
例: FileInputStream fis = new FileInputStream(“lala.txt”);
ObjectInputStream ois = new ObjectInputStream(fis);
读取文件的方法:readObject(Object obj);该方法返回一个读入时的类
例: Students li = (Students)ois.readObject();
输出流
字节输出流(FileOutputStream):
构造方法: FileOutputStream(String name/File file);
例: FileOutputStream fos = new FileOutputStream(“sss.txt”);
读入文件方法: public void write(byte b[]);
public void write(byte b[],int off,int len);
功能:写b[]个字节到文件。
字符输出流(FileWriter):
构造方法: FileWriter(String name/File file);
例: FileWriter fw = new FileWriter(“haah.txt”);
读入文件方法: public void write(int n);
public void Write(char b[]);
public void Write(char b[],int off,int len);
功能:写b[]个字符到文件。
缓冲输出流(BufferedWriter):
构造方法: BufferedWriter(Writer out);
例: FileWriter fw = new FileWriter(“hahahah.txt”);
BufferedWriter bw = new BufferedWriter(fw);
读入文件的方法: public void Write(String s);
public void Write(String s,int off,int len);
功能: 写字符串写到文件。
换行方法: newLine();
数据输出流(DataOutputStream):
构造方法: DataOutputStream(OutputStream out);
例: FileOutputStream fos = new FileOutputStream(“lala.txt”);
DataOutputStream dos = new DataOutputStream(fos);
读入文件的方法:
对象输出流(ObjectOutputStream):
构造方法: ObjectOutputStream(OutputStream out);
例: FileOutputStream fos = new FileOutputStream(“liji.txt”);
ObjectOutputStream oos = new ObjectOutputStream(fos);
读入文件的方法:WriteObject(object obj);
随机流(RandomAccessFile)
RandomAccessFile类既是输入流,又是输出流。
构造方法: RandomAccessFile(String name,String mode);
RandomAccessFile(File file,String mode);
mode的值只能取”rw”或”r”。
RandomAccessFile r = new RandomAccessFile(“tom.txt”,”rw/r”);
seek(long a): 用来定位读/写位置。其中,参数a用来确定读/写位置距离文件开头的字节个数.
getFilePointer(): 获取当前的读/写位置。
注意:RandomAccessFile流的readLine()方法在读取非ASCII字符的文件时(比如汉字的文件)会出现乱码现象,需要把readLine()读取的字符串用ISO-8859-1重新编码存放在byte数组中。
方法:(1) 读取字符串。
String str = in.readLine();
(2) 用ISO-8859-1重新编码。
byte b[] = str.getBytes(“ISO-8859-1”);
(3) 使用当前计算机默认编码将字节数组转化为字符串。
String s = new String(b);
读/写文件的方法:
数组流
字节数组输入流(ByteArrayInputStream):
构造方法: ByteArrayInputStream(byte []buf);
ByteArrayInputStream(byte []buf,int off,int len);
例:ByteArrayInputStream bis = new ByteArrayInputStream(b[]);
读取缓冲区的方法: public int read();
public int read(byte []b,int off,int len);
以byte buf[]的方式读取文件,将读取到的文件存放到字节数组buf[]中。
当读到文件的末尾时,返回-1。
字节数组输出流(ByteArrayOutputStream):
构造方法: ByteArrayOutputStream();
ByteArrayOutputStream(int size);
例:ByteArrayOutputStream b = new ByteArrayOutputStream();
读入缓冲区的方法: public void write(int b);
public void write(byte b[],int off,int len);
public byte[] toByteArray();该方法返回输出流写入缓冲区的所示字节。