文章目录
1 IO流
1.1 文件操作
- 创建文件
public File(String pathname);
public File(String parent, String child);
public class File_01 {
public static void main(String[] args) {
File file = new File("./file.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 操作
public String getName();
public String getAbsolutePath();
public String getParent();
public boolean isDirectory();
public boolean exists();
1.2 (字节流:二进制)输入流InputStream
- FileInputStream
- BufferedInputStream
- ObjectInputStream
public FileInputStream(File file);
public FileInputStream(FileDescriptor fdObj);
public FileInputStream(String name);
读取文件
public class File_01 {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
int readData = -1;
try {
fileInputStream = new FileInputStream("./file.txt");
while((readData = fileInputStream.read()) != -1){
// 转为char进行显示
System.out.print((char)readData);
}
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.3 (字符流:文本)输入流Reader
- FileReader
- BufferedReader
- InputStreamReader
1.4 输出流OutputStream
- FileOutputStream
- BufferedOutputStream
- ObjectOutputStream
1.5 输出流Writer
- FileWriter
- BufferedWriter
- InputStreamWriter
1.6 properties
- mysql.properties
ip=192.168.137.234
user=root
password=123456
- 读取,修改
package cn.xhh.fileio;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.26
*/
public class properties_02 {
public static void main(String[] args) throws IOException {
Properties pro = new Properties();
pro.load(new FileReader("./mysql.properties"));
// 显示
Set<Object> keys = pro.keySet();
for (Object obkey :keys) {
String key = (String)obkey;
System.out.println("key:" + key + " value:" + pro.getProperty(key));
}
// 修改
pro.setProperty("user", "xhh");
pro.setProperty("newKey", "mcy");
// 保存
pro.store(new FileWriter("./mysql.properties"), null);
}
}
2 网络编程
2.1 InetAddress
package cn.xhh.net;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.27
*/
public class Net_01 {
public static void main(String[] args) throws UnknownHostException {
// [1] 获取本机IP
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost); // LAPTOP-TDI06HTS/192.168.137.1
// [2] 根据主机名获取IP
InetAddress host1 = InetAddress.getByName("LAPTOP-TDI06HTS");
System.out.println(host1); // LAPTOP-TDI06HTS/192.168.137.1
// [3] 根据域名获取
InetAddress host2 = InetAddress.getByName("www.baidu.com");
System.out.println(host2); // www.baidu.com/112.80.248.75
// [4] host IP
String hostAddress = host1.getHostAddress();
System.out.println(hostAddress); // 192.168.137.1
}
}
2.2 Socket
2.3 ServerSocket -TCP (CS:字节流)
Server
package cn.xhh.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.27
*/
public class Server {
public static void main(String[] args) throws IOException {
// [1] 本机IP 监听8888端口 (fd bind listen) ServerSocket可以连接多个客户端 产生多个accept
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server listen prot: 8888");
// [2] 监听 等待连接
Socket accept = serverSocket.accept();
System.out.println("Server accept:" + accept.getClass());
// [3] 接收数据
InputStream inputStream = accept.getInputStream();
OutputStream outputStream = accept.getOutputStream();
// method1:读取
// int read = -1;
// while ((read = inputStream.read()) != -1){
// System.out.print((char)read);
// }
// method2:读取
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(buf)) != -1){
System.out.print(new String(buf, 0, readLen));
}
// 数据发送回去
outputStream.write("Hello Client info ..".getBytes());
accept.shutdownOutput();
// [4] 关闭
inputStream.close();
outputStream.close();
accept.close();
}
}
Client
package cn.xhh.net;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.27
*/
public class Client {
public static void main(String[] args) throws IOException {
// [1] 连接 8888 IP:prot
Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 8888);
System.out.println("Client accept:" + socket.getClass());
// [2] 通过流进行传输
OutputStream outputStream = socket.getOutputStream();
outputStream.write("hello xhh to Server".getBytes());
// *** 设置关闭写入标记
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
// 读取信息
byte[] buf = new byte[1024];
int readLen = 0;
while ((readLen = inputStream.read(buf)) != -1){
System.out.print(new String(buf, 0, readLen));
}
// [4] 关闭流和socket
outputStream.close();
inputStream.close();
socket.close();
}
}
2.4 ServerSocket -TCP (CS:字符流API更多)
Server
package cn.xhh.net;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.27
*/
public class v2_Server {
public static void main(String[] args) throws IOException {
// [1] 本机IP 监听8888端口 (fd bind listen) ServerSocket可以连接多个客户端 产生多个accept
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server listen prot: 8888");
// [2] 监听 等待连接
Socket accept = serverSocket.accept();
System.out.println("Server accept:" + accept.getClass());
// [3] 接收数据
InputStream inputStream = accept.getInputStream();
// 转为字符流
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s); // 接收的数据
OutputStream outputStream = accept.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
// 数据发送回去
bufferedWriter.write("Hello Client info ..");
bufferedWriter.newLine();
bufferedWriter.flush();
// [4] 关闭
bufferedWriter.close();
bufferedReader.close();
accept.close();
}
}
Client
package cn.xhh.net;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.27
*/
public class v2_Client {
public static void main(String[] args) throws IOException {
// [1] 连接 8888 IP:prot
Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 8888);
System.out.println("Client accept:" + socket.getClass());
// [2] 通过流进行传输
OutputStream outputStream = socket.getOutputStream();
// 字节流通过Writer转化为字符流
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("Hello, server ···· @xhh"); // 利用字符流
bufferedWriter.newLine(); // 插入换行符,表示写入结束(也是结束符) server: readLine()
bufferedWriter.flush(); // 必须手动刷新,不然不会写入
// [3] 接收Server 消息
InputStream inputStream = socket.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String s = bufferedReader.readLine();
System.out.println(s);
// [4] 关闭流和socket 关闭外层的流
bufferedReader.close();
bufferedWriter.close();
socket.close();
}
}
2.5 上传文件
客户端发送图片
服务端接收并保存(回复收到)
StreamUtils
package cn.xhh.uploadFile;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 此类用于演示关于流的读写方法
*
*/
public class StreamUtils {
/**
* 功能:将输入流转换成byte[]
* @param is
* @return
* @throws Exception
*/
public static byte[] streamToByteArray(InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();//创建输出流对象
byte[] b = new byte[1024];
int len;
while((len=is.read(b))!=-1){
bos.write(b, 0, len);
}
byte[] array = bos.toByteArray();
bos.close();
return array;
}
/**
* 功能:将InputStream转换成String
* @param is
* @return
* @throws Exception
*/
public static String streamToString(InputStream is) throws Exception{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder builder= new StringBuilder();
String line;
while((line=reader.readLine())!=null){ //当读取到 null时,就表示结束
builder.append(line+"\r\n");
}
return builder.toString();
}
}
Server
package cn.xhh.uploadFile;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.27
*/
public class FileUploadServer {
public static void main(String[] args) throws Exception {
// 1. 服务端 本机
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server 等待链接 ing....");
// 2. 等待链接
Socket socket = serverSocket.accept();
// 3. 读取客户端发送的数据
BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
byte[] bytes = StreamUtils.streamToByteArray(bis);
// 4. 写入到指定的路径
String filePath = "./data/vim_copy1.png";
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
bos.write(bytes);
// ::得到消息回复客户端已经收到消息
OutputStream socOps = socket.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socOps));
bw.write("Recv img OK");
bw.newLine();
bw.flush();
// 5. 关闭流
bw.close();
bos.close();
bis.close();
socket.close();
}
}
Client
package cn.xhh.uploadFile;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* @author : xhh
* @email : xhh0608@foxmail.com
* @date : 2021.9.27
*/
public class FileUploadClient {
public static void main(String[] args) throws Exception {
// 1.客户端链接
Socket socket = new Socket(InetAddress.getLocalHost().getHostAddress(), 8888);
// 2.创建从磁盘的读入流 字节流
String filePath = "./data/vim.png";
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(filePath));
// 读入到字节数组中
byte[] bytes = StreamUtils.streamToByteArray(bufferedInputStream); // 文件内容
// 3.通过网络传输为服务器
BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
bos.write(bytes);
socket.shutdownOutput(); // 设置结束标记
// ::接收传输成功的消息
InputStream socIps = socket.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(socIps));
String s = br.readLine();
System.out.println(s);
// 4. 关闭通道
br.close();
bos.close();
bufferedInputStream.close();
socket.close();
}
}
3 反射
3.1 反射机制入门
开闭原则:不改变源码,扩展功能
3.2 Class对象