学习目标:
- java学习第一周
学习内容:
- 容器
- Io流
- 多线程
- 网络编程
容器
Io流
1.核心代码try-with-recource结构
2.IO流体系
InputStream
OutPutStream
Reader
Writer
RandomAcessFile(没学)
Test1
package com.boots.ioDemo;
import java.io.FileInputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
FileInputStream fil =null;
try{
fil = new FileInputStream("D:\\a.txt");
int temp=0;
StringBuilder sb=new StringBuilder();
while ((temp=fil.read())!=-1)
{
sb.append((char)temp);
}
System.out.println(sb);
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
if (fil!=null)
{
try {
fil.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Test2
实现了阿帕奇下的IO包
package com.boots.ioDemo;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.FileFilter;
public class Test05 {
public static void main(String[] args) throws Exception{
FileUtils.copyDirectory(new File("D:\\API"),new File("d:/bbb"),
new FileFilter() {
@Override
public boolean accept(File pathname) {
return false;
}
}
);
}
}
多线程
两种实现方式:
1.继承Thread类
2.实现Runnable接口
Test(重要,生产者消费者模式)
在这里插入代码片/**
* 定义馒头类
*/
class ManTou{
private int id;
public ManTou(int id){
this.id = id;
}
public int getId(){
return this.id;
}
}
/**
* 定义缓冲区类
*/
class SynArea{
//定义存放面包的盒子
private ManTou[] mt = new ManTou[10];
//定义操作商店的索引
private int index;
/**
* 放馒头
*/
public synchronized void push(ManTou manTou){
//判断商店是否已满
while(this.index == this.mt.length){
try {
/**
* 语法:wait(),该方法必须要在synchronized块中调用。
* wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
* 其他需要该对象锁的线程就可以继续运行了。
*/
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//唤醒取面包的线程
/**
* 语法:该方法必须要在synchronized块中调用。
* 该方法会唤醒处于等待状态队列中的一个线程。
*/
this.notify();
this.mt[this.index] = manTou;
this.index++;
}
/**
* 取馒头
*/
public synchronized ManTou pop(){
while(this.index == 0){
try {
/**
* 语法:wait(),该方法必须要在synchronized块中调用。
* wait执行后,线程会将持有的对象锁释放,并进入阻塞状态,
* 其他需要该对象锁的线程就可以继续运行了。
*/
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notify();
this.index--;
return this.mt[this.index];
}
}
/**
* 定义生产者线程类
*/
class ShengChan extends Thread{
private SynArea ss;
public ShengChan(SynArea ss){
this.ss = ss;
}
@Override
public void run() {
for(int i=0;i<10;i++){
System.out.println("生产面包:"+i);
ManTou manTou = new ManTou(i);
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.ss.push(manTou);
}
}
}
/**
* 定义消费者线程类
*/
class XiaoFei extends Thread{
private SynArea ss;
public XiaoFei(SynArea ss){
this.ss = ss;
}
@Override
public void run() {
for(int i=0;i<10;i++){
ManTou manTou = this.ss.pop();
System.out.println("消费面包:"+i);
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ProduceThread {
public static void main(String[] args) {
SynArea ss = new SynArea();
new XiaoFei(ss).start();
new ShengChan(ss).start();
}
}
网络编程
TCP模式的单向通信,双向通信
实现点对点聊天
下面是单向通信为例的代码(包含服务端和客户端)
package com.boots.JavaWeb;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.security.spec.ECField;
import java.util.Scanner;
public class OneWaySocketClient {
public static void main(String[] args) {
try(
Socket socket =new Socket("127.0.0.1",8888);
Scanner scanner=new Scanner(System.in);
PrintWriter pw =new PrintWriter(socket.getOutputStream());
BufferedReader bf=new BufferedReader(new InputStreamReader(socket.getInputStream()));
) {
System.out.println("连接成功!");
while (true)
{
String str = scanner.nextLine();
pw.println(str);
pw.flush();
String s = bf.readLine();
System.out.println(""+s);
}
package com.boots.JavaWeb;
import org.w3c.dom.ls.LSOutput;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class OneWaySocketServe {
public static void main(String[] args) {
System.out.println("服务器等待连接……");
try(
ServerSocket ss=new ServerSocket(8888);
Socket socket=ss.accept();
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter pw=new PrintWriter(socket.getOutputStream());
)
{
System.out.println("连接成功!");
while (true){
String str=br.readLine();
System.out.println("客户端说:"+str);
pw.println("服务器回应:"+str);
pw.flush();
}
}catch (Exception e){
e.printStackTrace();
System.out.println("服务器丢失……");
}
}
}
学习时间:
- 七月10日到七月17日
学习产出:
提示:这里统计学习计划的总量
例如:
- 视频5*5小时
- 代码编写1*5小时