IO流

java中对文件的操作是以流的方式进行的。流是java内存中的一组有序数据系列。java将数据从源(文件、内存、键盘、网络)读入到内存中,形成了流,然后这些流还可以写到另外的目的地(文件、内存、控制台、网络)。
在我们操作IO流之前要明确的四步:

(1)明确要操作的数据是数据源还是数据目的(也就是要读还是要写)

源:InputStream			Reader
目的地:OutputStream		Writer

(2)明确要操作的设备上的数据是字节还是文本

字节:InputStream		OutputStream
文本:Reader				Writer

(3)明确数据所在的具体设备

源设备:
硬盘:文件 File开头
内存:数组,字符串
键盘:System.in
网络:Socket
对应目的设备:
硬盘:文件 File开头
内存:数组,字符串
屏幕:System.out
网络:Socket

(4)明确是否需要额外功能(比如是否需要转换流、高效流等)

需要转换—— 转换流 InputStreamReader 、OutputStreamWriter
需要高效—— 缓冲流Bufferedxxx
多个源—— 序列流 SequenceInputStream
对象序列化—— ObjectInputStream、ObjectOutputStream
保证数据的输出形式—— 打印流PrintStream 、Printwriter
操作基本数据,保证字节原样性——DataOutputStream、DataInputStream

File类

在开发中读取文件、生成文件、删除文件、修改文件的属性时经常用到该类。现在我们来看下File类的构造器:
在这里插入图片描述

File类构造方法不会给你检验这个文件或文件夹是否真实存在,因此无论该路径下是否存在文件或者目录,都不影响File对象的创建。
// 文件路径名 
String path = "D:\\123.txt";
File file1 = new File(path); 

// 文件路径名
String path2 = "D:\\1\\2.txt";
File file2 = new File(path2);     -------------相当于D:\\1\\2.txt

// 通过父路径和子路径字符串
 String parent = "F:\\aaa";
 String child = "bbb.txt";
 File file3 = new File(parent, child);  --------相当于F:\\aaa\\bbb.txt

// 通过父级File对象和子路径字符串
File parentDir = new File("F:\\aaa");
String child = "bbb.txt";
File file4 = new File(parentDir, child); --------相当于F:\\aaa\\bbb.txt

节点流

字节流:任意类型的数据都能写

字节输入流:InputStream FileInputStream

通过字节输入流读取文件的内容:

