《java中输入输出流 ,java中文件操作是很重要的》
2011-11-08 13:28
330 查看
1、FileInputStream类
FileInputStream(String name) 使用给定的文件名name创建一个FileInputStream对象
FileInputStream(File file) 使用File对象创建FileInpuStream对象
File类有两个常用方法:
File(String s) s确定文件名字
File(String directory,String s) directory是文件目录
例如:
File f=new File("Myfile.dat");
FileInputStream istream=new FileInputStream(f);
处理I/O异常
当出现I/O错误的时候,Java生成一个IOException(I/O异常)对象来表示这个错误的信号。
程序必须使用一个catch检测这个异常
例如:
try{
FileInputStream ins= new FileInputStream("Myfile.dat");
}
catch(IOException e){
System.out.println("File read Error:"+e);
}
从输入流中读取字节
int read() 返回0~255之间一个整数,如果到输入流末尾,则返回-1
int read(byte b[]) 读取字节数组
int read(byte b[],int off,int len) off指定把数据存放在b中什么地方,len指定读取的最大字节数
关闭流
close()
2、FileOutputStream类
FileOutputStream(String name) 使用指定的文件名name创建FileOutputStream对象
FileOutputStream(File file) 使用file对象创建FileOutputStream对象
FileOutputStream(FileDescriptor fdobj) 使用FileDescriptor对象创建FileOutputStream对象
3、FileReader类和FileWriter类
FileReader(String filename)
FileWriter(String filename)
处理时需要FileNotFoundException异常
4、RandomAccessFile类
RandomAccessFile不同于FileInputStream和FileOutputStream,不是他们的子类
当我们想对一个文件进行读写操作的时候,创建一个指向该文件的RandomAccessFile流就可以了
RandomAccessFile类有两个构造方法:
RandomAccessFile(String name, String mode) name是文件名,mode取r(只读)或rw(读写)
RandomAccessFile(File file,String mode) file给出创建流的源
seek(long a) 移动RandomAccessFile流指向文件的指针,a确定指针距文件开头的位置
getFilePointer() 获取当前文件的指针位置
close() 关闭文件
getFD() 获取文件的FileDescriptor
length() 获取文件长度
read() 读取一个字节数据
readBoolean() 读取一个布尔值
readByte() 读取一个字节
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 读取一个UTF字符串
setLength(long newLength) 设置文件长度
skipByte(int n) 在文件中跳过给定数量的字节
write(byte b[]) 写b.length个字节到文件
writeBoolean(bolean b)
writeByte(int v)
writeChar(char c)
writeChars(String s)
writeDouble(double d)
writeFloat(float v)
writeInt(int i)
writeLong(long l)
writeShort(int i)
writeUTF(String s)
5、管道流
PipedInputStream类
PipedInputStream() 创建一个管道输入流
PipedInputStream(PipedOutputStream a) 连接到输出流a的输入流
read() 从输入流中读取一个字节
read(byte b[],int off,int len) off是在b中的开始位置,len是字节长度
PipedOutputStream类
PipedOutputStream() 创建一个输出流
PipedOutputStream(PipedInputStream a) 连接到输入流a的输出流
write(int b)
write(byte b[],int off,int len)
counnect() 连接输入输出流
close() 关闭流
在使用的时候要捕获IOException异常。
6、数据流
DataInputStream类(数据输入流)
DataInputStream(InputStream in) 将数据输入流指向一个由in指定的输入流
DataOutputStream类(数据输出流)
DataOutputStream(OutputStream out) 将数据输出流指向一个由out指定的输出流
主要方法:
close()
read() 读取一个字节数据
readBoolean() 读取一个布尔值
readByte() 读取一个字节
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 读取一个UTF字符串
skipByte(int n) 在文件中跳过给定数量的字节
write(byte b[]) 写b.length个字节到文件
writeBoolean(bolean b)
writeByte(int v)
writeChar(char c)
writeChars(String s)
writeDouble(double d)
writeFloat(float v)
writeInt(int i)
writeLong(long l)
writeShort(int i)
writeUTF(String s)
7、对象流
ObjectInputStream类和ObjectOutputStream类分别是DataInputStream类和DataOutputStream类的子类
8、回压输入流
PushbackInputStream类
PushbackInputStream(InputStream in)
PushbackReader类
PushbackReader(Reader in)
unread(char c) 回压一个字符
unread(char c[]) 回压数组c中全部字符
unread(char c[],offset,int n) 回压c中从offset开始的n个字符
FileInputStream(String name) 使用给定的文件名name创建一个FileInputStream对象
FileInputStream(File file) 使用File对象创建FileInpuStream对象
File类有两个常用方法:
File(String s) s确定文件名字
File(String directory,String s) directory是文件目录
例如:
File f=new File("Myfile.dat");
FileInputStream istream=new FileInputStream(f);
处理I/O异常
当出现I/O错误的时候,Java生成一个IOException(I/O异常)对象来表示这个错误的信号。
程序必须使用一个catch检测这个异常
例如:
try{
FileInputStream ins= new FileInputStream("Myfile.dat");
}
catch(IOException e){
System.out.println("File read Error:"+e);
}
从输入流中读取字节
int read() 返回0~255之间一个整数,如果到输入流末尾,则返回-1
int read(byte b[]) 读取字节数组
int read(byte b[],int off,int len) off指定把数据存放在b中什么地方,len指定读取的最大字节数
关闭流
close()
2、FileOutputStream类
FileOutputStream(String name) 使用指定的文件名name创建FileOutputStream对象
FileOutputStream(File file) 使用file对象创建FileOutputStream对象
FileOutputStream(FileDescriptor fdobj) 使用FileDescriptor对象创建FileOutputStream对象
3、FileReader类和FileWriter类
FileReader(String filename)
FileWriter(String filename)
处理时需要FileNotFoundException异常
4、RandomAccessFile类
RandomAccessFile不同于FileInputStream和FileOutputStream,不是他们的子类
当我们想对一个文件进行读写操作的时候,创建一个指向该文件的RandomAccessFile流就可以了
RandomAccessFile类有两个构造方法:
RandomAccessFile(String name, String mode) name是文件名,mode取r(只读)或rw(读写)
RandomAccessFile(File file,String mode) file给出创建流的源
seek(long a) 移动RandomAccessFile流指向文件的指针,a确定指针距文件开头的位置
getFilePointer() 获取当前文件的指针位置
close() 关闭文件
getFD() 获取文件的FileDescriptor
length() 获取文件长度
read() 读取一个字节数据
readBoolean() 读取一个布尔值
readByte() 读取一个字节
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 读取一个UTF字符串
setLength(long newLength) 设置文件长度
skipByte(int n) 在文件中跳过给定数量的字节
write(byte b[]) 写b.length个字节到文件
writeBoolean(bolean b)
writeByte(int v)
writeChar(char c)
writeChars(String s)
writeDouble(double d)
writeFloat(float v)
writeInt(int i)
writeLong(long l)
writeShort(int i)
writeUTF(String s)
5、管道流
PipedInputStream类
PipedInputStream() 创建一个管道输入流
PipedInputStream(PipedOutputStream a) 连接到输出流a的输入流
read() 从输入流中读取一个字节
read(byte b[],int off,int len) off是在b中的开始位置,len是字节长度
PipedOutputStream类
PipedOutputStream() 创建一个输出流
PipedOutputStream(PipedInputStream a) 连接到输入流a的输出流
write(int b)
write(byte b[],int off,int len)
counnect() 连接输入输出流
close() 关闭流
在使用的时候要捕获IOException异常。
6、数据流
DataInputStream类(数据输入流)
DataInputStream(InputStream in) 将数据输入流指向一个由in指定的输入流
DataOutputStream类(数据输出流)
DataOutputStream(OutputStream out) 将数据输出流指向一个由out指定的输出流
主要方法:
close()
read() 读取一个字节数据
readBoolean() 读取一个布尔值
readByte() 读取一个字节
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 读取一个UTF字符串
skipByte(int n) 在文件中跳过给定数量的字节
write(byte b[]) 写b.length个字节到文件
writeBoolean(bolean b)
writeByte(int v)
writeChar(char c)
writeChars(String s)
writeDouble(double d)
writeFloat(float v)
writeInt(int i)
writeLong(long l)
writeShort(int i)
writeUTF(String s)
7、对象流
ObjectInputStream类和ObjectOutputStream类分别是DataInputStream类和DataOutputStream类的子类
8、回压输入流
PushbackInputStream类
PushbackInputStream(InputStream in)
PushbackReader类
PushbackReader(Reader in)
unread(char c) 回压一个字符
unread(char c[]) 回压数组c中全部字符
unread(char c[],offset,int n) 回压c中从offset开始的n个字符
相关文章推荐
- 【Java】利用文件输入输出流完成把一个文件夹内的所有文件拷贝的另一的文件夹的操作
- Java第九次作业--输入输出流和文件操作
- java的输入输出流与文件操作(1.基本定义)
- Java输入输出流和文件操作
- java的输入输出流与文件操作(2.读入写出及关流)
- java输入输出流及文件操作
- java输入输出流及文件操作
- java 输入输出流(文件操作)及序列化
- java的输入输出流与文件操作(3 .文件拷贝)
- java 对象输入输出流读写文件的操作实例
- java输入输出流及文件操作
- 输入/输出流---打开,读写文件操作
- java 输入输出流将jsp文件生成html,解决乱码问题
- 08_I/O输入输出流、文件重定向、格式控制、标准输入输出、 串流类、 文件操作、多线程
- JavaIO 总结笔记<三> 基本字节字符输入输出流和文件复制
- 学习Android的文件基本I/0输入输出流的操作
- c++学习笔记01-输入输出流&&文件输入输出操作
- 使用java输入输出流实现文件的复制:
- java ---输入输出流操作(代码整理)
- java输入输出流——文件拷贝