今日知识
- jdk安装和配置
- java基础补充
- javaweb安装和配置
- maven安装和配置
- 框架简介
jdk安装和配置
JAVA_HOME=安装目录
PATH=;%JAVA_HOME%/bin;
目的:cmd窗口可以直接使用bin目录下面的可执行文件
ArrayList
含义
底层采用可变数组的线性列表
体系结构
特点
元素有序,可以重复
因为底层采用可变数组
存放的是对象的引用
案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public class ListDemo { public static void main(String[] args) { User zs = new User(); zs.setName("ZhanShan");
List<User> dataList = new ArrayList<>();
dataList.add(zs);
System.out.println(dataList.get(0));
zs.setName("LiSi"); System.out.println(dataList.get(0));
System.out.println(dataList.isEmpty());
System.out.println(dataList.contains(zs));
System.out.println(dataList.size()); } }
class User{ private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
@Override public String toString() { return name; } }
|
IO
含义
输入输出
使用场景
分类
案例一
输入流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| public class InputDemo { public static void main(String[] args){ String filePath = "src/main/java/com/neu/day01/_02IO/input.txt"; readDataByByte(filePath, 5); readDataByChar(filePath, 5); }
public static void readDataByByte(String path, int span) { InputStream inputStream = null; try { inputStream = new FileInputStream(path); byte[] cache = new byte[span]; StringBuilder stringBuilder = new StringBuilder(); int len = -1; while ((len = inputStream.read(cache)) != -1) { System.out.println(len); String temp = new String(cache, 0, len); System.out.println(temp); stringBuilder.append(temp); } System.out.println(stringBuilder); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public static void readDataByChar(String path, int span) { Reader reader = null; try { reader = new FileReader(path); char[] cache = new char[span]; StringBuilder stringBuilder = new StringBuilder(); int len = -1; while ((len = reader.read(cache)) != -1) { System.out.println(len); String temp = new String(cache, 0, len); System.out.println(temp); stringBuilder.append(temp); } System.out.println(stringBuilder); } catch (IOException e){ e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|
案例二
输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| public class OutputDemo { public static void main(String[] args) { String content = "print(\"Hello World\")"; String path = "src/main/java/com/neu/day01/_02IO/output.txt";
writeDataByByte(path, content); writeDataByChar(path, content); }
public static void writeDataByByte(String path, String content) { OutputStream outputStream = null; try { outputStream = new FileOutputStream(path); outputStream.write(content.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (outputStream != null) { try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void writeDataByChar(String path, String content) { Writer wirter = null; try { wirter = new FileWriter(path); wirter.write(content); } catch (IOException e) { e.printStackTrace(); } finally { if (wirter != null) { try { wirter.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|
File
含义
案例一
File入门
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class FileDemo { public static void main(String[] args) { File file = new File("src/main/java/com/neu/day01/_02IO/Dir"); dfs(file, 0, ".txt"); }
public static void dfs(File dir, int i, String post) { File[] children = dir.listFiles(); if (children != null) { for (File child : children) { if (child.isFile() && !child.getName().endsWith(post)) continue; String head = " "; System.out.println(head.repeat(i) + child.getName()); if (child.isDirectory()) { dfs(child, i+1, post); } } } } }
|
缓冲流
示意图

案例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| public class BufferDemo {
public static void main(String[] args) { String path = "src/main/java/com/neu/day01/_02IO/buffer.txt"; writerDataByCharToBuffer(path, "hello world"); readDataByCharFromBuffer(path); }
public static void readDataByCharFromBuffer(String path) { Reader reader = null; BufferedReader bufferedReader = null; try { reader = new FileReader(path); bufferedReader = new BufferedReader(reader); String s = ""; StringBuilder stringBuilder = new StringBuilder(); while ((s = bufferedReader.readLine()) != null) { stringBuilder.append(s); } System.out.println(stringBuilder); } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public static void writerDataByCharToBuffer(String path, String content){ Writer writer = null; BufferedWriter bufferedWriter = null; try { writer = new FileWriter(path); bufferedWriter = new BufferedWriter(writer); bufferedWriter.write( content); } catch (IOException e){ e.printStackTrace(); } finally { if (bufferedWriter != null) { try { bufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|
代码统计
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| public class codeLineCount { public static void main(String[] args) { File dir = new File("src"); ArrayList<String> result = new ArrayList<String>(); int count = dfs(result, dir, 0, ".java"); for (int i=result.size()-1; i>=0; i--) { System.out.println(result.get(i)); } System.out.println("该目录下的Java文件代码总行数为: " + count); System.out.println("详细情况为上面的文件目录树"); }
public static int Count(File file) { String path = file.getPath(); int count = 0; Reader reader = null; BufferedReader bufferedReader = null; try { reader = new FileReader(path); bufferedReader = new BufferedReader(reader); String temp = ""; while ((temp = bufferedReader.readLine()) != null) { if (!"".equals(temp.trim())) count += 1; } } catch (IOException e) { e.printStackTrace(); } finally { if (bufferedReader != null) { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } return count; }
public static int dfs(ArrayList<String> result, File dir, int i, String suffix) { int count = 0; String head = " "; File[] children = dir.listFiles(); if (children == null) { result.add("0"); result.add(dir.getName()); return 0; } for (File child : children) { if (child.isFile()) { if (child.getName().endsWith(suffix)) { int temp = Count(child); result.add(head.repeat(i+1) + temp); result.add(head.repeat(i+1) + child.getName()); count += temp; } } else { int temp = dfs(result, child, i+1, suffix); count += temp; } } result.add(head.repeat(i) + count); result.add(head.repeat(i) + dir.getName()); return count; } }
|
网络编程
服务端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| public class Server { public static void main(String[] args) { ServerSocket serverSocket = null; Socket socket = null; try { serverSocket = new ServerSocket(8080); System.out.println("等待客户端连接"); socket = serverSocket.accept();
System.out.println("有客户端连接");
InputStream inputStream = socket.getInputStream(); byte[] cache = new byte[1024]; int len = -1; StringBuilder requestData = new StringBuilder(); while ((len = inputStream.read(cache)) != -1) { requestData.append(new String(cache, 0, len)); } System.out.println("请求数据为: " + requestData);
OutputStream outputStream = socket.getOutputStream(); outputStream.write(requestData.toString().getBytes()); socket.shutdownOutput(); } catch (IOException e) { e.printStackTrace(); } finally { if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|
客户端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
| public class Client { public static void main(String[] args) { Socket socket = null; try { socket = new Socket("localhost", 8080);
String requestData = "hello world"; OutputStream outputStream = socket.getOutputStream(); outputStream.write(requestData.getBytes()); socket.shutdownOutput();
InputStream inputStream = socket.getInputStream(); int len = -1; byte[] cache = new byte[1024]; StringBuilder responseData = new StringBuilder(); while ((len=inputStream.read(cache)) != -1) { responseData.append(new String(cache, 0, len)); } System.out.println("应答数据为: " + responseData);
} catch (IOException e) { e.printStackTrace(); } finally { if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
|
Tomcat安装和配置
Tomcat下载
点击链接进入官网下载最新的版本即可(7、8、9、10)
官网链接
Tomcat安装
将下载好的压缩包解压至特定路径后,配置环境
- 配置:
CATALINA_BASE = tomcat文件夹的路径(bin目录的前一级)
CATALINA_HOME = tomcat文件夹的路径(bin目录的前一级)
CATALINA_TMPDIR = tomcat文件夹下temp文件夹的路径(与bin目录同级)
注意事项:需要配置好JAVA_HOME
- 添加Path系统变量:
%CATALINA_HOME%\bin
- tomcat启动测试:
startup
maven安装和配置
Maven介绍
Maven是一个跨平台的项目管理工具。作为Apache组织的一个颇为成功的开源项目,其主要服务于基于Java平台的项目创建,依赖管理和项目信息管理。maven是Apache的顶级项目,解释为“专家,内行”,它是一个项目管理的工具,maven自身是纯java开发的,可以使用maven对java项目进行构建、依赖管理。
Maven作用
- 依赖管理
- 依赖指的就是是 我们项目中需要使用的第三方Jar包, 一个大一点的工程往往需要几十上百个Jar包,按照我们之前的方式,每使用一种Jar,就需要导入到工程中,还要解决各种Jar冲突的问题。
- Maven可以对Jar包进行统一的管理,包括快速引入Jar包,以及对使用的 Jar包进行统一的版本控制。
- 一键构建项目
- 之前我们创建项目,需要确定项目的目录结构,比如src 存放Java源码, resources存放配置文件,还要配置环境比如JDK的版本等等,如果有多个项目 那么就需要每次自己搞一套配置,十分麻烦。
- Maven为我们提供了一个标准化的Java项目结构,我们可以通过Maven快速创建一个标准的Java项目。
Maven下载
在官网中找到最新的链接下载即可
官网链接
阿里云镜像
Maven安装
解压下载的压缩包到没有中文和空格的路径后,配置环境变量
- 配置:
MAVEN_HOME = maven文件夹的路径(bin目录的前一级)
- 添加Path系统变量:
%MAVEN_HOME%\bin
- maven版本测试:
mvn-v
框架简介
TODO