java的基本IO操作与文件数据IO操作

  1. ,流根据方向不同分为输入流和输出流,参照点为当前程序。输入流用来读取数据,输出流用来写出数据。java.io.InputStream 抽象类,定义了输入流的读取字节方法,所有的字节输入流都继承自它,java.io.OutputStream则是所有字节输出流的父类。
    流分为节点流与处理流:节点流,也叫低级流,是真实负责读写数据的流,读写操作中必须有低级流,数据源明确;处理流,也叫高级流。读写可以没有高级流,高级流也不能独立存在,必须用于处理其他流,处理其他流的目的是简化读写数据中的操作。java.io.FileOutputStream文件输出流,是一个低级流,作用是向文件中写出字节
public class FOSDemo {
	public static void main(String[] args) throws IOException {
		/*
		 * 默认创建的FOS是覆盖写操作
		 * FOS会先将文件数据(若有数据)全部删除,然后在开始写。
		 */
		FileOutputStream fos=new FileOutputStream("fos.txt");
		String str="我爱北京天安门";
		/*
		 * String->byte[]
		 * 
		 * byte getBytes()
		 * 将当前字符串按照系统默认字符集转换为一组字节
		 *
		 * byte getBytes(String csn)
		 * 按照给定的字符集将当前字符串转换为一组字节
		 */
		byte[] data=str.getBytes("UTF-8");
		fos.write(data);
		
		System.out.println("写出完毕");
		fos.close();

	}

}

  1. 文件输出流 追加写操作
public class FOSDemo2 {

	public static void main(String[] args) throws IOException {
		/*
		 * 在创建FOS时,若指定第二个参数,并且该值为true时,则是追加写操作,那么本次通过FOS
		 * 写出的内容会被追加到该文件末尾
		 */
		FileOutputStream fos=new FileOutputStream("fos.txt",true);
		fos.write("天安门上太阳升".getBytes("UTF-8"));
		System.out.println("写出完毕");
		fos.close();

	}

}

  1. java.io.FileInputStream 文件输入流,是一个低级流,用于从文件中读取字节
public class FISDemo {

	public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("fos.txt");
		byte[] data=new byte[100];
		int len=fis.read(data);//读
		String str=new String(data,0,len,"UTF-8");//将读到的字节全部转为字符串
		System.out.println(str);
		fis.close();

	}

}

  1. 使用文件流复制文件
public static void main(String[] args) throws IOException {
		/*
		 * 使用文件输入流读取原文件,再使用文件输出流向目标文件中写。
		 * 顺序从原文件中读取每个字节并写入到目标文件即可完成复制
		 */
		FileInputStream src=new FileInputStream("aaa.txt");//读
		FileOutputStream desc=new FileOutputStream("aaa_cp1.txt");//写
		
		byte[] buf=new byte[1024*24];//快速复制的办法 ,先创建一个byte数组
		int len=-1;
		while((len=src.read(buf))!=-1){
			desc.write(buf, 0, len);
		}
		System.out.println("复制完毕");
		src.close();
		desc.close();
	}
  1. 缓冲流 java.io.BufferedInputStream,java.io.BufferedOutputStream
    缓冲字节输入输出流是一对高级流,使用他们可以加快读写效率.
    高级流可以处理其他流,但是无论添加了多少高级流,最底下都要有低级流,因为低级流是真实读写数据的流,高级流都是处理数据的. 高级流处理其他流就形成了流的链接。并且有效的组合不同的高级流可以得到叠加的效果
public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("bbb.docx");
		BufferedInputStream bis=new BufferedInputStream(fis);//提高读的效率,相当于给读的操作安一个净水器,装上fis
		
		FileOutputStream fos=new FileOutputStream("bbb_cp2.docx");
		BufferedOutputStream bos=new BufferedOutputStream(fos);//提高写的效率
		
		int d=-1;
		/*
		 * 缓冲流内部有一个缓冲区,当bis.read方法读取第一个字节时,实际上BIS会一次性读取一组
		 * 字节并存入内部的字节数组中,然后将第一个字节返回,当再次调用read方法时,BIS直接从字
		 * 节数组中将第二个字节返回,直到字节数组中所有字节全部返回后,才会再次读取一组字节。
		 * 
		 * 所以缓冲流也是依靠提高一次读写的数据量减少读写次数来达到提高读写效率的
		 * 
		 */
		while((d=bis.read())!=-1){
			bos.write(d);
		}
		System.out.println("复制完毕");
		
		//关流的时候关最外层的高级流就行了
		bis.close();
		bos.close();
		
		
		
		
		
		/*一个字节一个字节的读,复制的效率很慢
		FileInputStream fis=new FileInputStream("bbb.docx");
		FileOutputStream fos=new FileOutputStream("bbb_cp1.docx");
		int d=-1;
		while((d=fis.read())!=-1){
			fos.write(d);
		}
		System.out.println("复制完毕");
		fis.close();
		fos.close();*/
	}

  1. 缓冲输出流写出数据的缓冲区问题
