IO流【内存流、打印流、随机访问流】;初识网络编程

day37

IO流

继day36

各种流

对象流

day36

内存流

class ByteArrayInputStream -- 内存输入流

class ByteArrayOutputStream -- 内存输出流
注意:

  1. 内存流是程序和内存交互,跟文件无关
  2. 内存流是程序到内存的通道,是关闭不掉的
    应用场景:项目中频繁使用的数据可以使用内存流备份一份

为什么使用内存流:

ps:内存(服务器)和硬盘(数据库):交互频繁,效率低

即使用Redis缓存数据库,减少交互【就是服务器先与缓存数据库交互】,其中缓存数据库使用到内存流

内存输出流

理解:无参构造32传有参构造,new数组,write方法中循环写入数组,当然其也有相应的扩容机制

不会直接返回数组地址,而是拷贝一份,考虑安全和线程安全,返回数据字符串给你

java 复制代码
public class Test01 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		//关闭资源(内存流是程序到内存的通道,是关闭不掉的)
		//baos.close();
		
		//2.写入数据 -- 将数据写入到baos对象中的byte数组里
		baos.write("123abc木头人".getBytes());
		
		//获取流对象里的数据
		System.out.println(new String(baos.toByteArray()));
		System.out.println(baos.toString());
	}
}
内存输入流
java 复制代码
public class Test02 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		ByteArrayInputStream bais = new ByteArrayInputStream("123abc木头人".getBytes());
		
		//关闭资源(内存流是程序到内存的通道,是关闭不掉的)
		//bais.close();
		
		//2.读取数据
		byte[] bs = new byte[1024];
		int len;
		while((len = bais.read(bs)) != -1){
			System.out.println(new String(bs, 0,len));
		}
		
	}
}

打印流

class PrintStream -- 字节打印流

class PrintWriter -- 字符打印流
注意:打印流实际上就是输出流,只有一个方向(程序->文件)
PrintStream vs PrintWriter

区别1:PrintStream是以字节为单位,PrintWriter是以字符为单位

区别2:

​ PrintStream:将字节流转换为字节打印流

​ PrintWriter:将字节流和字符流转换为字符打印流

字节打印流
java 复制代码
public class Test01 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		//PrintStream ps = new PrintStream("io.txt");
		
		//1.创建流对象(字节流 -> 字节打印流)
		//PrintStream ps = new PrintStream(new FileOutputStream("io.txt"));
		
		//1.创建流对象(字节流 -> 字节打印流) + 在末尾追加
		PrintStream ps = new PrintStream(new FileOutputStream("io.txt",true));
		
		//2.写入数据
		ps.write("沙尘暴".getBytes());
		
		//3.关闭资源
		ps.close();
	}
}
字符打印流

输出流文件不存在的情况都会自动创建

工作中创建字符流一般都添加设置编码格式,但出现不能末尾追加;考虑前面两种情况和考虑效率,即new多个流转化,和带缓冲区的流

java 复制代码
public class Test02 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		//PrintWriter pw = new PrintWriter("io.txt");
		
		//1.创建流对象(字节流 -> 字节打印流)
		//PrintWriter pw = new PrintWriter(new FileOutputStream("io.txt"));
		
		//1.创建流对象(字节流 -> 字节打印流) + 在末尾追加
		//PrintWriter pw = new PrintWriter(new FileOutputStream("io.txt",true));
		
		//1.创建流对象(字符流 -> 字符打印流)
		//PrintWriter pw = new PrintWriter(new FileWriter("io.txt"));
		
		//1.创建流对象(字符流 -> 字符打印流) + 在末尾追加
		//PrintWriter pw = new PrintWriter(new FileWriter("io.txt",true));
		
		//1.创建流对象(设置编码格式 + 在末尾追加 + 考虑到效率)
		PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("io.txt",true), "GBK")));
		
		//2.写入数据
		pw.write("沙尘暴");
		
		//3.关闭资源
		pw.close();	
	}
}
重定向

理解:重新定义系统标准的输入流、输出流、错误输出流的方向

System.in:获取系统标准输入流的方向(控制台->程序)

System.out:获取系统标准输出流的方向(程序->控制台)

System.err:获取系统标准错误输出流的方向(程序->控制台)

//重定向:重新定义系统标准输入流的方向(文件->程序)

System.setIn(new FileInputStream("io.txt"));

//重定向:重新定义系统标准输出流的方向(程序->文件)

System.setOut(new PrintStream(new FileOutputStream("io.txt",true)));

//重定向:重新定义系统标准错误输出流的方向(程序->文件)

System.setErr(new PrintStream(new FileOutputStream("io.txt",true)));
回顾:day19【集合之前】标准输入、出、错误输出流有学习

重定向系统标准输入流的方向

System.in:获取系统标准输入流的方向(控制台->程序)

java 复制代码
public class Test03 {
	public static void main(String[] args) throws FileNotFoundException {
		
		//重定向:重新定义系统标准输入流的方向(文件->程序)
		System.setIn(new FileInputStream("io.txt"));
		
		InputStream in = System.in;
		
		Scanner scan = new Scanner(in);
		String str = scan.next();
		System.out.println(str);
		scan.close();
	}
}
重定向系统标准输出流的方向

System.out:获取系统标准输出流的方向(程序->控制台)

字节打印流

java 复制代码
public class Test04 {
	public static void main(String[] args) throws FileNotFoundException {
		
		//重定向:重新定义系统标准输出流的方向(程序->文件)
		System.setOut(new PrintStream(new FileOutputStream("io.txt",true)));
		
		PrintStream ps = System.out;
		ps.println("长城炮");
	}	
}
重定向系统错误输出流的方向

