IO流

来源:互联网 发布:node如何指定版本安装 编辑:程序博客网 时间:2024/06/05 18:36
java.io.File类用于表示文件(目录)
file类只用于表示文件(目录)的信息(名称、大小等),不能用于文件的访问。


RandomAccessFile     提供文件内容的访问,既可以读文件,也可以写文件
RandomAccessFile     支持随机访问文件,可以访问文件的任意位置。


1)java文件模型
在硬盘上的文件时byte byte byte存储的,是数据的集合
2)打开方式:(rw:读写;r:只读)
RandomAccessFile raf=new RandomAccessFile(file,"rw");
内部还有一个文件指针,刚打开文件时指针在开头
3)写的方法
raf.write(),只写一个字节,同时指针指向下一个位置,准备再次写入
4)读的方法
raf.read(),读一个字节
5)文件读写完成以后一定要关闭
raf.close();


IO流(输入流、输出流)
1.字节流
1)(InputStream、OutputStream)
InputStream抽象了应用程序读取数据的方式
OutPutStream抽象了应用程序写出数据的方式
2)EOF=End  读到-1就读到结尾 eof(读到文件结尾)
3)输入流基本方法
int b=in.read();读取一个字节无符号填充到int低八位.-1是EOF
in.read(byte[] buf); 读取数据填充到byte数组中
in.read(byte[] buf,int start,int size)start:从哪里开始,size读多少
4)输出流基本方法
out.write(int b) 写出一个byte到流,b的低8位
out.write(byte[] buf);
out.write(byte[] buf,int start ,int size)start:从哪里开始,size写多少
5)FileInputStream  具体实现了在文件上读取数据
单字节读取:不适合读取大文件
/**
* 单字节读取文件内容
* @param file
* @throws IOException
*/
public static void readFile(File file) throws IOException {
FileInputStream fis=new FileInputStream(file);
int i=1;
while(fis.read()!=-1){
if(fis.read()<=0xf){
System.out.print("0");
}
System.out.print(Integer.toHexString(fis.read() &0xff)+"  ");
if(i++%10==0){
System.out.println();
}
}
fis.close();
}
批量读取:
public static void readArrayFile(File file) throws IOException {
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[10 * 1024];
/*
* 从fis中批量读取字节, 放入到buf这个数组中, 从0位置开始放,最多放                 *buf.length个, 返回的是读到的自己个个数
*/
int bytes = 0;
int j = 0;
while ((bytes = fis.read(buf, 0, buf.length)) != -1) {
for (int i = 0; i < bytes; i++) {
if (bytes <= 0xf) {
System.out.print("0");
}
System.out.print(Integer.toHexString(buf[i] & 0xff) + "  ");
if (j++ % 10 == 0) {
System.out.println();
}
}
}
}
文件的拷贝:
public static void copyFile(File file, File newFile) throws IOException {
if (!file.exists()) {
throw new IllegalArgumentException("文件:" + file + "不存在");
}
if (!file.isFile()) {
throw new IllegalArgumentException("文件:" + file + "不是文件");
}
FileOutputStream fos = new FileOutputStream(newFile);
FileInputStream fis = new FileInputStream(file);
byte[] buf = new byte[8 * 1024];
int b;
while ((b = fis.read(buf, 0, buf.length)) != -1) {
fos.write(buf, 0, b);
fos.flush();
}
fis.close();
fos.close();
}
6)FileOutputStream:实现了向文件中写出byte数据的方法
FileOutputStream fos=new FileOutputStream(File file)
FileOutputStream fos=new FileOutputStream(File file,boolean append)
7)DataOutputStream/DataInputStream
对"流"功能的扩展,可以更加方便的读取int,long,字符等类型数据
DataOutputStream dos=new DataOutputStream(new FileOutputStream(File file))
DataInputStream dos=new DataInputStream(new FileInputStream(File file))
8)BufferedInputStream/BufferedOutputStream
这两个流类为IO提供了带缓冲区的操作,一般打开文件进行写入或读取操作时,都会加上缓冲,这种模式提高了IO的性能。
2.字符流(操作的是文本文件)
1)编码问题
2)认识文本和文本文件
java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)
文件时byte byte byte...的数据序列
文本文件是文本(char)序列按某种编码方案序列化为byte的存储结构
3)字符流(Reader Writer)
字符的处理,一次处理一个字符
字符的底层任然是基本的字节序列。
    InputStreamReader 完成byte流解析为char流,按照编码解析(默认项目编码)
    OutputStreamWriter 提供char流到byte流,按照编码处理
    InputStreamReader isr=new InputStreamReader(new FileInputStream(File file));
    OutputStreamWriter isw=new OutputStreamWriter(new FileOutputStream(File file));
    FileReader/FileWriter  直接操作文件(不能设定编码)
    FileReader fr=new FileReader(File file);
    FileWriter fw=new FileWriter(File file);
字符流的过滤器
    BufferedReader  --->readLine 一次读一行(不能识别换行)
    BufferedWriter/PrintWriter  -->写一行
BufferedReader br=new BufferedReader(new InputStreamReader(newFileInputStreamReader(File file)))
BufferedWriter bw=new BufferedWriter(new InputStreamWriter(newFileInputStreamWriter(File file)))
PrinterWriter pw=new PrinterWriter(File file);
3.对象的序列化,反序列化
1)对象序列化,就是将Object转换成byte系列,反之叫对象的反序列化。
2)序列化流(ObjectOutputStream)---->writeObject()
  反序列化流(ObjectInputStream)--->readObject()
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(File file));
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(File file));
3)序列化接口(Serializable)
  对象必须实现序列化接口,才能进行序列化,否则会出现异常,这个借口,没有任何方法,只是一个标   准
4) transient 修饰的元素不会进行jvm默认序列化,也可以自己完成这个元素的序列化
可以在类中重写writeObject 和readObject
  private transient int age;//不希望被jvm进行默认序列化的元素。
  private void writeObject(java.io.ObjectOutputStream s)
        throws java.io.IOException{
s.writeDefaultObject();
s.write(age);
}
   private void readObject(java.io.ObjectInputStream s)
        throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject()
this.age=s.readInt()
}
5)序列化  父类如果实现了序列化接口,子类就不用实现序列化接口了
对子类对象进行反序列化操作时,如果其父类没有实现序列化接口,那么其父类的构造函数会被调用
0 0