JAVA IO流

IO流简单概括

IO流包括输入(input)与输出(output),它是完全面向对象的,主要采用了装饰器模式避免大量的类,包括了最大的可能性,提供了较好的扩展机制。

Java IO流可以概括为:两个对应、一个桥梁。两个对应指字节流和字符流的对应,输入流和输出流的对应。一个桥梁指从字节流到字符流的桥梁。

流的操作有两种:字节流(InputStream读取,OutputStream写入)和字符流(Reader读取,Writer写入)

File类

File是对文件和目录的操作

例:在这里插入图片描述

字节流操作二进制文件

音频,视频等都是二进制文件。

FileInputStream:字节流读取数据
FileOutputStream:字节流写入数据

package com.lc.hp;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

//字节流操作

public class ZjDemo {

	public static void main(String[] args) {
		
		FileInputStream fis = null;
		FileOutputStream fos = null;
		try {
			fis = new FileInputStream("d:\\lc.png");
			fos = new FileOutputStream("d:\\cl.png");
			byte [] b = new byte[1024];
			int result = 0;
			try {
				while((result = fis.read(b))!=-1){
					
					fos.write(b,0,result);
				}
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
}

字节流缓冲区操作

package com.lc.hp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//字节流缓冲区

public class HcDemo {

	public static void main(String[] args) {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			fis = new FileInputStream("d:\\lc.png");
			fos = new FileOutputStream("d:\\cl.png");
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(bos);
			byte [] b = new byte[1024];
			int result = 0;
			try {
				while((result = bis.read(b))!=1){
					bos.write(b,0,result);
				}
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			try {
				fis.close();
				fos.close();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
}

字符流操作

字符流有BufferedReader和BufferedWriter
它两个实现了自带缓冲区的字符流高效读写

package com.lc.hp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

//字符流

public class ZfDemo {

	public static void main(String[] args) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			br = new BufferedReader(new FileReader(new File("d:\\lc.txt")));
			try {
				bw = new BufferedWriter(new FileWriter(new File("d:\\cd.txt")));
				String temp = null;
				while((temp = br.readLine())!=null){
					bw.write(temp);
					bw.newLine();
				}
				bw.flush();//刷新缓冲区
				
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
}

字符流缓冲区

package com.lc.hp;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

//字符流缓冲区

public class ZfHcDemo {

	public static void main(String[] args) {
		BufferedReader br = null;
		BufferedWriter bw = null;
		br = new BufferedReader(new InputStreamReader(System.in));
		try {
			bw = new BufferedWriter(new FileWriter(new File("d:\\lc.txt")));
			String temp = "";
			while((temp = br.readLine())!=null){
				if(temp.equals("yue")){
					break;
				}
				bw.write(temp);
				bw.newLine();
			}
			bw.flush();
			
		} catch (IOException e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			try {
				br.close();
				bw.close();
			} catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}
}

查看电脑操作系统的编码格式

idea和eclipse是不一样的

eclipse:
在这里插入图片描述
IDEA:
在这里插入图片描述
飞鸟与鱼不同路,从此山水不相逢。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值