public static void main(String[] args) throws IOException {
		FileOutputStream fos=new FileOutputStream("bos.txt");
		BufferedOutputStream bos=new BufferedOutputStream(fos);
		
		bos.write("呵呵".getBytes("UTF-8"));
		//强制将缓冲区中的字节一次性写出
		bos.flush();
		System.out.println("写出完毕");
		bos.close();
	}

  1. 对象流对象流是一对高级流,作用是方便读写Java中的对象。java.io.ObjectOutputStream 对象输出流,可以将给定的对象转换为一组字节后写出。
/**
 * 该类用于测试作为对象流读写对象使用
 * 
 * 当一个类需要被对象流读写,那么该类必须实现java.io.Serializable接口
 * @author xiaoxiannv
 *
 */
public class Person implements Serializable{
	
	
	/**
	 * 当一个类实现了Serializable接口后,应当添加一个常量serialVersionUID
	 * 该常量为当前类的序列化版本号,若不定义系统会根据当前类的结构生成,但是只要类的结构发生变化,
	 * 版本号也会相应发生改变
	 * 
	 * 版本号影响着反序列化的结果,即:当OIS对一个对象进行反序列化时,会检查该对象与类的版本号是否一致,
	 * 若一致:反序列化成功,但是若该对象与类的结构不一致时,采用兼容模式,能还原的属性都还原。
	 * 若不一致:反序列化直接抛出版本不一致异常
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private String gender;
	/*
	 * transient关键字用来修饰属性
	 * 当被修饰后,该类实例在使用OOS进行对象序列化时,该属性值被忽略,从而达到对象“瘦身”的目的
	 */
	private transient List<String> otherInfo;
	
	public Person(){
		
	}

	public Person(String name, int age, String gender, List<String> otherInfo) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
		this.otherInfo = otherInfo;
	}

	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;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public List<String> getOtherInfo() {
		return otherInfo;
	}

	public void setOtherInfo(List<String> otherInfo) {
		this.otherInfo = otherInfo;
	}
	
	public String toString(){
		return name+","+age+","+gender+","+otherInfo;
	}
}

public class OOSDemo {
	public static void main(String[] args) throws IOException {
		Person p=new Person();
		p.setName("赵老师");
		p.setAge(20);
		p.setGender("女");
		List<String> otherInfo=new ArrayList<String>();
		otherInfo.add("是一个老师");
		otherInfo.add("爱好是唱歌");
		otherInfo.add("喜欢爬山");
		p.setOtherInfo(otherInfo);
		
		FileOutputStream fos=new FileOutputStream("person.obj");
		ObjectOutputStream oos=new ObjectOutputStream(fos);
		
		/*
		 * ObjectOutputStream的writeObject方法可以将给定对象转换为一组字节后写出。
		 * 这些字节比该对象实际内容要大,因为除了数据外还有结构等描述信息。
		 * 
		 * 下面的代码实际经历了两个操作:
		 * 1.oos将Person对象转换为一组字节。
		 * 	将一个对象转换为一组字节的过程称为:对象序列化
		 * 	
		 * 2.再通过fos将这组字节写入到硬盘。
		 * 	将该对象转换的字节写入到硬盘做长久保存的过程称为:对象持久化
		 */
		oos.writeObject(p);
		System.out.println("写出对象完毕");
		oos.close();
	}

}

  1. java.io.ObjectInputStream 对象输入流,作用是可以进行对象反序列化,读取一组字节并还原为对象。OIS读取的字节必须是由OOS将对象序列化得到的字节,否则会抛出异常。
public class OISDemo {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		FileInputStream fis=new FileInputStream("person.obj");
		ObjectInputStream ois=new ObjectInputStream(fis);
		//对象反序列化
		Person p=(Person)ois.readObject();
		System.out.println(p);
		ois.close();
	}

}

  1. 缓冲字符流BufferedWriter,BufferedReader特点是可以按行读写字符串。java.io.PrintWriter 具有自动行刷新的缓冲字符输出流。创建PW时,它一定会在内部创建BufferedWriter作为缓冲功能的叠加。
