一、IO
1. java.io.File
常用方法:
public boolean createNewFile() throws IOException:创建文件
public boolean mkdir():创建文件夹
public boolean mkdirs():可以同时创建路径中不存在的文件夹
public boolean renameTo(File dest):将文件重命名成dest指定的名字,并移到dest指定的路径下
public File[] listFiles():返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件
public boolean isFile():检测文件是否为普通文件。 如果文件不是目录,则是正常文件
public String getAbsolutePath():返回此抽象路径名的绝对路径名字符串
public boolean isDirectory():判断是否为目录
2. 文件遍历
案例代码:
public class Demo4 {
public static void main(String[] args) {
File e = new File("e://");
File[] files = e.listFiles();
listFile(files);
}
public static void listFile(File[] files){
if(files!=null&&files.length>0){
for (File file:files) {
if(file.isFile()){
if(file.getName().endsWith(".avi")){
if(file.length()>100*1024*1024)
System.out.println("找到了一个avi文件"+file.getAbsolutePath());
}
}else {
File[] files2 = file.listFiles();
listFile(files2);
}
}
}
}
}
3. 相对路径与绝对路径
绝对路径:从盘符开始,是一个完整的路径,例如:c://a.txt
相对路径:在Java代码中是相对于项目目录路径,这是一个不完整的便捷路径,在Java开发中很常用。例如:a.txt,则该文件就存在于项目所在的文件夹中
4. 流概述
IO流 可以把数据传输的操作看作是数据的流动,分为输入Input和输出Output
java的IO操作就是java.io包下的一些常用类的使用,通过这些常用类对数据进行读取和写出
IO流的分类 通过流的方向可以分为:输入流和输出流
按照流动的数据类型分为:字节流 字符流
字节流:输入流 InputStream
输出流 outputStream
字符流:输入流 Reader
输出流 Write
5. java.io.FileOutputStream
案例代码:
public static void main(String[] args) throws IOException {
//OutputStream
FileOutputStream fos = new FileOutputStream("c://a.txt");
/*byte[] bytes = {65,66,67,68,69};
fos.write(bytes);*/
//byte[] bytes2 = {65,66,67,68,69};
byte[] bytes2 = "ABCDEF".getBytes();
fos.write(bytes2,2,2);
//fos.write(bytes2);
fos.close(); //写在哪在哪关闭
System.out.println("已经写出");
}
6. java.io.FileInputStream
案例代码:
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("c://a.txt");
/*while (true){
byte b = (byte) fis.read();
if(b==-1){
break;
}
System.out.println(b);
}*/
byte[] bytes = new byte[10];
int len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
len = fis.read(bytes);
System.out.println(new String(bytes,0,len));
fis.close();
}
7. 字符输出
代码案例:
public class Demo9 {
public static void main(String[] args) throws IOException {
//writer
FileWriter fw = new FileWriter("c://b.txt",true);
//fw.write('a');
fw.append("锄禾日当午").append(",").append("汗滴禾下土"); //可以一致追加
fw.write("锄禾日当午");
fw.flush(); //刷新
fw.close();
}
}
8. 字符读取
代码案例:
public class Demo10 {
public static void main(String[] args) throws IOException {
//reader
FileReader fr = new FileReader("b.txt");
while (true){
int c = fr.read();
if(c==-1){
break;
}
System.out.println((char)c);
}
char[] chars = new char[100];
//fr.read(chars);
System.out.println(chars[0]);
fr.close();
}
}
9. 字节转换字符流
案例代码:
public class Demo11 {
public static void main(String[] args) throws IOException {
//转换流 :将字节流转换成字符流 使用了装饰者模式
FileInputStream fis = new FileInputStream("c://a.txt");
//将字节输入流转换为字符输入流 参数为要转换的字节流
InputStreamReader isr = new InputStreamReader(fis,"gbk");
while (true){
int c = isr.read();
if(c==-1){
break;
}
System.out.println((char) c);
}
}
}
10. Print与BufferedReader
Print案例代码:
public class Demo12 {
public static void main(String[] args) throws FileNotFoundException {
//收集异常信息
try {
String s = null;
s.toString();
}catch (Exception e){
PrintWriter pw = new PrintWriter("c://bug.txt");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
pw.print(format.format(new Date()));
e.printStackTrace(pw);
pw.close();
}
}
}
缓存读取流,将字符输入流转换为带有缓存,可以一次读取一行的缓存字符读取流
案例代码:
FileReader fw=new FileReader("c://c.txt");
BufferedReader br=new BufferedReader(fw);
String text=br.readLine();
System.out.println(text);
11. properties
案例代码:
public class Demo13 {
public static void main(String[] args) throws IOException {
//properties文件与properties类
/*Properties pt = new Properties();
//键=值
pt.put("name","金苹果");
pt.put("info","讲述历了金苹果种植的过程");
FileWriter fw = new FileWriter("c://book.properties");
pt.store(fw,"存储的图书");
fw.close();*/
Properties pt = new Properties();
Reader fw = new FileReader("c://book.properties");
pt.load(fw);
System.out.println(pt.get("name"));
System.out.println(pt.get("info"));
}
}
public void store(Writer writer, String comments) throws IOException:将此Properties
表中的此属性列表(键和元素对)以适合使用load(Reader)方法的格式写入输出字符流。
public void load(Reader reader) throws IOException:以简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。
12. 序列化技术
案例代码:
要实现Java对象的序列化,只要将类实现标识接口——Serializable接口即可,不需要我们重写任何方 法就可以实现序列化。
序列化案例:
反序列化案例:
将实体类中不想序列化的属性添加transient修饰词,案例展示如下:
static修饰符修饰的属性也不会参与序列化和反序列化,案例展示如下:
我们将实体类中的stuName属性添加了static修饰词,并在对象序列化后执行如下代码:
最终代码的运行结果为:
13. try-with-resources
案例代码:
public class Demo15 {
public static void main(String[] args) throws FileNotFoundException {
//try-with-resources
/*try {
FileReader fr = new FileReader("c://book.txt");
int c = fr.read();
System.out.println((char)c);
fr.close();
} catch (IOException e) {
e.printStackTrace();
}*/
//jdk9
FileReader fr = new FileReader("c://book.txt");
PrintWriter pw = new PrintWriter("c://book.txt");
try(fr;pw){
int c = fr.read();
System.out.println((char)c);
}catch (IOException e) {
e.printStackTrace();
}
}
}
14. Externalizable实现Java序列化
Externalizable继承自Serializable,使用Externalizable接口需要实现readExternal方法和 writeExternal方法来实现序列化和反序列化。
案例代码:
测试结果如下:
Externalizable接口继承了Serializable接口,所以实现Externalizable接口也能实现序列化和反序列 化。 Externalizable接口中定义了writeExternal和readExternal两个抽象方法,这两个方法其实对应 Serializable接口的writeObject和readObject方法。可以这样理解:Externalizable接口被设计出来的 目的就是为了抽象出writeObject和readObject这两个方法,但是目前这个接口使用的并不多。
Externalizable与Serializable的区别如下:
二、 多线程
因为多线程在之前已经单独整理过了,所以这里就不在整理了,就提供一个博客链接:
报名学习开课吧Java商业项目实战就业班_一个正在努力的好孩子的博客-优快云博客
三、网络编程
1. 网络编程概述
注意:IPv4协议的地址为32位,所以它可以提供的地址数为:2^32-1个(大约43亿个)。IPv6能够提供的地址数为:2^128-1个
网络编程程序可分为:.B/S 程序 : 浏览器与服务器程序和C/S 程序 : 客户端与服务器程序
2. TCP程序
三次握手的过程:
第一次握手:建立连接时,客户端发送syn包(seq=j)到服务器,并进入SYN_SENT状态,等待服务器确认;SYN:同步序列编号(Synchronize Sequence Numbers)。
第二次握手:服务器收到syn包,必须确认客户的SYN(ack=j+1),同时自己也发送一个SYN包(seq=k),即SYN+ACK包,此时服务器进入SYN_RECV状态。
第三次握手:客户端收到服务器的SYN+ACK包,向服务器发送确认包ACK(ack=k+1),此包发送完毕,客户端和服务器进入ESTABLISHED(TCP连接成功)状态,完成三次握手。
四次挥手的过程:
(1) TCP客户端发送一个FIN,用来关闭客户到服务器的数据传送。
(2) 服务器收到这个FIN,它发回一个ACK,确认序号为收到的序号加1。和SYN一样,一个FIN将占用一个序号。
(3) 服务器关闭客户端的连接,发送一个FIN给客户端。
(4) 客户端发回ACK报文确认,并将确认序号设置为收到序号加1。
案例程序:
客户端:
服务器:
3. UDP 协议(数据报)
用户数据报协议, 与tcp协议不同, UDP的连接是不可信的. 数据发送的成功与失败 与 数据报是无关的.
4. 在服务器中加入多线程
服务器端部分代码展示:
public static void main(String[] args) throws IOException { ServerSocket server = new ServerSocket(55565); System.out.println("服务器启动完毕"); while (true) { Socket socket = server.accept(); new Thread() { @Override public void run() { InputStream inputStream = null; String username = null; String password = null; PrintStream printStream = null; OutputStream outputStream = null; try { inputStream = socket.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); username = bufferedReader.readLine(); password = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); } if (username.equals("admin") && password.equals("abc")) { try { outputStream = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } printStream = new PrintStream(outputStream); printStream.println("管理员登录成功!"); } else if (username.equals("user") && password.equals("123")) { try { outputStream = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } printStream = new PrintStream(outputStream); printStream.println("普通用户登录成功!"); } else { try { outputStream = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } printStream = new PrintStream(outputStream); printStream.println("用户名或密码不正确"); } } }.start(); System.out.println("一个客户端连接了"); } }