System.err:获取系统标准错误输出流的方向(程序->控制台)

另:打印到文件不会是红色,是黑色

java 复制代码
public class Test05 {
	public static void main(String[] args) throws FileNotFoundException {
		
		//重定向:重新定义系统标准错误输出流的方向(程序->文件)
		System.setErr(new PrintStream(new FileOutputStream("io.txt",true)));
		
		PrintStream ps = System.err;
		ps.println("苍山雪洱海月");
	}
}

随机访问流

class RandomAccessFile
理解:该流认为文件是一个大型的byte数组。有一个隐藏的指针(默认为0),其实就是下标,可以从指针的位置写入或读取,意味着该流两个方向
模式:r-读,rw-读写

利用随机访问流 向文件写入数据

rw原因:读的权限比写的权限高

写入是替换对应字节数的内容,不像前面的流是全部清空替换

需求1:

向文件写入 数字、英文、中文数据

java 复制代码
public class Test01 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		RandomAccessFile w = new RandomAccessFile("io.txt", "rw");
		
		//2.写入数据
		w.write("123abc木头人".getBytes());
		
		//3.关闭资源
		w.close();
	}
}
需求2:

在文件末尾追加

java 复制代码
public class Test02 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		File file = new File("io.txt");
		RandomAccessFile w = new RandomAccessFile(file, "rw");
		
		//设置指针的位置
		w.seek(file.length());
		
		//2.写入数据
		w.write("123abc木头人".getBytes());
		
		//3.关闭资源
		w.close();
	}
}
利用随机访问流 读取文件里的数据
需求1:

读取数据

java 复制代码
public class Test03 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		RandomAccessFile r = new RandomAccessFile("io.txt", "r");

		//2.读取数据
		byte[] bs = new byte[1024];
		int len;
		while((len = r.read(bs)) != -1){
			System.out.println(new String(bs, 0, len));
		}
		
		//3.关闭资源
		r.close();
	}
}
需求2:

从英文处开始读取数据

ps:123abc木头人

java 复制代码
public class Test04 {
	public static void main(String[] args) throws IOException {
		
		//1.创建流对象
		RandomAccessFile r = new RandomAccessFile("io.txt", "r");

		//设置指针的位置
		r.seek(3);
		
		//2.读取数据
		byte[] bs = new byte[1024];
		int len;
		while((len = r.read(bs)) != -1){
			System.out.println(new String(bs, 0, len));
		}
		
		//3.关闭资源
		r.close();
	}
}
拷贝文件 -- 断点续传

设置指针,读写都要设置,保证断点续传,即从指针位置继续

java 复制代码
public class Copy {
	public static void main(String[] args) throws IOException {
		
		RandomAccessFile r = new RandomAccessFile("奇男子.mp4", "r");
		File targetFile = new File("copy.mp4");
		RandomAccessFile w = new RandomAccessFile(targetFile, "rw");
		
		//设置指针
		long fileLength = targetFile.length();
		r.seek(fileLength);
		w.seek(fileLength);
		
		byte[] bs = new byte[1024];
		int len;
		while((len = r.read(bs)) != -1){
			w.write(bs, 0, len);
		}
		
		r.close();
		w.close();
	}
}

初识网络编程

实现多台计算机之间实现数据的共享和传递,网络应用程序主要组成为:

网络编程+IO流+多线程

理解图

TCP、UDP都是传输层的协议, HTTP属于应用层协议

TCP

​ 建立连接,形成传输数据的通道;在连接中进行大数据量传输;通过三次握手完成连接,是可靠协议;必须建立连接,效率会稍低,例如:打电话

UDP

​ 将数据源和目的封装到数据包中,不需要建立连接;每个数据报的大小在限制在64k;因无连接,是不可靠协议;不需要建立连接,速度快:例如发短信

HTTP

​ 建立连接:客户端通过TCP/IP协议建立与服务器的连接,发送请求:客户端发送HTTP请求,服务器处理,发送响应, 接收响应;关闭连接:在完成请求和响应后,客户端和服务器都可以选择关闭连接,释放资源。

总结

1.内存流

2.打印流

字节打印流、字符打印流、重定向

3.随机访问流

4.初识网络编程
流的大类:BIO(学)、NIO、AIO

相关推荐
Bluesonli3 分钟前
第 2 天:创建你的第一个 UE5 C++ 项目!
c++·学习·ue5·虚幻·虚幻引擎·unreal engine
前端达人22 分钟前
「AI学习笔记」深度学习进化史:从神经网络到“黑箱技术”(三)
人工智能·笔记·深度学习·神经网络·学习
taopi202422 分钟前
android java系统弹窗的基础模板
android·java·开发语言
蝴蝶不愿意44 分钟前
《苍穹外卖》项目学习记录-Day10来单提醒
学习
pay顿1 小时前
C++基础day1
c++·学习·笔试
松仔log1 小时前
Java多线程——对象的组合
java·开发语言·jvm
Qhumaing1 小时前
Python学习——函数参数详解
开发语言·python·学习
酷爱码1 小时前
springboot 动态配置定时任务
java·spring boot·后端
从未止步..1 小时前
Jenkins未在第一次登录后设置用户名,第二次登录不进去怎么办?
java·运维·jenkins
老马啸西风2 小时前
IM 即时通讯系统-42-基于netty实现的IM服务端,提供客户端jar包,可集成自己的登录系统
java