文章目录
简介
- 本文是2021/04/30整理的笔记
- 赘述可能有点多,还请各位朋友耐心阅读
- 本人的内容和答案不一定是最好最正确的,欢迎各位朋友评论区指正改进
易错练习
练习1
以下说法正确的是?【单项选择题题】【简单】
A.RandomAccessFile类是File类的子类
B.FileWriter类提供有操作基本数据类型的方法
C.RandomAccessFile类提供有删除磁盘文件的方法
D.File类提供有删除磁盘文件的方法
答案:D
练习2
以下代码的执行结果是?【单项选择题】【简单】
public class Example {
public static void main(String[] args) throws IOException {
RandomAccessFile file = new RandomAccessFile(“test.txt”, “rw”);
file.writeBoolean(true);
file.writeInt(123456);
file.writeInt(7890);
file.writeLong(10000000);
file.writeInt(777);
file.writeFloat(0.0001f);
file.seek(5);
System.out.println(file.readInt());
file.close();
}
}
A.123456
B.7890
C.10000000
D.777
答案:B
练习3
在输入流的read方法返回哪个值的时候表示读取结束?【单项选择题】【简单】
A.0
B.1
C.-1
D.null
答案:C
练习4
有如下代码:【单项选择题题】【简单】
public class Example implements Serializable {
Collar c = new Collar();
}
class Collar implements Serializable {
CollarPart cp1 = new CollarPart();
CollarPart cp2 = new CollarPart();
}
class CollarPart implements Serializable {
}
如果Example实例被序列化,则多少对象将被序列化?
A.1
B.2
C.3
D.4
答案:D
练习5
请问以下哪些修饰符用于修饰类属性时,该属性不能被序列化?【多项选择题】【简单】
A.private
B.static
C.transient
D.protected
答案:B、C
文件知识点复习
什么是文件
文件可以认为是相关记录或放在一起的数据的集合
File
代表文件或者文件夹
- 文件:new File(String pathname) pathname:文件名或者路径名/文件名
- 文件夹:new File(Sting pathname) pathname 路径名
File最常用的方法
- exists():代表文件|文件夹是否存在
- isFile():代表是否是文件
- isDirectory():是否是文件夹
- getName() 获得名称
- getAbsolutePath():得到绝对路径
- length():文件的大小
- createNewFile() 创建文件
- delete删除文件
- listFiles() 返回文件列表数组
- mkdir()创建单级目录
- 级联创建多级目录
FilenameFilter 过滤某一个文件夹下的所有以xxx结尾或者开头或者包含的文件列表
@Test
public void test(){
File file = new File("D:\\images");
File[] files = file.listFiles((dir, name) -> name.endsWith(".jpg"));
for (File file1 : files) {
System.out.println("file1 = " + file1);
}
}
路径
- 绝对路径:/或者\开头 代表项目所在磁盘的根目录:比如项目在D:/a/b/c下,则/代表D盘的根目录就是D:/
- 相对路径:new File(“a.txt”),则a.txt在当前项目的根目录下
IO流
流的概念
流是一串连续不断的数据的集合
流的分类
- 输入流和输出流
- 字节流和字符流
- 流式 字节流和字符流
- 非流式 File RandomAccessFile
- 其他
字节流
- InputStream
- FileInputStream
- 构造方法new FileInputStream(“文件名”)
- 读方法
byte[] bytes = new byte[1024];
int len;
while((len = in.read(bytes))!=-1){
new String (bytes,0,len);
}
- ObjectInputStream
- 构造方法 new ObjectInputStream(new FileInputStream(“文件名”))
- 对象类型 对象名 = (对象类型)in.readObject()
- ByteArrayInputStream
- 构造方法 new ByteArrayInputStream(byte[ ])
- read()
- FilterInputStream
- DataInputStream
- 构造方法:new DataInputStream(new FileInputStream(“文件名”))
- readXXX() XXX代表基本数据类型
- BufferedInputStream
- PushBackInputStream
- 构造方法
- new PushBackInputStream(new FileInputStream(文件名)) 一次回推一个字节
- new PushBackInputStream(new FileInputStream(文件名),n) 一次最多回推n个字节
- 回推1个 unread(int)
- 回推多个 unread(byte[ ])
回推一部分 unread(byte[ ],int off,int len)
- 构造方法
- DataInputStream
- FileInputStream
- OutputStream
- FileOutputStream
- write(byte[ ]) 将字节数组从0开始,byte[ ].length个长度写到流中
- write(byte[ ], int off, int len) 将字节数组从off开始,len个长度写到流中
- write(int n) 将int的低8位1个字节写到流当中
- flush() 刷新流 将数据写到目标
- close() 关流 释放资源
- ObjectOutputStream
- 构造方法 new ObjectOutputStream(new FileOutputStream(“文件名”))
- 对象类实现Serializable 生成serialVersionUID
- writeObject(对象)
- ByteArrayOutputStream
- 构造方法new ByteArrayOutputStream
- write(int n)
- FilterOutputStream
- DataOutputStream
- 构造方法 newDataOutputStream(new FileOutputStream(“文件名”))
- writeXXX(对应的参数) XXX代表基本数据类型
- BufferedOutputStream
- PrintOutputStream
- DataOutputStream
- FileOutputStream
文件复制
in = new FileInputStream(“文件名”)
out = new FileOutputStream(“文件名”)
字符流
- Reader
- FileReader
- 构造方法
- read
- BufferedReader
- 构造方法
new BufferedRead(new FileRead(文件名)) - 方法
read
- 构造方法
- CharArrayReader
- InputStreamReader
- FilterReader
- PushBackReader
- FileReader
- Writer
- FileWriter
- 构造方法
- new FileWriter(File file) 覆盖原来的内容
- new FileWriter(File file,boolean append) 不覆盖原来的内容,追加新内容
- new FileWriter(String filename) 覆盖原来的内容
- new FileWriter(String filename,boolean append) 不覆盖原来的内容,追加新内容
- write方法
- 构造方法
- BufferedWriter
- 构造方法
new BufferedWriter(new FileWriter(文件名)) - write方法
- write(String)
- write(String s,int off,int len)
- newLine() 写一个行分隔符
- 构造方法
- CharArrayWriter
- OutputStreamWriter
- FilterWriter
- PushBackWriter
- PrintWriter
- FileWriter
自定义序列化
在实现了Serializable接口的类里写2个方法
private void writeObject(ObjectOutputStream out){
out.defaultWriteObject();//调用默认序列化的方法
out.writeUTF(address+"");
}
private void readObject(ObjectInputStream in){
in.defaultReadObject();//默认
address=in.readUTF();
}
实现Externalizable接口
重写两个方法
writeExternal(ObjectOutput out)
readExternal(ObjectInput in)
transient static 修饰的属性不会序列化
Scanner
- next() 取得下一个字符串,遇到空格停止
- nextInt() 取得下一个整数,遇到空格停止
- newLine() 取得一整行数据(空格含在内)
int [] a = new int[10]
for(int i = 0;i<a.length;i++){
a[i] = scanner.nextInt();
}
1 2 3 4 5 6 7 8 9 10
网络编程
常见协议
TCP:Transmission control Protocol 传输控制协议
IP:Internet Protocol 网络协议
UDP:User Datagram Protocol 用户数据报协议
ICMP:Internet Control Message Protocol 网络控制报文协议
IGMP:Internet Group Message Protocol 网络组报文协议
ARP:Address Resolution Protocol 地址解析协议
HTTP:Hyper Text Transfer Protocol超文本传输协议
FTP File Transfer Protocol 文件传输协议
IP:4位 用.分隔的整数 192.168.70.254
网段(子网) IP的前几位 一般是前3位 同一网段的主机是互通的
TCP与UDP的区别
- TCP 建立连接可靠 UDP不建立连接不可靠
- TCP使用字节流发送 UDP使用数据报发送
- TCP慢 UDP快
HTTP协议
- 60年美国人Ted Nelson
- 请求方式
- GET:请求参数在url?参数名称=参数值=&参数名称2=参数值2 没有请求体 不安全
- 用户在浏览器地址栏输入网址,打Enter
- 点击网页的超链接
- POST 请求参数在请求体中 安全
- 用户登录 填写表单数据
- GET:请求参数在url?参数名称=参数值=&参数名称2=参数值2 没有请求体 不安全
- HTTP协议的请求报文格式
- 请求行:GET 资源路径名称?参数名称=参数值&参数名称2=参数值2 HTTP/版本号 换行符(Windows系统下\r\n)
- 请求头:
名称: 值\r\n- Accept:image/gif;imape/jpeg;text/html;* / * 浏览器能接受的MIME类型\r\n
- Accept - Language:zh-cn 浏览器能接受的语言\r\n
- User - Agent:用户代理说白了就是当前使用的浏览器的类型名称和对应的版本信息\r\n
- content-Length: 12 请求体的字节长度\r\n
- 空行\r\n
- 请求体 参数名称=参数值=&参数名称2=参数值2
- HTTP协议响应报文格式
- 状态行:HTTP/1.1 状态码 状态描述信息\r\n
- 响应头:
名称: 值\r\n- Date: 日期时间 响应发出的时间\r\n
- Content - Type:text/html ;服务器告诉浏览器自己能响应的对象的类型\r\n
- User - Agent:用户代理说白了就是当前使用的浏览器的类型名称和对应的版本信息\r\n
- content-Length: 300 响应体的字节长度\r\n
- 空行\r\n
- 响应体 html网页结构
- 重要的响应状态码
- 200 OK 一切正常
- 404 NotFound 请求的资源未找到
- 500 Server Internal Error服务器内部错误