Java--IO

java.io.File类用于表示文件(目录)
File类只用于表示文件(目录)的信息(名称、大小等),不能用于文件内容的访问


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


(1)java文件模型
  在硬盘上的文件是byte byte byte存储的,是数据的集合
(2)打开文件
  有两种模式"rw"(读写)  "r"(只读)
  RandomAccessFile raf = new RandomeAccessFile(file,"rw")
  文件指针,打开文件时指针在开头 pointer = 0;
(3) 写方法
    raf.write(int)--->只写一个字节(后8位),同时指针指向下一个位置,准备再次写入
(4)读方法
   int b = raf.read()--->读一个字节
(5)文件读写完成以后一定要关闭(Oracle官方说明)




序列化与基本类型序列化
1)将类型int 转换成4byte或将其他数据类型转换成byte的过程叫序列化
     数据---->n byte
2)反序列化
    将n个byte 转换成一个数据的过程
    nbyte ---> 数据
3)RandomAccessFile提供基本类型的读写方法,可以将基本类型数据
   序列化到文件或者将文件内容反序列化为数据
 IO流(输入流、输出流)
 字节流、字符流
 1.字节流
 1)InputStream、OutputStream
    InputStream抽象了应用程序读取数据的方式
    OutputStream抽象了应用程序写出数据的方式 
 2)EOF = End   读到-1就读到结尾
 3)输入流基本方法
   int  b = in.read();读取一个字节无符号填充到int低八位.-1是 EOF
   in.read(byte[] buf) 
   in.read(byte[] buf,int start,int size)
4)输出流基本方法
  out.write(int b)  写出一个byte到流,b的低8位
  out.write(byte[] buf)将buf字节数组都写入到流
  out.write(byte[] buf,int start,int size)
  
 5)FileInputStream--->具体实现了在文件上读取数据
 6)FileOutputStream 实现了向文件中写出byte数据的方法
 7)DataOutputStream/DataInputStream
    对"流"功能的扩展,可以更加方面的读取int,long,字符等类型数据
   DataOutputStream
        writeInt()/writeDouble()/writeUTF()


 8)BufferedInputStream&BufferedOutputStream
 这两个流类位IO提供了带缓冲区的操作,一般打开文件进行写入
 或读取操作时,都会加上缓冲,这种流模式提高了IO的性能
 从应用程序中把输入放入文件,相当于将一缸水倒入到另一个缸中:
 FileOutputStream--->write()方法相当于一滴一滴地把水“转移”过去
 DataOutputStream-->writeXxx()方法会方便一些,相当于一瓢一瓢把水“转移”过去
 BufferedOutputStream--->write方法更方便,相当于一飘一瓢先放入桶中,再从桶中倒入到另一个缸中,性能提高了
   
   
 2.字符流
 1) 编码问题
 2)认识文本和文本文件
 java的文本(char)是16位无符号整数,是字符的unicode编码(双字节编码)
 文件是byte byte byte ...的数据序列
文本文件是文本(char)序列按照某种编码方案(utf-8,utf-16be,gbk)序列化为byte的存储结果
3)字符流(Reader Writer)---->操作的是文本文本文件
字符的处理,一次处理一个字符
字符的底层任然是基本的字节序列
字符流的基本实现
   InputStreamReader   完成byte流解析为char流,按照编码解析
   OutputStreamWriter  提供char流到byte流,按照编码处理  
   
   FileReader/FileWriter
 字符流的过滤器
   BufferedReader   ---->readLine 一次读一行

   BufferedWriter/PrintWriter   ---->写一行    

实例:写了一个文件复制的客户端程序用来对比各个流读写同一文件的效率

//文件读写工具类,实现了各个流的各种读写操作
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.swing.JOptionPane;

