37 IO流【各种流(内存流、打印流、随机访问流)】、初识网络编程

本文详细介绍了Java中的IO流,包括内存流(ByteArrayInputStream和ByteArrayOutputStream)、字节打印流与字符打印流(PrintStream和PrintWriter),以及随机访问流(RandomAccessFile)的用法。同时涉及网络编程的基础概念,如TCP、UDP和HTTP协议的应用。
摘要由CSDN通过智能技术生成

day37

IO流

各种流

2.内存流

class ByteArrayInputStream – 内存输入流

class ByteArrayOutputStream – 内存输出流

注意:

  1. 内存流是程序和内存交互,跟文件无关
  2. 内存流是程序到内存的通道,是关闭不掉的

应用场景:项目中频繁使用的数据可以使用内存流备份一份

为什么使用内存流:

ps:内存(服务器)和硬盘(数据库):交互频繁,效率低
使用Redis缓存数据库,减少交互【就是服务器先与缓存数据库交互】,其中缓存数据库使用到内存流

内存输出流

理解:无参构造,有参构造,new数组,write方法中循环写入数组,当然其也有相应的扩容机制
不会直接返回数组地址,而是拷贝一份,考虑安全和线程安全,返回数据字符串

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());
	}
}
内存输入流
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));
		}
		
	}
}

3.打印流

class PrintStream – 字节打印流

class PrintWriter – 字符打印流

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

PrintStream vs PrintWriter

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

区别2:

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

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

字节打印流
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多个流转化,和带缓冲区的流

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:获取系统标准输入流的方向(控制台->程序)

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:获取系统标准输出流的方向(程序->控制台)

字节打印流

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:获取系统标准错误输出流的方向(程序->控制台)

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

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("苍山雪洱海月");
	}
}

4.随机访问流

class RandomAccessFile

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

模式:r-读,rw-读写

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

rw原因:读的权限比写的权限高
写入是替换对应字节数的内容,不像前面的流是全部清空替换

需求1:

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

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:

在文件末尾追加

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:

读取数据

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木头人

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();
	}
}
拷贝文件 – 断点续传

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

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

注意流的大类:BIO(学)、NIO、AIO

初识网络编程

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

网络编程+IO流+多线程

理解

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

TCP

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

UDP

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

HTTP

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

初识网络编程理解图

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值