<5>第五期

news/2023/12/2 11:15:37

七、AVA反射

1.-反射的基本概念

一般情况下,我们知道一个类,那可以通过这个类创建对象;

但是如果要求通过一个对象找到一个类,这时候反射就派上用场了。

java反射实现的核心就是Class类 java.lang包下的。

2.java反射-java Class基本使用
// 这里对象.getClass() 调用的是Object类的getClass()
//得到Class对象 然后再调用Class里的getName()方法,获取到完整包路径类;
public class Test01 {
public static void main(String[] args) {Student stu=new Student();System.out.println( stu.getClass().getName());
}
}
// 
public class Student {private String name;private Integer age;@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}public Student(String name, Integer age) {super();this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}	
}
//  
public class Test02 {public static void main(String[] args) {//通过完整包路径类型来实例化Class对象:try {//此时需要用到泛型  光标放到对应class位置上,就可以看到所需的泛型一般通用符  ?Class<?> c=Class.forName("com.java1234_7.Student");System.out.println(c.getName()); } catch (ClassNotFoundException e) {e.printStackTrace();}}}
//  
public class Test03 {public static void main(String[] args) {Class<?> c=null;try {//此时需要用到泛型  光标放到对应class位置上,就可以看到所需的泛型一般通用符  ?c=Class.forName("com.java1234_7.Student");System.out.println(c.getName()); 	} catch (ClassNotFoundException e) {e.printStackTrace();}// 因为返接收回的泛型,可以直接用Student/*** 此时访问的是Student的无参 */Student s=null;try {c.newInstance();} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}s.setAge(23);s.setName("sss");System.out.println(s);}
}
//
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Test04 {public static void main(String[] args) {Class<?> c=null;try {c=Class.forName("com.java1234_7.Student");  System.out.println(c.getName());} catch (ClassNotFoundException e) {e.printStackTrace();}Student s=null;Constructor<?>[] cons=c.getConstructors();try {s=(Student) cons[0].newInstance("四史",68);} catch (Exception e) {e.printStackTrace();}System.out.println(s);}
}
3.通过反射获取类的基本结构
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;public class Test05 {public static void main(String[] args) {Class<?> c=null;try {c=Class.forName("com.java1234_7.Student");  System.out.println(c.getName());} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("---------------------------------------------");//获取所有构造方法//1.通过getConstructors()方法Constructor<?>[] cons= c.getConstructors();//增强for循环for(Constructor<?> con :cons){System.out.println("构造方法:"+con);}System.out.println("--------------------------------------------");//2,通过getMethods()方法获取所有方法Method[]  mts= c.getMethods();for(Method m:mts){System.out.println(m);}System.out.println("--------------------------------------------");// 3,通过getDeclaredFields()方法获取所有属性Field[] fd=c.getDeclaredFields();for(Field f:fd){System.out.println(f);}}
}
4.通过反射调用方法和操作属性

1,通过反射调用方法,主要通过invoke方法

package com.java1234_7;import java.lang.reflect.Method;public class Test06 {public static void main(String[] args) {Class<?> c=null;try {c=Class.forName("com.java1234_7.Student");  //反射获取类所实现的所有接口用到c=Class.forName("com.java1234_7.Student2");  System.out.println(c.getName());} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("---------------------------------------------");try {// 获取单个方法实现调用  getClass().getMethod()Object obj=c.newInstance();Method m2=obj.getClass().getMethod("setName", String.class);m2.invoke(obj, "小锋");Method m=obj.getClass().getMethod("getName");String name=(String) m.invoke(obj);System.out.println("name="+name);} catch (Exception e) {e.printStackTrace();}}
}

2,通过反射操作属性,java里反射可以操作私有属性,只需要设置下


import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Test07 {public static void main(String[] args) {Class<?> c=null;try {//反射获取类所实现的所有接口用到c=Class.forName("com.java1234_7.Student2");  System.out.println(c.getName());} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println("---------------------------------------------");try {Object obj=c.newInstance();Field f1= c.getDeclaredField("name");//反射提供的方法,可以去访问私有属性f1.setAccessible(true);f1.set(obj, "协商");System.out.println(f1.get(obj));} catch (Exception e) {e.printStackTrace();}}
}

八、JAVA集合

1.集合的引入

方法接口祖先级别:Collection

在对数据进行操作时候,数组也可以,但是存在一些问题,数组必须先定义好长度,也就是数组的长度是固定,不能根据我们的需求自动变长或者变短。

假如我们定义一个长度为3的数组,此时已经存储了3个,再想加一个时候就比较麻烦,用集合就可以解决好这个问题。

2.List集合

Collection接口是集合的老祖宗

List接口的主要实现类有ArrayList,和LinkedList。

上才艺:具体那个方法的使用用api工具查询

ArrayList

import java.util.ArrayList;
public class TestArrayList {public static void print(ArrayList<String> arraylist){System.out.println("当前输出的元素:");for(int i=0;i<arraylist.size();i++){System.out.print(arraylist.get(i)+" ");}System.out.println();}public static void main(String[] args) {ArrayList<String> arraylist=new ArrayList<String>();//对这个集合的增删改查//增arraylist.add("张1");arraylist.add("张2");arraylist.add("张3");arraylist.add("张4");arraylist.add("张5");print(arraylist);// 删除arraylist.remove(0);print(arraylist);//改arraylist.set(2, "测试");print(arraylist);//查System.out.println(arraylist.get(3));}
}

LinkedList:

import java.util.LinkedList;
public class TestLinkedList {public static void print(LinkedList<String> linkedlist){System.out.println("输出的集合是:");for(int i=0;i<linkedlist.size();i++){System.out.print(linkedlist.get(i)+" ");}System.out.println();}public static void main(String[] args) {LinkedList<String> linkedlist=new LinkedList<String>();linkedlist.add("张三");linkedlist.add("李四");linkedlist.add("王五");linkedlist.add("李四");linkedlist.add("赵六");print(linkedlist);System.out.println("-------------对数据进行操作---------------");// 返回集合的长度System.out.println(linkedlist.size());//向集合索引为2位置中插入一个值linkedlist.add(2, "测试");print(linkedlist);//从此列表中移除第一次出现的指定元素linkedlist.remove("李四");print(linkedlist);//indexOf 寻找位置System.out.println(linkedlist.indexOf("李四"));//peekFirst 获取第一个元素System.out.println(linkedlist.peekFirst());//peekFirst 获取最后的元素System.out.println(linkedlist.peekLast());}
}
3.集合的遍历

Iterator和foreach

lterator:

// 先创一个Student类
package com.java1234_8;public class Student {private String name;private Integer age;public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}@Overridepublic String toString() {return "Student [name=" + name + ", age=" + age + "]";}public Student(String name, Integer age) {super();this.name = name;this.age = age;}public Student() {super();}
}
// lterator 
import java.util.Iterator;
import java.util.LinkedList;
public class LteratorTest {public static void main(String[] args) {LinkedList<Student>  linkedlist=new LinkedList<Student>();linkedlist.add(new Student("张三",30));linkedlist.add(new Student("李四",20));linkedlist.add(new Student("王二",60));linkedlist.add(new Student("麻子",50));// 	System.out.println(linkedlist);/*** 用Iterator遍历集合*/Iterator<Student> it=linkedlist.iterator();  // 返回一个迭代器//has.Next判断有没有值while(it.hasNext()){// .next为取值Student s=it.next();   // 返回迭代的下一个元素。System.out.println("姓名:"+s.getName()+"年龄:"+s.getAge());}}
}
//

HashSet:

Set集合是Collection接口的子接口,没有对Collection接口进行扩展,里面不允许存在重复的内容;

import java.util.HashSet;
import java.util.Iterator;public class TestHashSet {public static void main(String[] args) {/*** 1,HashSet是无序* 2,不循序有重复的值*/HashSet<String> hs=new HashSet<String>();hs.add("21221");hs.add("112");hs.add("312");hs.add("421");hs.add("312");/*** 用Iterator遍历集合*/Iterator<String> it=hs.iterator();while(it.hasNext()){String s=it.next();System.out.println(s+" ");}}
}

HashMap集合:

是存放一对值的最大接口,即接口中的每一个元素都是一对,以key->value键值对的形式保存;

import java.util.HashMap;
import java.util.Iterator;public class TestHashMap {public static void main(String[] args) {HashMap<String,Student> hashMap=new HashMap<String,Student>();hashMap.put("1号", new Student("张三",10));hashMap.put("2号", new Student("李四",20));hashMap.put("3号", new Student("王五",30));// 通过key,获取valueStudent s=hashMap.get("1号");System.out.println(s.getName()+":"+s.getAge());Iterator<String> it=hashMap.keySet().iterator(); // 获取key的集合,再获取迭代器while(it.hasNext()){String key=it.next();  // 获取keyStudent student=hashMap.get(key);  // 通过key获取valueSystem.out.println("key="+key+" value=["+student.getName()+","+student.getAge()+"]");}}
}

九、Java多线程

1.Java多线程-多线程的引入

什么是多线程呢,我可以简单的理解成 一边吃饭,一边听音乐 这个是多线程;假如 吃完饭再听音乐,或者先听音乐再吃饭,这个是单线程。

程序里同时执行多个任务并且加以控制 这个是java多线程的含义。同时干多个事,能充分利用cpu 内存等硬件设备,提高程序运行效率。

public class Test01 {/*** 听音乐*/private static void music(){for(int i=0;i<100;i++){System.out.println("听音乐");}}/*** 吃饭*/private static void eat(){for(int i=0;i<100;i++){System.out.println("吃饭");}}public static void main(String[] args) {music();eat();}
}
//我们用上多线程,一般吃饭,一边听音乐, 
//Eat线程类:
public class Eat extends Thread{@Overridepublic void run() {for(int i=0;i<100;i++){try {Thread.sleep(100);System.out.println("吃饭");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}//Music类:
public class Music extends Thread{@Overridepublic void run() {for(int i=0;i<100;i++){try {Thread.sleep(100);System.out.println("听音乐");} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}
//测试
public class Test02 {public static void main(String[] args) {Music musicThread=new Music();Eat eatThread=new Eat();musicThread.start();eatThread.start();}
}
2.Java多线程-Java多线程实现

1,继承Thread类

2,实现Runnable接口

3,多线程实现数据共享

// 1,继承Thread类
public class Thread1 extends Thread{private int baoZi=1;private String threadName;public Thread1(String threadName) {super();this.threadName = threadName;}@Overridepublic void run() {while(baoZi<=10){System.out.println(threadName+" 吃"+baoZi+"第个包子");baoZi++;}}public static void main(String[] args) {// 张三 李四一起吃包子 每人吃10个Thread1 t1=new Thread1("张三线程");Thread1 t2=new Thread1("李四线程");t1.start();t2.start();}  
}
//2,实现Runnable接口
public class Thread2 implements Runnable{private int baoZi=1;private String threadName;public Thread2(String threadName) {super();this.threadName = threadName;}@Overridepublic void run() {while(baoZi<=10){System.out.println(threadName+" 吃"+baoZi+"第个包子");baoZi++;}}public static void main(String[] args) {// 张三 李四一起吃包子 每人吃10个Thread2 t1=new Thread2("张三线程");Thread2 t2=new Thread2("李四线程");Thread t11=new Thread(t1);Thread t12=new Thread(t2);t11.start();t12.start();}}
//3,多线程实现数据共享
public class Thread3 implements Runnable{private int baoZi=1;private String threadName;public Thread3(String threadName) {super();this.threadName = threadName;}@Overridepublic synchronized void run() {while(baoZi<=10){System.out.println(threadName+" 吃"+baoZi+"第个包子");baoZi++;}}public static void main(String[] args) {Thread3 t1=new Thread3("超级张三线程");Thread t11=new Thread(t1);Thread t12=new Thread(t1);Thread t13=new Thread(t1);t11.start();t12.start();t13.start();}}
3.Java多线程-线程的状态

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gUQtXx9I-1657878648851)(D:\java1234\java1234笔记\java1234笔记截图\image-20220714212750489.png)]

1,创建状态

在程序中用构造方法创建了一个线程对象后,新的线程对象便处于新建状态,此时,它已经有了相应的

内存空间和其他资源,但还处于不可运行状态。新建一个线程对象可采用Thread 类的构造方法来实现,例

如,“Thread thread=new Thread();”。

2,就绪状态

新建线程对象后,调用该线程的start()方法就可以启动线程。当线程启动时,线程进入就绪状态。此时,

线程将进入线程队列排队,等待CPU 服务,这表明它已经具备了运行条件。

3,运行状态

当就绪状态的线程被调用并获得处理器资源时,线程就进入了运行状态。此时,自动调用该线程对象

的run()方法。run()方法定义了该线程的操作和功能。

4,堵塞状态

一个正在执行的线程在某些特殊情况下,如被人为挂起或需要执行耗时的输入/输出操作时,将让出

CPU 并暂时中止自己的执行,进入堵塞状态。堵塞时,线程不能进入排队队列,只有当引起堵塞的原因被

消除后,线程才可以转入就绪状态。

5,死亡状态

线程调用stop()方法时或run()方法执行结束后,即处于死亡状态。处于死亡状态的线程不具有继续运

行的能力。

4.Java多线程-线程常用方法

1,getName(); 返回该线程的名称。

2,currentThread();返回对当前正在执行的线程对象的引用。

3,isAlive();测试线程是否处于活动状态。

4,sleep();线程休眠。

5,setPriority(int newPriority);更改线程的优先级。

6,yield();暂停当前正在执行的线程对象,并执行其他线程。

public class Demo1 implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){// 获取当前线程Thread t=Thread.currentThread();System.out.println(t.getName()+":"+i); // 返回线程的名称}}public static void main(String[] args) {Demo1 demo1=new Demo1();new Thread(demo1).start();new Thread(demo1).start();new Thread(demo1,"线程3").start();}}
//
public class Demo2 implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){// 获取当前线程Thread t=Thread.currentThread();System.out.println(t.getName()+":"+i); // 返回线程的名称}}public static void main(String[] args) {Demo2 demo2=new Demo2();Thread t1=new Thread(demo2);System.out.println("t1是否活动:"+t1.isAlive());t1.start();System.out.println("t1是否活动:"+t1.isAlive());}
}
//
public class Demo3 implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){try {Thread.sleep(1000);// 获取当前线程Thread t=Thread.currentThread();System.out.println(t.getName()+":"+i); // 返回线程的名称} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {Demo3 demo1=new Demo3();new Thread(demo1).start();}}
//
public class Demo4 implements Runnable{@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){try {Thread.sleep(1000);// 获取当前线程Thread t=Thread.currentThread();System.out.println(t.getName()+":"+i); // 返回线程的名称} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {Demo4 demo4=new Demo4();Thread t1=new Thread(demo4,"线程A");Thread t2=new Thread(demo4,"线程B");Thread t3=new Thread(demo4,"线程C");t1.setPriority(Thread.MAX_PRIORITY);t2.setPriority(Thread.MIN_PRIORITY);t3.setPriority(Thread.NORM_PRIORITY);t3.start();t1.start();t2.start();}}
//
public class Demo5 implements Runnable{@SuppressWarnings("static-access")@Overridepublic void run() {// TODO Auto-generated method stubfor(int i=0;i<10;i++){try {Thread.sleep(100);// 获取当前线程Thread t=Thread.currentThread();System.out.println(t.getName()+":"+i); // 返回线程的名称if(i==5){System.out.println("线程礼让:");Thread.currentThread().yield();}} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {Demo5 demo1=new Demo5();new Thread(demo1,"线程A").start();new Thread(demo1,"线程B").start();}}
5.Java多线程-线程同步
public class Thread1 implements Runnable{private int baoZi=1;private String threadName;public Thread1(String threadName) {super();this.threadName = threadName;}@Overridepublic void run() {while(baoZi<=10){System.out.println(threadName+" 吃第"+baoZi+"个包子");baoZi++;}}public static void main(String[] args) {Thread1 t1=new Thread1("超级张三线程");Thread t11=new Thread(t1);Thread t12=new Thread(t1);Thread t13=new Thread(t1);t11.start();t12.start();t13.start();}}
// 我们发现 会有多个线程同时进入方法吃包子的情况发生,这时候,就引入了线程同步。可以给方法加同步,同一时刻,只允许一个线程进入方法;// 关键字 synchronized
public class Thread3 implements Runnable{private int baoZi=1;private String threadName;public Thread3(String threadName) {super();this.threadName = threadName;}@Overridepublic synchronized void run() {while(baoZi<=10){System.out.println(threadName+" 吃第"+baoZi+"个包子");baoZi++;}}public static void main(String[] args) {Thread3 t1=new Thread3("超级张三线程");Thread t11=new Thread(t1);Thread t12=new Thread(t1);Thread t13=new Thread(t1);t11.start();t12.start();t13.start();}}
//
public class Thread4 implements Runnable{private int baoZi=1;private String threadName;public Thread4(String threadName) {super();this.threadName = threadName;}@Overridepublic void run() {/*** 同步块*/synchronized (this) {while(baoZi<=10){System.out.println(threadName+" 吃第"+baoZi+"个包子");baoZi++;}}}public static void main(String[] args) {Thread4 t1=new Thread4("超级张三线程");Thread t11=new Thread(t1);Thread t12=new Thread(t1);Thread t13=new Thread(t1);t11.start();t12.start();t13.start();}}

十、JavaIO流

1.IO 流简介**

定义:流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QMB4F0mj-1657878648852)(D:\java1234\java1234笔记\java1234笔记截图\image-20220714221038141.png)]

IO 流的分类

根据处理数据类型的不同分为:字符流和字节流

根据数据流向不同分为:输入流和输出流

2.文件操作File 类

1,public boolean mkdir() 创建此抽象路径名指定的目录。

2,public boolean createNewFile() 创建一个文件

3,public boolean delete() 删除此抽象路径名表示的文件或目录。如果此路径名表示一个目录,则该目录

必须为空才能删除。

4,public boolean exists() 测试此抽象路径名表示的文件或目录是否存在。

5,public File[] listFiles() 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文

件。

6,public boolean isDirectory() 测试此抽象路径名表示的文件是否是一个目录。

//创建文件目录和文件 
import java.io.File;
import java.io.IOException;public class Demo1 {public static void main(String[] args) throws IOException {File file=new File("c://java创建的目录");boolean b=file.mkdir();  // 创建虚拟目录if(b){System.out.println("目录创建成功!");file=new File("c://java创建的目录//java创建的文件.txt");boolean b2=file.createNewFile();  // 创建文件if(b2){System.out.println("文件创建成功!");}else{System.out.println("文件创建失败!");}}else{System.out.println("目录创建失败!");}}
}
// 删除文件和文件目录
import java.io.File;
import java.io.IOException;public class Demo2 {public static void main(String[] args) throws IOException {File file=new File("c://java创建的目录//java创建的文件.txt");if(file.exists()){  // 假如文件存在boolean b=file.delete();  // 删除文件if(b){System.out.println("删除文件成功!");}else{System.out.println("删除文件失败!");}}file=new File("c://java创建的目录");if(file.exists()){boolean b=file.delete();  // 删除目录if(b){System.out.println("删除目录成功!");}else{System.out.println("删除目录失败!");}}}
}
//遍历目录
import java.io.File;public class Demo3 {public static void main(String[] args) {File file=new File("C://apache-cxf-3.1.5");File files[]=file.listFiles();  // 遍历目录for(int i=0;i<files.length;i++){System.out.println(files[i]);}}
}
//递归遍历所有文件
import java.io.File;public class Demo4 {/*** 打印文件* @param file*/public static void listFile(File file){if(file!=null){if(file.isDirectory()){  // 是目录System.out.println(file);  // 打印下目录File f[]=file.listFiles();  // 遍历目录if(f!=null){for(int i=0;i<f.length;i++){listFile(f[i]);  // 递归调用}}}else{   // 是文件System.out.println(file);  // 是文件,直接打印文件的路径}}}public static void main(String[] args) {File file=new File("C://apache-tomcat-7.0.63");listFile(file);}
}
3.Java IO流-InputStream和OutputStream

InputStream是输入流 OutputStream是输出流;

InputStream输入流可以把文件从硬盘读取到内存;

OutputStream输出流可以把文件从内存写入到硬盘;

我们实际使用的都是InputStream和OutputStream的子类;

比如文件操作方面用的是FileInputStream和FileOutputStream;

准备工作,我们在C盘建一个txt文件 测试文件.txt ,随便加点内容:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AsP8GYTY-1657878648853)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220714222708236.png)]

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;public class Demo1 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");InputStream inputStream=new FileInputStream(file);  // 实例化FileInputStreambyte b[]=new byte[1024];int len=inputStream.read(b);inputStream.close(); // 关闭输入流System.out.println("读取的内容是:"+new String(b,0,len));}
}//上面那个是定义了固定字节数组 一批读取的,我们现在改进下,获取文件长度,然后定义指定字节数组的长度;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;public class Demo2 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");InputStream inputStream=new FileInputStream(file);  // 实例化FileInputStreamint fileLength=(int)file.length();byte b[]=new byte[fileLength];inputStream.read(b);inputStream.close(); // 关闭输入流System.out.println("读取的内容是:"+new String(b));}
}
//我们再来一种方式 一个字节一个字节读取;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;public class Demo3 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");InputStream inputStream=new FileInputStream(file);  // 实例化FileInputStreamint fileLength=(int)file.length();byte b[]=new byte[fileLength];int temp=0;int len=0;while((temp=inputStream.read())!=-1){// 一个字节一个字节读取,放到b字节数组里b[len++]=(byte)temp;}inputStream.close(); // 关闭输入流System.out.println("读取的内容是:"+new String(b));}
}//输出流;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;public class Demo4 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");OutputStream out=new FileOutputStream(file);String str="你好,我好,大家好,Java好";byte b[]=str.getBytes();out.write(b); //  将b字节数组写入到输出流out.close();  // 关闭输出流}
}
//我们把指定文件写入到文件;
//上面那种是直接覆盖的,我们再来一个追加的;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;public class Demo5 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");OutputStream out=new FileOutputStream(file,true);String str="你好,我好,大家好,Java好";byte b[]=str.getBytes();out.write(b); //  将b字节数组写入到输出流out.close();  // 关闭输出流}
}
4.Java IO流-BufferedInputStream和BufferedOutputStream

带缓冲的输入和输出流;

这里缓冲的概念,就是在A,B之间建立内存缓冲区,读取得快,就先放缓冲区,然后再从缓冲区写入指定目标,和没有缓冲比,效率快很多。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;public class Demo6 {/*** 缓冲* @throws Exception*/public static void bufferStream()throws Exception{// 定义了一个带缓冲的字节输入流BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream("C://《一头扎进J2SE》V2.0视频笔录2.doc"));// 定义了一个带缓冲的字节输出流BufferedOutputStream bufferedOutputStream=new BufferedOutputStream(new FileOutputStream("C://复制的《一头扎进J2SE》V2.0视频笔录2.doc"));int b=0;long startTime=System.currentTimeMillis(); // 开始时间while((b=bufferedInputStream.read())!=-1){bufferedOutputStream.write(b);}bufferedInputStream.close();bufferedOutputStream.close();long endTime=System.currentTimeMillis();  // 结束时间System.out.println("缓冲花费的时间是:"+(endTime-startTime));}/*** 非缓冲* @throws Exception*/public static void stream() throws Exception{InputStream inputStream=new FileInputStream("C://《一头扎进J2SE》V2.0视频笔录.doc");  // 定义一个输入流OutputStream outputStream=new FileOutputStream("C://复制的《一头扎进J2SE》V2.0视频笔录.doc");int b=0;long startTime=System.currentTimeMillis(); // 开始时间while((b=inputStream.read())!=-1){outputStream.write(b);}inputStream.close();outputStream.close();long endTime=System.currentTimeMillis();  // 结束时间System.out.println("非缓冲花费的时间是:"+(endTime-startTime));}public static void main(String[] args)throws Exception {stream();bufferStream();}
}
//把文件从A地址复制到B地址,运行输出://非缓冲花费的时间是:2368   缓冲花费的时间是:31//我们明显发现 带缓冲的效率高;
5.Java IO流-Reader和Writer

主要用于文本的读取和写入,一般使用的实现类是FileReader和FileWriter;


import java.io.File;
import java.io.FileReader;
import java.io.Reader;public class Demo1 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");Reader reader=new FileReader(file);char c[]=new char[1024]; // 字符数组int len=reader.read(c);reader.close();  // 关闭输入流System.out.println("读取的内容是:"+new String(c,0,len));}
}
// 直接读取;import java.io.File;
import java.io.FileReader;
import java.io.Reader;public class Demo2 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");Reader reader=new FileReader(file);char c[]=new char[1024]; // 字符数组int temp=0;int len=0;while((temp=reader.read())!=-1){c[len++]=(char)temp;}reader.close();  // 关闭输入流System.out.println("读取的内容是:"+new String(c,0,len));}
}
// 一个一个字符读取;import java.io.File;
import java.io.FileWriter;
import java.io.Writer;public class Demo3 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");Writer out=new FileWriter(file);String str="我爱中华";out.write(str);  // 将字符串写入输出流out.close();  // 关闭输出流}
}
// 写入文件;import java.io.File;
import java.io.FileWriter;
import java.io.Writer;public class Demo4 {public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");Writer out=new FileWriter(file,true);String str="我爱中华2";out.write(str);  // 将字符串写入输出流out.close();  // 关闭输出流}
}
//追加写入;

解决eclipse控制台信息显示不全问题

解决eclipse控制台信息显示不全问题

eclipes控制台有默认的显示行数或者大小;

我们可以设置下,来增大显示行数;

菜单->windows->preferences

搜索console

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k0zFmrRG-1657878648859)(D:\java1234\java1234笔记\java1234笔记截图\image-20220714224611125.png)]

十一、java debug断点调试的重要性

java debug断点调试的重要性

以掌握好debug断点调试,我们无需搞输出语句 直接打断点,程序执行到断点处停止,我们可以直接观察变量的值,以及表达式的值,我们甚至可以

动态修改变量的值来调试,非常方便,然后我们可以控制调试的执行,主要有F6 执行下一步,F8执行完成或者执行到下一个断点,F5进入方法内部;

1.eclipse debug使用基本操作

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BVVHhEBf-1657878648860)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20220714225400501.png)]

2.eclipse debug常见调试 F6 单步 F8完成 F5进入方法

eclipse 里debug调试主要三个快捷方式

F6 单步执行 执行到下一行代码

F8是执行完 假如后面还有断点 执行到下一个断点处

F5是进入方法里执行

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-MPAGdief-1657878648861)(D:\java1234\java1234笔记\java1234笔记截图\image-20220714225516462.png)]

3.eclipse debug ctrl+shift+i查看表示式值

eclipse debug ctrl+shift+i查看表示式值

在eclipse里debug断点调试的时候,当我们需要查看某个表达式的时候,可以用ctrl+shift+i快捷方式;

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-nmdW5r4X-1657878648862)(D:\java1234\java1234笔记\java1234笔记截图\image-20220714225642044.png)]

4.eclipse debug 运行时动态修改变量值

这里讲一个高级点的debug功能,就是可以运行时候,动态修改变量的值。在企业级开发中,往往搞点测试数据麻烦,

所以直接debug的时候随便改数据,来进行各种测试,比较方便;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;

public class Demo4 {

public static void main(String[] args) throws Exception {File file=new File("C://测试文件.txt");Writer out=new FileWriter(file,true);String str="我爱中华2";out.write(str);  // 将字符串写入输出流out.close();  // 关闭输出流
}

}
//追加写入;


#### **解决eclipse控制台信息显示不全问题**解决eclipse控制台信息显示不全问题eclipes控制台有默认的显示行数或者大小;我们可以设置下,来增大显示行数;菜单->windows->preferences搜索console[外链图片转存中...(img-k0zFmrRG-1657878648859)]#### 十一、**java debug断点调试的重要性**java debug断点调试的重要性以掌握好debug断点调试,我们无需搞输出语句 直接打断点,程序执行到断点处停止,我们可以直接观察变量的值,以及表达式的值,我们甚至可以动态修改变量的值来调试,非常方便,然后我们可以控制调试的执行,主要有F6 执行下一步,F8执行完成或者执行到下一个断点,F5进入方法内部;##### 1.**eclipse debug使用基本操作**[外链图片转存中...(img-BVVHhEBf-1657878648860)]##### 2.**eclipse debug常见调试 F6 单步 F8完成 F5进入方法**eclipse 里debug调试主要三个快捷方式 F6 单步执行 执行到下一行代码F8是执行完 假如后面还有断点 执行到下一个断点处F5是进入方法里执行[外链图片转存中...(img-MPAGdief-1657878648861)]##### 3.**eclipse debug ctrl+shift+i查看表示式值**eclipse debug ctrl+shift+i查看表示式值在eclipse里debug断点调试的时候,当我们需要查看某个表达式的时候,可以用ctrl+shift+i快捷方式;[外链图片转存中...(img-nmdW5r4X-1657878648862)]##### 4.**eclipse debug 运行时动态修改变量值**这里讲一个高级点的debug功能,就是可以运行时候,动态修改变量的值。在企业级开发中,往往搞点测试数据麻烦,所以直接debug的时候随便改数据,来进行各种测试,比较方便;[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-q30gmBb3-1657878648863)(D:\java1234\java1234笔记\java1234笔记截图\image-20220714225754486.png)]

http://www.ppmy.cn/news/131457.html

相关文章

# Chapter 5

Chapter 5 Gradient Temporal-Difference Learning with Linear Function Approximation 本章提供了线性函数近似情况下梯度-TD算法的核心思想和理论结果。在这里&#xff0c;我们在Baird(1995&#xff1b;1999)的工作基础上&#xff0c;探讨了用于线性函数逼近的时差学习的真…

【C++】课节笔记及梳理总结---EP5

一、课节笔记 第四章 函数与预处理 Ⅰ、概述 1.模块化程序设计   (1)基本思想&#xff1a;将一个大程序按照功能分割成若干个小模块   (2)开发方法&#xff1a;自上而下&#xff0c;逐步分解&#xff0c;分而治之  2.※ C 是模块化程序设计语言 ※ Ⅱ、定义函数的一般形式…

USB Composite 组合设备之多路CDC实现

USB Composite 组合设备之多路CDC实现 USB复合设备与组合设备区别效果展示修改相关配置修改配置项修改设备描述符修改配置、接口、端点描述符接口修改FIFO配置 知识点FIFO分配 注意事项 USB复合设备与组合设备区别 其实多个接口组合在一起有2种情况 第一种叫做USB复合设备&…

Ep5 线性模型with Pytorch

1、流程 确定数据集、 设计模型(算出预测值)、 构建损失函数&#xff08;最终为一个标量值&#xff0c;只有标量才能用backward&#xff09;和优化器、 训练周期&#xff08;forward算loss&#xff0c;backward算grad&#xff0c;update更新wi&#xff09; 2、numpy的广播…

HBase Shell 常用命令练习

HBase Shell 常用命令练习 前言一、HBase Shell是什么&#xff1f;二、HBase Shell使用步骤1.启动HBase2.启用HBase Shell3.键入HBase Shell命令操作HBase 三、常用HBase Shell实例1.常用的HBase Shell命令2.一个运用上述命令的综合实例&#xff1a; 总结 前言 提示&#xff1…

侃侃算法EP5·二叉树及其遍历

1. 前言 这个板块旨在记录一些日常中或是面试中会问到的算法和数据结构相关的内容&#xff0c;更多是给自己总结和需要的人分享。在内容部分可能由于我的阅历和实战经历不足&#xff0c;会有忽视或是写错的点&#xff0c;还望轻喷。 2. 内容 关于什么是树、子树、根节点、叶…

ES6、ES7、ES8、ES9、ES10新特性及其兼容性

强烈推荐阅读一篇文章&#xff0c;也是自己为了做保存把地址贴到自己博客&#xff0c;大家一起学习&#xff1a; ECMAScript 6 入门教程——阮一峰 盘点ES7、ES8、ES9、ES10新特性

es_01

字段&#xff1a;等于一个属性 文档&#xff1a;行数据等于多个字段组成 映射&#xff1a;mapping表结构 索引&#xff1a;index 数据库 存文档 类型&#xff1a;忽略 正排索引&#xff1a; 需要按照key来搜索每个key下的value&#xff0c;要收到全部的数据&#xff0c;就要进…

es(八)

单字符串串多字段查询:Dis Max Query 想在百度搜索一个单字符 should是如何算分过程 查询 should 语句句中的两个查询加和两个查询的评分乘以匹配语句句的总数除以所有语句句的总数

JS高级+ES678

js高级 数据类型 基本(值)类型 Number: 任意数值String: 任意文本Boolean: true/falseundefined: undefinednull: null 对象(引用)类型 Object: 任意对象 主要用来包含无序复杂的数据Array: 特别的对象类型(下标/内部数据有序)Function: 特别的对象类型(可执行)&#xff0c;F…

阿里云ECS部署ES

背景 最近越来越多的公司把业务搬迁到云上&#xff0c;公司也有这个计划&#xff0c;自己抽时间在阿里云和Azure上做了一些小的尝试&#xff0c;现在把阿里云上部署ES和kibana记录下来。为以后做一个参考&#xff0c;也希望对其他人有帮助。 这里以阿里云为例&#xff0c;由于测…

ES-08-ElasticSearch数据分片(shard)

说明 ElasticSearch数据分片&#xff08;shard&#xff09;创建多分片索引、更改多分片索引副本分片数量、路由计算和分片控制官方文档&#xff1a;https://www.elastic.co/cn/ 核心概念 》什么是数据分片&#xff08;shard&#xff09;&#xff1f; 一个分片是一个底层的工…

ES-09-ElasticSearch分词器

说明 ElasticSearch分词器默认分词器&#xff08;标准分词器&#xff09;、ik分词器、ik分词器扩展字典自定义词语关键词&#xff1a;keyword、text、ik_max_word、ik_smart、词条、词典、倒排表官方文档&#xff1a;https://www.elastic.co/cn/ik分词器文档&#xff1a;https…

Elasticsearch8.0

Elastic 中国社区官方博客_CSDN博客-Elastic,Elasticsearch,Kibana领域博主Elastic 中国社区官方博客擅长Elastic,Elasticsearch,Kibana,等方面的知识https://elasticstack.blog.csdn.net/ ✅ 启动 elasticsearch # cd /usr/local/elastic/elasticsearch/ # ./bin/elasticsearc…

ES6/ES7/ES8/ES9/ES10

ES10 ES10 功能完全指南 好犯困啊 我来打打字 string.prototype.matchAll() ‘Hello’.match(‘l’) eg:判断字符串中存在几个某元素 yannanna’.match(/n/g).length 扁平化多维数组&#xff08;想不出啥时候会用到&#xff09; let array [1, 2, 3, 4, 5]; array.map(x &g…

ES

文章目录 1. 什么是ElasticSearch&#xff1f;为什么要使用Elasticsearch?——克服模糊查询的缺点、查询速度快2. ES中的倒排索引是什么&#xff1f;——词→文章3. ES是如何实现master选举的&#xff1f;——各节点分别排序投票4. 如何解决ES集群的脑裂问题——增大最少候选节…

es 客户端

ES客户端&#xff1a;Elasticsearch Clients 语言无关性 Java REST ClientJava APIPython APIGo API.Net APIPHP APIJavaScripts APIRuby APIPerl APIElandRustCommunity Contributed Clients Java API 生命周期&#xff08;生卒年&#xff1a;ES 0.9 - ES 7.x&#xff09;…

ES7,ES8,ES10新特性

ES7 ES7在ES6的基础上增加了三项内容 求幂运算符 ** console.log(3 ** 2 ) // 9 Array.prototype.includes()方法 includes()的作用是查找一个值在不在数组中&#xff0c;接受两个参数&#xff1a;搜索值和搜索的开始索引。如果没有传递参数默认的索引是0 // 下面的这两种方…

ES7+ES8

撰文为何 身为一个前端开发者&#xff0c;ECMAScript(以下简称ES)早已广泛应用在我们的工作当中。了解ECMA机构流程的人应该知道&#xff0c;标准委员会会在每年的6月份正式发布一次规范的修订&#xff0c;而这次的发布也将作为当年的正式版本。以后的改动&#xff0c;都会基于…

elasticsearch系列七:ES Java客户端-Elasticsearch Java client

一、ES Client 简介 1. ES是一个服务&#xff0c;采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API &#xff0c;端口 9200 这种连接方式对应于架构图中的RESTful style API这一层&#xff0c;这种客户端的连接方式是RESTful风格的&#xff0c;使用http…
最新文章