public class FileUtils {
    public FileUtils() {
		// TODO Auto-generated constructor stub
	}
    //字节流批量复制
	public static boolean CopyFileByByteArray(File srcFile, File destFile) {
		if (!srcFile.exists())
			System.out.println("文件" + srcFile + "不存在!");
		if (!srcFile.isFile())
			System.out.println(srcFile + "不是文件!");
		FileOutputStream out = null;
		FileInputStream in = null;
		try {
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(destFile);
			int c;
			byte bytes[] = new byte[20 * 1024];
			while ((c = in.read(bytes, 0, bytes.length)) != -1) {
				out.write(bytes, 0, c);
				out.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			// 关闭流
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

//		JOptionPane.showMessageDialog(null, "字节流批量读取!");
		return true;
	}
	//单字节复制
	public static boolean CopyFileByByte(File srcFile,File destFile) {
		FileOutputStream out = null;
		FileInputStream in = null;
		try {
			in = new FileInputStream(srcFile);
			out = new FileOutputStream(destFile);
			int c;
			while ((c = in.read()) != -1) {
				out.write(c);
				out.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			// 关闭流
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
//		JOptionPane.showMessageDialog(null, "字节流!");
		return true;	
	}
	//字节缓冲流复制
	public static boolean CopyFileByByteBuffer(File srcFile,File destFile) {
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try {
			in = new BufferedInputStream(
					 new FileInputStream(srcFile));
			out = new BufferedOutputStream(
					new FileOutputStream(destFile));
			int c;
			while ((c = in.read()) != -1) {
				out.write(c);
				out.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} finally {
			// 关闭流
			try {
				in.close();
				out.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

//		JOptionPane.showMessageDialog(null, "字节缓冲流");
		return true;
	}
	//文本文件用字符流批量复制
	public static boolean CopyFileByCharArray(File srcFile,File destFile){
		InputStreamReader isr = null;
		OutputStreamWriter osw = null;
		try {
			isr = new InputStreamReader(new FileInputStream(srcFile));
			osw =  new OutputStreamWriter(new FileOutputStream(destFile));
			char buffer [] = new char[20*1024];
			int c;
			while((c=isr.read(buffer, 0, buffer.length))!=-1) {
				osw.write(buffer, 0, c);
				osw.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally {
			try {
				osw.close();
				isr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		JOptionPane.showMessageDialog(null, "字符流批量读取");
		return true;
	}
	//字符流单字节读取
	public static boolean CopyFileByChar(File srcFile,File destFile) {
		InputStreamReader isr = null;
		OutputStreamWriter osw = null;
		try {
			isr = new InputStreamReader(new FileInputStream(srcFile));
			osw =  new OutputStreamWriter(new FileOutputStream(destFile));
			int c;
			while((c=isr.read())!=-1) {
				osw.write(c);
				osw.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally {
			try {
				osw.close();
				isr.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		JOptionPane.showMessageDialog(null, "字符流!");
		return true;
	}
	//FileWriter/FileReader
	public static boolean CopyByFrAndFw(File srcFile,File destFile){
		FileReader fr = null;
		FileWriter fw = null;
		try {
			fw = new FileWriter(destFile);
			fr = new FileReader(srcFile);
			char buffer[] = new char[20*1024];
			int c;
			while((c=fr.read(buffer, 0,buffer.length))!=-1) {
				fw.write(buffer, 0, c);
				fw.flush();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		finally {
			try {
				fr.close();
				fw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		JOptionPane.showMessageDialog(null, "FileReader/FileWriter");
		return true;
	}
	//BufferedReader/BufferedWriter
	public  static boolean CopyFileByBrAndBw(File srcFile,File destFile) {
		   BufferedReader br = null;
		   BufferedWriter bw = null;
		   try {
			   br =  new BufferedReader((InputStreamReader)new FileReader(srcFile));
			 //  br =  new BufferedReader(new InputStreamReader(new FileInputStream(srcFile)));
			  bw =  new BufferedWriter((OutputStreamWriter)new FileWriter(destFile));
			 //  bw =  new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destFile)));
			   String line = null;
			   while((line = br.readLine())!=null) {
			//	   System.out.println(line);
				   bw.write(line);
				   bw.newLine();
				   bw.flush();
			   }
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		 finally {
			   try {
				br.close();
				   bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
//		JOptionPane.showMessageDialog(null, "字符缓冲流");		   
		return true;
	}
}
//客户端程序,调用各种文件读写方法
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class FileCopyClient extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel pan ;
	JButton button;
	JComboBox<String> com;
	JComboBox<String> subCom;
	JFileChooser jfc;
	JTextArea area;
	//JScrollPane jsp ;
	public FileCopyClient() {
		// TODO Auto-generated constructor stub
		//添加组件
		init();
		
		this.setTitle("IOTestClient");
		this.setSize(600, 400);
		this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	//添加组件
	void init() {
		//实例化组件
		jfc =new JFileChooser();
		area = new JTextArea();
	//	area.setFont(new Font("楷体", Font.BOLD, 15));//若写在jsp定义之后会发生空指针异常,待解决
		pan = new JPanel(null);
		com = new JComboBox<String>();
		subCom = new JComboBox<String>();
		com.addItem("文件");
		com.addItem("文本");
		JLabel l1 = new JLabel("文件地址:");
		JLabel l2 = new JLabel("目的地址:");
		JTextField text1 = new JTextField(50);
		JTextField text2 = new JTextField(50);
		JButton button = new JButton("确定");
		JButton btnFile = new JButton("选择文件");
		JButton btnDir = new JButton("选择目录");
		JScrollPane jsp = new JScrollPane(area);
		
		//设置字体
		l1.setFont(new Font("楷体",Font.BOLD,20));
		l2.setFont(new Font("楷体",Font.BOLD,20));
		text1.setFont(new Font("楷体",Font.BOLD,15));
		text2.setFont(new Font("楷体",Font.BOLD,15));
		button.setFont(new Font("楷体",Font.BOLD,20));
		btnFile.setFont(new Font("楷体",Font.BOLD,13));
		btnDir.setFont(new Font("楷体",Font.BOLD,13));
		com.setFont(new Font("楷体",Font.BOLD,15));
		subCom.setFont(new Font("楷体",Font.ITALIC,15));
	//	area.setFont(new Font("楷体",Font.BOLD,15));
		//布局
		jsp.setBounds(100, 230, 400, 130);
		area.setBounds(100, 230, 400, 130);
		com.setBounds(10,10,100,20);
		subCom.setBounds(com.getX()+com.getWidth()+10, com.getY(), com.getWidth()+100, com.getHeight());
		l1.setBounds(80, 70, 100,40);
		l2.setBounds(l1.getX(), l1.getY()+l1.getHeight()+5, l1.getWidth(),l1.getHeight());
		text1.setBounds(l1.getX()+l1.getWidth(),l1.getY(), 250, 40);
		text2.setBounds(text1.getX(), l2.getY(), text1.getWidth(), text1.getHeight());
		button.setBounds(text1.getX()+text1.getWidth()/3, text2.getY()+text2.getHeight()+20, 80, 40);
		btnFile.setBounds(text1.getX()+text1.getWidth()+10, text1.getY(), 90, 40);
		btnDir.setBounds(text2.getX()+text2.getWidth()+10, text2.getY(), btnFile.getWidth(),btnFile.getHeight());
		
		//添加事件监听
		com.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				if(com.getSelectedIndex()==0) {
					subCom.removeAllItems();
					subCom.addItem("字节流");
					subCom.addItem("字节流批量读取");
					subCom.addItem("字节缓冲流");
				}else {
					subCom.removeAllItems();
					subCom.addItem("FileReader/FileWriter");
					subCom.addItem("字符缓冲流");
					subCom.addItem("字符流");
					subCom.addItem("字符流批量读取");
				}
			}
		});
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String srcText = text1.getText().trim();
				String destText = text2.getText().trim();
				File srcFile = new File(srcText);
				File destFile = new File(destText);
				area.setFont(new Font("楷体",Font.ITALIC,15));
				if(!srcFile.exists()) {
					if(area==null) System.out.println("error");
					area.append("源文件不存在!\n");
					System.out.println("源文件不存在!");
				     return ;
				}
				if(!srcFile.isFile()) {
					area.append("目标不是文件!\n");
					System.out.println("目标不是文件!");
				     return ;
				}
				if(!destFile.isDirectory()) {
					area.append("目标地址不是目录!\n");
					System.out.println("目标地址不是目录!");
					return ;
				}
				destFile = new File(destText+"/"+srcFile.getName());
			//	System.out.println(srcFile.getAbsolutePath() +"  "+srcFile.getName());
			//	System.out.println(destFile.getAbsolutePath()+"  "+destFile.getAbsolutePath());
				boolean success = false;
				if(subCom.getSelectedItem()==null) {
					JOptionPane.showMessageDialog(pan, "请选择要使用的IO流!");
					return ;
				}
				//获取所选中io流的index
				int index = subCom.getSelectedIndex();
				//开始计时
				long start = System.currentTimeMillis();
				if (com.getSelectedItem().toString().equals("文件")) {
					if (index == 0)
						success = FileUtils.CopyFileByByte(srcFile, destFile);
					else if (index == 1)
						success = FileUtils.CopyFileByByteArray(srcFile, destFile);
					else
						success = FileUtils.CopyFileByByteBuffer(srcFile, destFile);
				} else {
					if (index == 0)
						success = FileUtils.CopyByFrAndFw(srcFile, destFile);
					else if (index == 1)
						success = FileUtils.CopyFileByBrAndBw(srcFile, destFile);
					else if (index == 2)
						success = FileUtils.CopyFileByChar(srcFile, destFile);
					else
						success = FileUtils.CopyFileByCharArray(srcFile, destFile);

				}
                //计时结束
			    long end = System.currentTimeMillis();
			    if(success) {
			    	area.append("复制成功!使用 "+subCom.getSelectedItem().toString()+" 用时:"+(end-start)+"\n");
			   // 	System.out.println("成功!");
			    }
			    else {
			    	area.append("失败!\n");
			   // 	System.out.println("失败!");
			    }
			    	
			}
		});
		btnFile.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
				// TODO Auto-generated method stub
				jfc.showOpenDialog(pan);
				if(jfc.getSelectedFile()!=null)
					text1.setText(jfc.getSelectedFile().getAbsolutePath());
			}
		});
		btnDir.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				// TODO Auto-generated method stub
				jfc.showOpenDialog(pan);
				if(jfc.getSelectedFile()!=null)
					text2.setText(jfc.getSelectedFile().getAbsolutePath());
			}
		});
	
		//添加组件
		pan.add(l1);
		pan.add(l2);
		pan.add(text1);
		pan.add(text2);
		pan.add(button);
		pan.add(btnFile);
		pan.add(btnDir);
		pan.add(com);
		pan.add(subCom); 	
		pan.add(jsp);

		this.add(pan);
	}
	
	public static void main(String[] args) {
		new FileCopyClient();
	}

}

以一个56KB的图片文件为例,运行结果如下:


同样,使用字符流运行结果如下,复制后的文件不能正常打开:


由以上运行结果对比可得,在文件读写过程中使用字节流的read(byte [])方法或字符流的read(char [])方法效率会比较高。


   
   
3.对象的序列化,反序列化
1)对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化 
2)序列化流(ObjectOutputStream),是过滤流----writeObject
   反序列化流(ObjectInputStream)---readObject


3)序列化接口(Serializable)
   对象必须实现序列化接口 ,才能进行序列化,否则将出现异常

   这个接口,没有任何方法,只是一个标准

样例:

//Student类实现可序列化接口
import java.io.Serializable;

public class Student implements Serializable {
    int id;
    String name;
    int age;
    public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(int id, String name, int age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
	
    
    
	
}
//文件的序列化,与反序列化
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectSeriaDemo {
      public static void main(String[] args) throws IOException{
    	  File file = new File("demo/obj.dat");
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));
		//序列化
		Student stu = new Student(1001,"张三",20);
		oos.writeObject(stu);
		//反序列化
		Student stu1 = null ;
		try {
			stu1 = (Student)ois.readObject();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(stu1.toString());
		
	}
}
运行结果:Student [id=1001, name=张三, age=20]


 
4) transient关键字
    private void writeObject(java.io.ObjectOutputStream s)
       throws java.io.IOException
private void readObject(java.io.ObjectInputStream s)
       throws java.io.IOException, ClassNotFoundException
       
   分析ArrayList源码中序列化和反序列化的问题
 
5)序列化中 子类和父类构造函数的调用问题
 一个类要序列化,就要实现Serializable接口,反过来说,不实现Serializable就不能序列化。当父类没实现Serializable接口,子类继承父类并实现Serializable接口,子类序列化时,父类的部分是没有被序列化的,那么反序列化的时候就没办法找到父类的部分,这时候怎么办呢,就调用父类构造器初始化父类部分。
 
 样例:
 
package com.objectseria;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ObjectSeriaDemo2 {
     public static void main(String[] args) throws IOException, ClassNotFoundException {
//		ObjectOutputStream oos = new ObjectOutputStream(
//				new FileOutputStream("demo/obj2.dat"));
//		Foo2 foo2 = new Foo2();
//		oos.writeObject(foo2);
//		oos.close();
		
		ObjectInputStream ois = new ObjectInputStream(
				new FileInputStream("demo/obj2.dat"));
		Foo2 foo2 = (Foo2)ois.readObject();
		ois.close();
	}
}
class Foo implements Serializable{
	 public Foo() {
		// TODO Auto-generated constructor stub
	    System.out.println("Foo...");
	 }
}
class Foo1 extends Foo implements Serializable{
	 public Foo1() {
		// TODO Auto-generated constructor stub
	    System.out.println("Foo1...");
	 }
}
class Foo2 extends Foo1 implements Serializable{
	 public Foo2() {
		// TODO Auto-generated constructor stub
	    System.out.println("Foo2...");
	 }
}


 
 
 
   
   
   
   
   
   
   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值