public class TestInputStream02 {
	public static void main(String[] args) {
		InputStream is=null;
		try {
			is=new FileInputStream("a.txt");
			byte[] b=new byte[1024];
			int len=0;
			while((len=is.read(b))!=-1){
				String str=new String(b, 0, len);
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(is!=null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

字节输出流:OutputStream FileOutputStream
通过字节输出流可以将数据写出到文件中:

public class TestOutputStream {
	public static void main(String[] args) {
		OutputStream os=null;
		try {
			os=new FileOutputStream("b.txt");
			byte[] b="我是中国人".getBytes();
			os.write(b);
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(os!=null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

通过两者的结合可以实现文件的拷贝:

public class CopyUtils {
	public static void main(String[] args) {
		copyFile("C:/Users/HAN/Pictures/Saved Pictures/内存分析.png","内存分析.png");
	}
	
	public static void copyFile(String src,String dest){
		copyFile(new File(src),new File(dest));
	}
	
	public static void copyFile(File src,File dest){
		InputStream is=null;
		OutputStream os=null;
		
		try {
			is=new FileInputStream(src);
			os=new FileOutputStream(dest);
			byte[] car=new byte[1024];
			int len=-1;
			while((len=is.read(car))!=-1){
				os.write(car, 0, len);
			}
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(os!=null){
					os.close();
				}
				if(is!=null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}
}

字符流:只能操作纯文本

字符输入流:Reader FileReader

字符输出流:Writer FileWriter

public class CharFileCopy {
	public static void main(String[] args) {
		//图片拷贝不成功
		//copyFile("C:/Users/HAN/Pictures/Saved Pictures/内存分析.png","内存分析2.png");
		copyFile("a.txt","c.txt");
	}
	
	public static void copyFile(String src,String dest){
		copyFile(new File(src),new File(dest));
	}
	
	public static void copyFile(File src,File dest){
		Reader reader=null;
		Writer writer=null;
		
		try {
			reader=new FileReader(src);
			writer=new FileWriter(dest);
			char[] car=new char[1];
			int len=-1;
			while((len=reader.read(car))!=-1){
				writer.write(car, 0, len);
				//System.out.println(new String(car,0,len));
			}
			writer.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(writer!=null){
					writer.close();
				}
				if(reader!=null){
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

功能流:(包裹节点流)

缓冲流:增加功能、提高性能、提高读写效率

字节缓冲流:BufferedInputStream BufferedOutputStream

/**
 * 字节缓冲流
 *
 */
public class BufferedByte01 {
	public static void main(String[] args) {
		InputStream is=null;
		OutputStream os=null;
		try {
			is=new BufferedInputStream(new FileInputStream("a.txt"));
			os=new BufferedOutputStream(new FileOutputStream("d.txt"));
			byte[] car=new byte[1024];
			int len=-1;
			while((len=is.read(car))!=-1){
				os.write(car, 0, len);
			}
			os.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(os!=null){
					os.close();
				}
				if(is!=null){
					is.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

字符缓冲流:BufferedReader BufferedWriter

/**
 * 字符缓冲流
 *
 */
public class BufferedChar01 {
	public static void main(String[] args) {
		BufferedReader br=null;
		BufferedWriter bw=null;
		try {
			br=new BufferedReader(new FileReader("a.txt"));
			bw=new BufferedWriter(new FileWriter("b.txt"));
			String str=null;
			while((str=br.readLine())!=null){
				bw.write(str);
				bw.newLine();
			}
			bw.flush();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(bw!=null){
					bw.close();
				}
				if(br!=null){
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

转换流:字节转字符

Data流:操作基本数据类型

/**
 * 数据流
 *
 */
public class DataStream {
	public static void main(String[] args) throws IOException {
		writer("f.txt");
		reader("f.txt");
	}
	
	public static void writer(String dest) throws IOException{
		DataOutputStream os=new DataOutputStream(new FileOutputStream(dest));
		boolean flag=false;
		int i=10;
		double d=2.0;
		os.writeBoolean(flag);
		os.writeInt(i);
		os.writeDouble(d);
		os.flush();
		os.close();
	}
	
	public static void reader(String src) throws IOException{
		DataInputStream is=new DataInputStream(new FileInputStream(src));
		boolean flag=is.readBoolean();
		int i=is.readInt();
		double d=is.readDouble();
		System.out.println(flag+"--"+i+"--"+d);
		is.close();
	}
}

对象流:操作任意类型,包括对象+数据

对象流:数据+数据类型

  • 序列化和反序列化:序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程
  • 序列化输出流 : ObjectOutputStream
  • 反序列化输入流: ObjectInputStream
  • 先序列化后反序列化
  • 不是所有的类都能序列化 实现java.io.Serializable接口
  • 不是所有的属性都需要序列化 transient
  • 静态的内容不能序列化
  • 如果父类有实现序列化,子类没有,子类中所有的内容都能序列化
  • 如果父类中没有实现序列化,子类有实现序列化,子类只能序列化自己的内容
public class ObjectStream {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		writer("Object.txt");
		reader("Object.txt");
	}
	//反序列化输出流
	public static void writer(String dest) throws IOException{
		ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(dest));
		Student stu=new Student("张三",18);
		int[] arr=new int[]{1,2,3,4,5};
		os.writeObject(stu);
		os.writeObject(arr);
		os.flush();
		os.close();
	}
	//序列化输入流
	public static void reader(String src) throws IOException, ClassNotFoundException{
		ObjectInputStream is=new ObjectInputStream(new FileInputStream(src));
		Object obj=is.readObject();
		if(obj instanceof Student){
			Student stu=(Student)obj;
			System.out.println(stu);
		}
		System.out.println(Arrays.toString((int[])is.readObject()));
		
		is.close();
	}
}

class Student implements Serializable{
	private String name;
	private int age;
	public Student() {
		
	}
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	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 [name=" + name + ", age=" + age + "]";
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值