Java学习第31天---I/O框架

千锋逆战班:孙华建
在千锋学习第31天
“未来的你会感谢今天奋斗的自己”
今天我学习了java课程,I/O框架
#中国加油!武汉加油!千锋加油!也为自己加油!!!#…

课堂案例:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestFileOutputStream {

	public static void main(String[] args) throws IOException {
		//1,输出字节流 OutputStream,
		//2,输出字节节点流,具有实际传输数据的功能(文件路径,boolean append) true代表追加,不覆盖
		//路径正确,文件不存在,则会自动创建一个文件
		//绝对路径,"D:\\课堂案例\\target.txt"必须存在
		//相对路径,"Files\\target.txt"),与上一种形式等价,相对于当前项目的路径下,寻找该路径和文件
		FileOutputStream fos = new FileOutputStream("Files\\target.txt");	
		
		String s = "你";
		
		byte[] bs = s.getBytes();
		System.out.println(bs[0]+ "\t" +bs[1]);
		
		fos.write(bs);
		
		fos.write('你');
		
//		fos.write(65);
//		
//		fos.write(66);
//		
//		fos.write(67);
//		
//		fos.write('D');
		
//		byte[] bytes = new byte[]{65,66,67,68,69,70,'G'};
//		
//		fos.write(bytes); //一次输出一组字节
		//字节数组,起始下标,长度
//		fos.write(bytes, 1, 3);

	}

}

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

public class TestFileInputStream {

	public static void main(String[] args)throws IOException {
		FileInputStream fis = new FileInputStream("Files\\target.txt");
		
//		while(true){
//			int n = fis.read();
//			if(n == -1){
//				break;
//			}
//			System.out.println((char)n);
//		}
		
		byte[] bytes = new byte[4];
		
		while(true){
			int count = fis.read(bytes);
			
			if(count == -1){
				break;
			}
			
			for(int i = 0;i<count;i++){
				System.out.print((char)bytes[i]+"\t");
			}
			System.out.println();
		}
		
		
		
			
	
	}

}


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

public class TestFileIO {

	public static void main(String[] args) throws IOException {
		//将指定图片上传一份到项目里
		
		//文件是在存储设备中的---》读到程序中---》写到存储设备中去
		//输入流
		FileInputStream fis = new FileInputStream("C:\\Users\\winter\\Desktop\\link.jpg");
		//输出流
		FileOutputStream fos = new FileOutputStream("Files\\宠物.jpg");
		//9273,空间过大过小都有问题
		int len  = 0;//每次读到的字节
		
		while((len = fis.read())!= -1){//只要读到的不是-1
			
			fos.write(len);//读多少写多少
	
		}
		
				
		
		
		//释放资源
		fis.close();
		fos.close();
	}

}

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

public class TestBufferesOutPut {
	public static void main(String[] args) throws IOException {
		//有参构造需要一个字节输出节点流
		//先创建字节输出节点流
		FileOutputStream fos = new FileOutputStream("Files\\buff.txt");
		//增强节点流
		BufferedOutputStream bos = new BufferedOutputStream(fos);
		//过滤流的write方法,是先写入缓冲区
		bos.write('A');
		bos.write('B');
		bos.write('C');
		bos.write('D');
		
		bos.flush();//刷新缓冲区,将缓冲区数据一次性写入到文件中,并清空当前缓冲区
		
		bos.write('E');
		bos.write('F');
		
		bos.close();
		
		//-------------------------------
		FileInputStream fis = new FileInputStream("Files\\buff.txt");
		
		byte[] bytes = new byte[100];
		
		fis.read(bytes);
		
		for(int i = 0;i<bytes.length;i++){
			System.out.println((char)bytes[i]);
		}
	}
}


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

public class TestObjectStream {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		OutputStream os = new FileOutputStream("Files\\obj.txt");
		
		ObjectOutputStream oos = new ObjectOutputStream(os);
		
		Student stu = new Student("小明",13,"男",100D,new Address("北京","100000"));
		Student stu1 = new Student("小白",12,"男",99D,new Address("北京","100000"));
		Student stu2 = new Student("小红",13,"女",100D,new Address("北京","100000"));
		
		
		oos.writeObject(stu);
		oos.writeObject(stu1);
		oos.writeObject(stu2);
		
		oos.flush();
		
		InputStream is = new FileInputStream("Files\\obj.txt");
		ObjectInputStream ois = new ObjectInputStream(is);
		
//		Object obj = ois.readObject();
//		Student s = (Student)result;
//		System.out.println(obj);
//		System.out.println(s.name);
		
		while(true){
			try{
				Object obj = ois.readObject();
				System.out.println(obj);
			}catch(Exception e){
				break;
			}			
		}		
	}
}

//static属于类,会影响序列化
class Student implements Serializable{
	String name;
	Integer age;
	String sex;
	Double score;
	transient Address add;//表示不参与序列化
	//引用数据类型需要序列化!!
	public Student(String name, Integer age, String sex, Double score,Address add) {
		super();
		this.add = add;
		this.name = name;
		this.age = age;
		this.sex = sex;
		this.score = score;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + ", score=" + score + ", add=" + add + "]";
	}
	
}

class Address implements Serializable{
	String position;
	String zipCode;
	public Address(String position, String zipCode) {
		super();
		this.position = position;
		this.zipCode = zipCode;
	}
	@Override
	public String toString() {
		return "Address [position=" + position + ", zipCode=" + zipCode + "]";
	}
	
	
}

课堂笔记:
在这里插入图片描述

作业:
3,
在这里插入图片描述
输入流;字节流;节点流

4,
在这里插入图片描述
I, int; 输入字符数
读入字节个数; 输入字符用一个数组保存
读入字节个数; 输入字符用bs数组保存,从下标…开始,保存…

5,
在这里插入图片描述
AB

6,
在这里插入图片描述
I,创建一个文件;则覆盖此文件
II,再次创建追加;会

7,
在这里插入图片描述
改错:第二个错误标注错误,应该是将fin.close()放在finally里
8,
在这里插入图片描述

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

public class TestFileIO {

	public static void main(String[] args) throws IOException {
		
		//I
		FileOutputStream os = new FileOutputStream("test.txt");
		String s = "Hello World";
		byte[] b = s.getBytes();
		os.write(b);
		
		//II,III
		FileInputStream is = new FileInputStream("test.txt");
		
		try{
			is.read(b);
			for(int i = 0;i<b.length;i++){
				System.out.print((char)b[i]);
			}
		}catch(Exception e){
			
		}finally{
			os.close();
			is.close();
		}
		
		
	}

}

在这里插入图片描述
13,
Serializable;transient

15,
在这里插入图片描述
第一个是打印对象的toString,第二个指向流中写入一个对象

16,
在这里插入图片描述
在这里插入图片描述
B Address没有实现Serializable接口

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值