数据源到程序(InputStream、Reader读进来)
程序到目的地(OutPutStream、Writer写出去)
可以直接从数据源或目的地读写数据。
不直接连接到数据源或目的地,是处理流的流。通过对其他流的处理提高程序的性能。
按照字节读取数据(InputStream、OutputStream)
字节流能够处理一切的内容,包括文本和非文本, 可能存在乱码,但是我们可以设置编码格式解决乱码。麻烦
int read(byte[] bs)
import java.io.*;
public class Demo003ReadStream {
public static void main(String[] args) throws IOException {
// 抽象文件对象
File file = new File("a.txt");
// 提升作用域
InputStream is = null;
// 中文 在 GBK 占两个字节 在utf-8占3个字节
// 打开流
is = new FileInputStream(file);
byte[] bs = new byte[1024];
int len;
while ((len = is.read(bs)) != -1) {
System.out.println(new String(bs, 0, len, "UTF-8"));
}
if (is != null) {
is.close();
}
}
}
//运行结果
你我他1234
write(byte[] bs)
import java.io.*;
/**
* 字节流写出文件
* 1、文件
* 2、流
*/
public class Demo006WriterStream {
public static void main(String[] args) throws IOException {
// 1、文件
File file = new File("a.txt");
// 2、流
OutputStream os = new FileOutputStream(file, true); // 第二个参数 appeend:true 可以追加,不覆盖
byte[] bs = "ab".getBytes("utf-8");
// 写出
os.write(bs);
// 刷新
os.flush();
os.close();
}
}
运行结果
int read(byte[] car)
write(byte b[], int off, int len)
package com.shsxt.io;
import java.io.*;
public class Demo009CopyStream {
public static void main(String[] args) throws IOException {
// 源头
File src = new File("test.png");
// 目的地
File dest = new File("testcopy.png");
InputStream is = new FileInputStream(src);
OutputStream os = new FileOutputStream(dest);
byte[] car = new byte[2];
int len ;
while((len=is.read(car))!=-1){
os.write(car,0,len);
}
os.flush();
os.close();
is.close();
}
}
BufferedInputStream(InputStream in) 创建一个BufferedInputStream 并保存其参数,输入流 in ,供以后使用。
BufferedInputStream(InputStream in, int size) 创建BufferedInputStream 具有指定缓冲区大小,并保存其参数,输入流in ,供以后使用。
int read(byte[] car)
import java.io.*;
/**
* 处理流 需要有结点留存在
*/
public class Demo001BufferedInputStream {
public static void main(String[] args) throws IOException {
// 获取节点流连接到文件
InputStream is = new FileInputStream("a.txt");
// 处理流 套在结点流之上
BufferedInputStream bis = new BufferedInputStream(is);
byte[] car = new byte[1024];
int len;
while((len = bis.read(car))!=-1){
System.out.println(new String(car,0,len,"utf-8"));
}
// 内部会帮我们关闭其他流
bis.close();
}
}
write(byte b[])
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Demo003BufferedOutputStream {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(os);
bos.write("核力量".getBytes());
bos.flush();
bos.close();
}
}
按照字符读取数据(Reader、Writer)
字符流只能处理文本,要嘛不出乱码,要嘛出乱码了解决不了
处理的就是纯文本,字符流简单, 但是可能有乱码,解决不了
int read()读取
import java.io.*;
/**
* 字符流
*/
public class Demo004Reader {
public static void main(String[] args) throws IOException {
// 文件
File file = new File("a.txt");
Reader r = new FileReader(file);
// 一个一个的字符读取,每个字符对应的ascii码
int ch ;
while((ch=r.read())!=-1){
System.out.println((char)ch);
}
if(r!=null){
r.close();
}
}
}
//运行结果
你
我
他
1
2
3
4
int read(char[] c)读取
import java.io.*;
import java.util.Arrays;
public class Demo005Reader {
public static void main(String[] args) throws IOException {
// 文件
File file = new File("a.txt");
Reader r = new FileReader(file);
// 接收读取到的字符个数
int len ;
char[] c = new char[3];
while((len = r.read(c))!=-1){
System.out.println(new String(c,0,len));
}
r.close();
}
}
//运行结果
你我他
123
4
write(String str)
package com.shsxt.io;
import java.io.*;
public class Demo007Writer {
public static void main(String[] args) throws IOException {
File file = new File("a.txt");
Writer w = new FileWriter(file,true);
String str = new String("xy".getBytes("utf-8"));
w.write(str);
w.flush();
w.close();
}
}
运行结果
int read()
write(int c)
int read(byte[] car)
write(byte b[], int off, int len)
package com.shsxt.io;
import java.io.*;
/**
* 字符流拷贝
* 读 -> 写
*/
public class Demo008CopyChar {
public static void main(String[] args) throws IOException {
// 源头
File src = new File("a.txt");
// 目的地
File dest = new File("acopy.txt");
Reader r = new FileReader(src);
Writer w = new FileWriter(dest);
// 使用read()进行copy
// int c ;
// while((c=r.read())!=-1){
// w.write(c);
// }
//使用read(char[] car)数组进行copy
char[] car = new char[1024];
int len ;
// 循环读取
while((len=r.read(car))!=-1){
w.write(car,0,len);
}
w.flush();
w.close();
r.close();
}
}
BufferedReader(Reader in) 创建使用默认大小的输入缓冲区的缓冲字符输入流。
BufferedReader(Reader in, int sz) 创建使用指定大小的输入缓冲区的缓冲字符输入流。
String readLine()
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Demo002BufferedReader {
public static void main(String[] args) throws IOException {
Reader r = new FileReader("a.txt");
BufferedReader br = new BufferedReader(r);
String data;
while(null!=(data=br.readLine())){
System.out.println(data);
}
br.close();
}
}
write(byte b[])
package com.shsxt.bufferdio;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class Demo003BufferedOutputStream {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("a.txt",true);
BufferedOutputStream bos = new BufferedOutputStream(os);
bos.write("核力量".getBytes());
bos.flush();
bos.close();
}
}
处理的都是字符(字节、字符)
字符流: 方便,可能不出乱码,但是一旦别人有乱码,你这个流就一定乱码
字节流:麻烦,但是只要你能够有机会不乱码,我都可以做到
转换流:将字节流转为字符流 处理乱码(编码集、解码集)。
import java.io.*;
/**
* 转换流:
* 字节流-> 字符流
*
* 文件 GBK
* 程序 UTF-8
*/
public class Demo001 {
public static void main(String[] args) throws IOException {
// 解决乱码而且方便
// 通过字节流读取文件内容, 由转换流内部帮我们将字节转为字符,而且我们设置了编码,这是文件的编码
BufferedReader bufferedReader =new BufferedReader( new InputStreamReader(new FileInputStream("a.txt"),"GBK" ) );
String str1 ;
while((str1=bufferedReader.readLine())!=null){
System.out.println(str1);
}
bufferedReader.close();
}
}
必须先写,再读。读的顺序必须和写的顺序保持一致
可以处理基本类型+String,保留数据的类型。前提是读取顺序与写出顺序一致,否则读取数据不正确
import java.io.*;
/**
* 处理流 结点流
*/
public class Demo001DataOutputStream {
public static void main(String[] args) throws IOException {
OutputStream os = new FileOutputStream("b.txt");
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF("趣味");
dos.flush();
dos.close();
}
}
运行结果
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo002DataInputStream {
public static void main(String[] args) throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("b.txt"));
String str = dis.readUTF();
System.out.println(str);
dis.close();
}
}
//运行结果
趣味
以前我们学过的流都只能读写字节,字符形式的数据,而java中非常重要并且常见的对象类型的数据,如果想要存储到文件中应该怎么操作呢?这个时候就使用到了对象流。
序列化:是一个用于将对象状态转换为字节流的过程,可以将其保存到磁盘文件中或通过网络发送到任何其他程序
反序列化:从字节流创建对象的相反的过程称为 反序列化
package com.shsxt.dataStream;
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private transient int age; // 属性将不被序列化
private boolean gender;
public Person() {
System.out.println("Person.Person");
}
public Person(String name, int age, boolean gender) {
this.name = name;
this.age = age;
this.gender = gender;
System.out.println("Person.Person.....");
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
'}';
}
}
package com.xxxx.dataStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/**
* 对象流
*/
public class Demo003ObjectOutputStream {
public static void main(String[] args) throws IOException {
// 节点流
OutputStream os = new FileOutputStream("c.txt");
// 处理流 对象流
ObjectOutputStream oos = new ObjectOutputStream(os);
// 写出对象
oos.writeObject(new Person("zs",18,true));
oos.flush();
oos.close();
}
}
运行结果
package com.xxxx.dataStream;
import java.io.*;
/**
* 创建对象的方式:
* 1、new
* 2、克隆
* 3、反序列化--- 不走构造器
*/
public class Demo004ObjectInputStream {
public static void main(String[] args) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c.txt"));
Person p = (Person)ois.readObject();
System.out.println(p);
ois.close();
}
}
//运行结果
Person{
name='zs', age=0, gender=true}
}
运行结果
[外链图片转存中…(img-QSPv6PpL-1627040042518)]
[外链图片转存中…(img-oyzfAp79-1627040042519)]
package com.xxxx.dataStream;
import java.io.*;
/**
* 创建对象的方式:
* 1、new
* 2、克隆
* 3、反序列化--- 不走构造器
*/
public class Demo004ObjectInputStream {
public static void main(String[] args) throws Exception {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("c.txt"));
Person p = (Person)ois.readObject();
System.out.println(p);
ois.close();
}
}
//运行结果
Person{
name='zs', age=0, gender=true}
文章浏览阅读645次。这个肯定是末尾的IDAT了,因为IDAT必须要满了才会开始一下个IDAT,这个明显就是末尾的IDAT了。,对应下面的create_head()代码。,对应下面的create_tail()代码。不要考虑爆破,我已经试了一下,太多情况了。题目来源:UNCTF。_攻防世界困难模式攻略图文
文章浏览阅读2.9k次,点赞3次,收藏10次。偶尔会用到,记录、分享。1. 数据库导出1.1 切换到dmdba用户su - dmdba1.2 进入达梦数据库安装路径的bin目录,执行导库操作 导出语句:./dexp cwy_init/[email protected]:5236 file=cwy_init.dmp log=cwy_init_exp.log 注释: cwy_init/init_123..._达梦数据库导入导出
文章浏览阅读1.9k次。1. 在官网上下载KindEditor文件,可以删掉不需要要到的jsp,asp,asp.net和php文件夹。接着把文件夹放到项目文件目录下。2. 修改html文件,在页面引入js文件:<script type="text/javascript" src="./kindeditor/kindeditor-all.js"></script><script type="text/javascript" src="./kindeditor/lang/zh-CN.js"_kindeditor.js
文章浏览阅读2.3k次,点赞6次,收藏14次。SPI的详情简介不必赘述。假设我们通过SPI发送0xAA,我们的数据线就会变为10101010,通过修改不同的内容,即可修改SPI中0和1的持续时间。比如0xF0即为前半周期为高电平,后半周期为低电平的状态。在SPI的通信模式中,CPHA配置会影响该实验,下图展示了不同采样位置的SPI时序图[1]。CPOL = 0,CPHA = 1:CLK空闲状态 = 低电平,数据在下降沿采样,并在上升沿移出CPOL = 0,CPHA = 0:CLK空闲状态 = 低电平,数据在上升沿采样,并在下降沿移出。_stm32g431cbu6
文章浏览阅读1.2k次,点赞2次,收藏8次。数据链路层习题自测问题1.数据链路(即逻辑链路)与链路(即物理链路)有何区别?“电路接通了”与”数据链路接通了”的区别何在?2.数据链路层中的链路控制包括哪些功能?试讨论数据链路层做成可靠的链路层有哪些优点和缺点。3.网络适配器的作用是什么?网络适配器工作在哪一层?4.数据链路层的三个基本问题(帧定界、透明传输和差错检测)为什么都必须加以解决?5.如果在数据链路层不进行帧定界,会发生什么问题?6.PPP协议的主要特点是什么?为什么PPP不使用帧的编号?PPP适用于什么情况?为什么PPP协议不_接收方收到链路层数据后,使用crc检验后,余数为0,说明链路层的传输时可靠传输
文章浏览阅读587次。软件测试工程师移民加拿大 无证移民,未受过软件工程师的教育(第1部分) (Undocumented Immigrant With No Education to Software Engineer(Part 1))Before I start, I want you to please bear with me on the way I write, I have very little gen...
文章浏览阅读304次。Thinkpad X250笔记本电脑,装的是FreeBSD,进入BIOS修改虚拟化配置(其后可能是误设置了安全开机),保存退出后系统无法启动,显示:secure boot failed ,把自己惊出一身冷汗,因为这台笔记本刚好还没开始做备份.....根据错误提示,到bios里面去找相关配置,在Security里面找到了Secure Boot选项,发现果然被设置为Enabled,将其修改为Disabled ,再开机,终于正常启动了。_安装完系统提示secureboot failure
文章浏览阅读10w+次,点赞93次,收藏352次。1、用strtok函数进行字符串分割原型: char *strtok(char *str, const char *delim);功能:分解字符串为一组字符串。参数说明:str为要分解的字符串,delim为分隔符字符串。返回值:从str开头开始的一个个被分割的串。当没有被分割的串时则返回NULL。其它:strtok函数线程不安全,可以使用strtok_r替代。示例://借助strtok实现split#include <string.h>#include <stdio.h&_c++ 字符串分割
文章浏览阅读2.3k次。1 .高斯日记 大数学家高斯有个好习惯:无论如何都要记日记。他的日记有个与众不同的地方,他从不注明年月日,而是用一个整数代替,比如:4210后来人们知道,那个整数就是日期,它表示那一天是高斯出生后的第几天。这或许也是个好习惯,它时时刻刻提醒着主人:日子又过去一天,还有多少时光可以用于浪费呢?高斯出生于:1777年4月30日。在高斯发现的一个重要定理的日记_2013年第四届c a组蓝桥杯省赛真题解答
文章浏览阅读851次,点赞17次,收藏22次。摘要:本文利用供需算法对核极限学习机(KELM)进行优化,并用于分类。
文章浏览阅读1.1k次。一、系统弱密码登录1、在kali上执行命令行telnet 192.168.26.1292、Login和password都输入msfadmin3、登录成功,进入系统4、测试如下:二、MySQL弱密码登录:1、在kali上执行mysql –h 192.168.26.129 –u root2、登录成功,进入MySQL系统3、测试效果:三、PostgreSQL弱密码登录1、在Kali上执行psql -h 192.168.26.129 –U post..._metasploitable2怎么进入
文章浏览阅读257次。本文将为初学者提供Python学习的详细指南,从Python的历史、基础语法和数据类型到面向对象编程、模块和库的使用。通过本文,您将能够掌握Python编程的核心概念,为今后的编程学习和实践打下坚实基础。_python人工智能开发从入门到精通pdf