public static void main(String[] args) throws IOException {
		/*
		 * 提供了多种构造方法
		 * 其中有两个可以直接对文件进行写出操作的构造方法
		 * PrintWriter(File file)
		 * PrintWriter(String path)
		 * 
		 */
		PrintWriter pw=new PrintWriter("pw.txt","UTF-8");
		pw.println("锄禾日当午");
		pw.println("汗滴禾下土");
		System.out.println("写出完毕");
		pw.close();

	}

  1. PrintWriter也提供了可以处理其他流的构造方法。提供的方法可以传入字节流,也可以处理字符流。并且,当使用这类构造方法时,可以再传入第二个参数,该参数为boolean值,该值为true时,则具有了自动行刷新功能
public static void main(String[] args) throws IOException {
		FileOutputStream fos=new FileOutputStream("pw1.txt");
		OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
		PrintWriter pw=new PrintWriter(osw);
		pw.println("呵呵");
		pw.println("嘿嘿");
		System.out.println("写出完毕");
		pw.close();

	}
  1. java.io.BufferedReader缓冲字符输入流,特点:按行读取字符串
public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("src/day08/BRDemo.java");
		InputStreamReader isr=new InputStreamReader(fis);//转变为字符流
		BufferedReader br=new BufferedReader(isr);
		/*
		 * BufferedReader提供了按行读取方法
		 * String readLine()
		 * 连续读取若干字符,直到读取到换行符为止,并将换行符之间读取的字符以一个字符串返回。若返回NULL,
		 * 则表示读取到末尾
		 * 注意:该字符串不包含最后的换行符
		 */
		String line=null;
		while((line=br.readLine())!=null){
			System.out.println(line);
		}
		br.close();
	}

  1. 字符流,字符流的读写单位为字符。字符流都是高级流,虽然以字符为单位读写数据 ,但是实际底层还是读写字节,只是从字节与字符的转换工作交给了字符流来完成。java.io.Reader:字符输入流的顶级父类,java.io.Writer:字符输出流的顶级父类
    转换流,java.io.OutputStreamWriter,特点是可以按照指定的字符集写出字符。之所以称OutputStreamWriter与InputStreamReader为转换流,是因为大多数的字符流都只处理其它字符流,而低级流又是字节流,这就导致字符流不能处理字节流的问题,转换流相当于是一个转换器的作用。他们可以将字节流先转变为字符流,这样其它的字符流就可以处理了。
public static void main(String[] args) throws IOException {
		FileOutputStream fos=new FileOutputStream("osw.txt");
		OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");
		osw.write("我爱北京天安门");
		osw.write("天安门上太阳升");
		System.out.println("写出完毕");
		osw.close();

	}

  1. java.io.InputStreamReader字符输入流,可以按照给定的字符集读取字符。

	public static void main(String[] args) throws IOException {
		FileInputStream fis=new FileInputStream("osw.txt");
		InputStreamReader isr=new InputStreamReader(fis,"UTF-8");
		int d=-1;//用-1表示文件末尾
		while((d=isr.read())!=-1){
			System.out.print((char)d);
		}
		
		isr.close();
	}
  1. 完成记事本功能要求:
    程序启动后,要求用户输入一个文件名,然后创建该文件,之后提示用户开始输入内容 ,并将用户输入的每一行内容都按行写入到该文件。当用户输入“exit”时,退出程序
public class Note {

	public static void main(String[] args) throws IOException {
		Scanner scanner=new Scanner(System.in);
		System.out.println("请输入文件名:");
		String fileName=scanner.nextLine();
		FileOutputStream fos=new FileOutputStream(fileName);
		OutputStreamWriter osw=new OutputStreamWriter(fos);
		//自动行刷新
		PrintWriter pw=new PrintWriter(osw);
		System.out.println("请您开始输入内容");
		String line=null;
		while(true){
			line=scanner.nextLine();
			if("exit".equals(line)){
				System.out.println("再见");
				break;
			}
			/*
			 * 若PrintWriter具有自动行刷新功能, 那么每当调用println方法后就会自动flush
			 */
			pw.println(line);
			pw.flush();
		}
		
		pw.close();
	}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值