java高级总结
1.file的常用用法
// 文件的读取和写入
File file = new File("D:\\zzz\\2.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));//true在文件的末尾写入,默认为false,清除在加入
writer.write("hello00");
writer.newLine();
writer.flush();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
2.如何实现序列化?不想让某个字段进行序列化处理该如何实现?
关于序列化,常又称为持久化,将其写入磁盘中。
进而对于编码规则来说:
任一一个实体类必须要去实现 Serializable 接口,方便以后将该类持久化,或者将其用于转为字节数组,用于网络传输。
对于一个实体类,不想将所有的属性都进行序列化,有专门的关键字 transient:
3.io流的分类
按流的方向分为:输入流和输出流。按流的数据单位不同分为:字节流和字符流。按流的功能不同分为:节点流和处理流。
File f = new File("a.txt");
FileOutputStream fop = new FileOutputStream(f);
// 构建FileOutputStream对象,文件不存在会自动新建
OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
// 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
writer.append("中文输入");
// 写入到缓冲区
writer.append("\r\n");
// 换行
writer.append("English");
// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
writer.close();
// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
fop.close();
// 关闭输出流,释放系统资源
FileInputStream fip = new FileInputStream(f);
// 构建FileInputStream对象
InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
// 构建InputStreamReader对象,编码与写入相同
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
// 转成char加到StringBuffer对象中
}
System.out.println(sb.toString());
reader.close();
// 关闭读取流
fip.close();
// 关闭输入流,释放系统资源
4.了解线程的状态有几种
5种,新建、就绪、运行、阻塞、死亡
创建线程的两种方式
继承Thread
实现Runnable
public class runnableDemo extends Thread {
private Thread t;
private String threadName;
public runnableDemo(String threadName, Thread th) {
this.threadName = threadName;
System.out.println("create: "+threadName);
}
static int tick=20;
static Object ob="aa";//创建一个静态钥匙
@Override
public void run() {
System.out.println("run"+threadName);
while (tick >=0){
synchronized (ob){
if(tick>0){
System.out.println(threadName+"卖出了"+tick+"张票");
tick--;
} else {
System.out.println(threadName+"票卖完了");
break;
}
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Thread th=new Thread();
runnableDemo r1=new runnableDemo("窗口1",th);
runnableDemo r2=new runnableDemo("窗口2",th);
r1.start();
r2.start();
}
6.线程的常用方式
Thread th=new Thread();
Thread.start:开始线程
th.join():等待该线程终止的时间最长为
interrupt:中断线程
Thread.yield():暂停当前正在执行的线程对象,并执行其他线程。
sleep():休眠
wait:wait()属于object类,会释放当前线程,进入等待此对象的等待索定池。比方说,线程A调用Obj.wait(),线程A就会停止运行,而转为等待状态。
wait()方法与notify()必须要与synchronized(resource)一起使用
notify():用某个对象的notify()方法能够唤醒一个正在等待这个对象的monitor(即锁,或者管程),如果有多个,只能唤醒其中的一个。
notifyAll():调用notifyAll()方法能够唤醒所有正在等待这个对象的monitor的线程
7.用什么方法实现线程的通信
synchronized同步
while轮询
wait/notify机制
管道通信
8.了解tcp,upd的网络编程方式
TCP:TCP 是传输控制协议的缩写,它保障了两个应用程序之间的可靠通信。通常用于互联网协议,被称 TCP / IP。
UDP:UDP 是用户数据报协议的缩写,一个无连接的协议。提供了应用程序之间要发送的数据的数据包。
区别:udp与tcp的主要区别在于udp不一定提供可靠的数据传输,该协议不能保证数据准确无误地到达目的地。
多人聊天
package com.dz.redis.service.text;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
public class client extends Thread {
static Socket socket = null;
Scanner input = new Scanner(System.in);
static String name=null;
public static void main(String[] args) {
int x=(int)(Math.random()*100);
client.name="client"+x;
System.out.println("************客户端"+x+"*************");
try {
socket = new Socket("127.0.0.1", 9999);
System.out.println("已经连上服务器了");
} catch (Exception e) {
e.printStackTrace();
}
client t = new client();
Read r = new Read(socket);
Thread print = new Thread(t);
Thread read = new Thread(r);
print.start();
read.start();
}
@Override
public void run() {
try {
Thread.sleep(1000);
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true) {
String msg = input.next();
out.println(name+"说:"+msg);
out.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.dz.lianxi.text;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class print extends Thread{
static List<Socket> socketList=new ArrayList<>();
Scanner sc=new Scanner(System.in);
public print(Socket socket) {
socketList.add(socket);
}
@Override
public void run() {
try {
Thread.sleep(1000);
while (true){
String str=sc.next();
for (int i = 0; i <socketList.size() ; i++) {
Socket socket = socketList.get(i);
PrintWriter out=new PrintWriter(socket.getOutputStream());
out.print("服务端说:"+sc);
out.flush();
}
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
}
}
package com.dz.lianxi.text;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
class Read implements Runnable {
static Socket socket = null;
public Read(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
Thread.sleep(1000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
while (true) {
System.out.println( in.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.dz.lianxi.text;
import javafx.print.Printer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class service extends Thread{
private String threadName;
private Thread thread;
static List<Socket> socketList=new ArrayList<Socket>();
static Socket socket = null;
static ServerSocket serverSocket = null;
public service() {
try {
serverSocket =new ServerSocket(9999);
} catch (IOException e) {
e.printStackTrace();
}
}
public service(String threadName, Thread thread) {
this.threadName="服务端";
this.thread = thread;
try {
serverSocket =new ServerSocket(9999);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("服务端");
Thread thread=new Thread();
service service=new service();
int count=0;
while (true){
System.out.println("端口9999等待连接");
try {
socket=serverSocket.accept();
count++;
System.out.println("已经有几个用户连接:"+count);
socketList.add(socket);
} catch (IOException e) {
e.printStackTrace();
}
print print=new print(socket);
Thread read=new Thread();
Thread write=new Thread();
read.start();
write.start();
}
}
@Override
public void run() {
// 重写run方法
try {
Thread.sleep(1000);
BufferedReader in = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
while (true) {
String jieshou = in.readLine();
System.out.println( jieshou);
for (int i = 0; i < socketList.size(); i++) {
Socket socket=socketList.get(i);
PrintWriter out = new PrintWriter(socket.getOutputStream());
if (socket!=this.socket) {
out.println(jieshou);
}else{
out.println("(你)"+jieshou);
}
out.flush();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
9.如何设置线程的优先级
线程的优先级分为1-10,优先级越高,数量越大,当然了,java默认的优先级是5
改变线程的优先级:setPriority()方法
r1.getPriority()默认的优先级为5
10,如何实现线程的同步控制
实现同步的三种方法:
1.)使用同步代码块
2.)使用同步方法
3.)使用互斥锁ReetrantLock(更灵活的代码控制)
11.什么是注释?什么是注解?
首先来说注释有三种:// /* / /* */ 前两bai种编du译器直接跳过,从来不阅读,第三种编译器是zhi可以看懂的,
注解:注解(Annotation),也叫元数据。是标记,一种代码级别的说明。也可以理解成是一种应用在类、方法、参数、属性、构造器上的特殊修饰符。
编程题
文件的操作
File file = new File("D:\\zzz\\2.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(file,true));//true在文件的末尾写入,默认为false,清除在加入
writer.write("hello00");
writer.newLine();
writer.flush();
BufferedReader reader = new BufferedReader(new FileReader(file));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
筛选指定文件
public class NameFilter implements FilenameFilter {
private String extent;
public NameFilter(String extent) {
this.extent = extent;
}
@Override
public boolean accept(File dir, String name) {
return name.endsWith(extent);
}
}
public static void main(String[] args) {
File file=new File("D:\\zzz");
System.out.println(file.getAbsolutePath());//输出完整路径
NameFilter filter=new NameFilter("txt");//帅选为txt文件
File[] files=file.listFiles(filter);
if(files!=null){
for (Object oba:files
) {
if(oba!=null){
System.out.println(oba);
}
}
}
线程同步控制
1.)同步方法
2.)同步代码块
3.)使用特殊域变量(volatile)实现线程同步
4.)加上lock锁
package com.dz.lianxi.text;
public class bank {
static int count=100;
public void addMoney(int money){
synchronized ("ok"){
count=count+money;
System.out.println("存储后的钱:"+count);
}
}
public synchronized void deletaMoney(int money){
if(count-money<0){
System.out.println("余额不足");
return;
}
count-=money;
System.out.println("取钱后的余额:"+count);
}
public void queryMoney(){
System.out.println("当前的余额为:"+count);
}
}
public static void main(String[] args) {
bank bk = new bank();
Thread th = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
bk.addMoney(10);
bk.deletaMoney(20);
bk.queryMoney();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread th1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
bk.addMoney(15);
bk.deletaMoney(20);
bk.queryMoney();
}
}
});
th.start();
th1.start();
}
网络编程
看第八题