17做网站广州沙河地址,网页制作的基本知识,网站模板 英文,建设网站平台的建议文章目录 IO流字符流字符流原理解析flush和close方法 文件拷贝代码文件加密解密修改文件中的数据 缓冲流字节缓冲流字符缓冲流例题 转换流序列化流序列化流/对象操作输出流 反序列化流序列化流/反序列化流的细节汇总打印流字节打印流字符打印流 解压缩流压缩流Commons-io常见方… 文章目录 IO流字符流字符流原理解析flush和close方法 文件拷贝代码文件加密解密修改文件中的数据 缓冲流字节缓冲流字符缓冲流例题 转换流序列化流序列化流/对象操作输出流 反序列化流序列化流/反序列化流的细节汇总打印流字节打印流字符打印流 解压缩流压缩流Commons-io常见方法Hutool工具包 多线程并发和并行实现方式成员方法生命周期同步代码块同步方法Lock锁等待唤醒机制生产者和消费者 IO流
字符流
字符流原理解析
创建字符输入流对象 底层:联文件并创建缓冲区长度为8192的字节数读取数据
底层
判断缓冲区中是否有数据可以读取缓冲区没有数据:就从文件中获取数据装到缓冲区中每次尽可能装满缓冲区 如果文件中也没有数据了返回-1缓冲区有数据就从缓冲区中读取 空参的read方法:一次读取一个字节遇到中文一次读多个字节把字节解码并转成十进制返回 有参的read方法:把读取字节解码强转三步合并了强转之后的字符放到数组中
flush和close方法
成员方法说明public void flush()将缓冲区中的数据刷新到本地文件中public void close()释放资源/关流
文件拷贝代码 public class demo1 {public static void main(String[] args) throws IOException {File src new File(E:\\文件练习JAVA嘿嘿嘿);File dest new File(E:\\拷贝后的地址);copydir(src, dest);}private static void copydir(File src, File dest) throws IOException {dest.mkdirs();File[] files src.listFiles();for(File file : files){if(file.isFile()) {FileInputStream fis new FileInputStream(file);FileOutputStream fos new FileOutputStream(new File(dest, file.getName()));byte[] bytes new byte[1024];int len;while((len fis.read(bytes)) ! -1){fos.write(bytes, 0, len);}fos.close();fis.close();}else {copydir(file, new File(dest, file.getName()));}}}
}文件加密解密
public class Demo2 {public static void main(String[] args) throws IOException {FileInputStream fis new FileInputStream(C:\\Users\\HP\\Desktop\\111\\哈哈\\4月27日 日报.pdf);FileOutputStream fos new FileOutputStream(C:\\Users\\HP\\Desktop\\111\\哈哈\\copy_4月27日 日报.pdf);int b;while ((b fis.read()) ! -1) {fos.write(b ^ 2);}fos.close();fis.close();}
}修改文件中的数据
//1
public class demo1 {public static void main(String[] args) throws IOException {FileReader fr new FileReader(a.txt);StringBuilder sb new StringBuilder();int ch;while((ch fr.read()) ! -1) {sb.append((char) ch);}fr.close();System.out.println(sb);String str sb.toString();String[] arrStr str.split(-);ArrayListInteger list new ArrayList();for(String s : arrStr) {int i Integer.parseInt(s);list.add(i);}Collections.sort(list);System.out.println(list);FileWriter fw new FileWriter(a.txt);for (int i 0; i list.size(); i) {if(i list.size() - 1)fw.write(list.get(i) );elsefw.write(list.get(i) -);}fw.close();}
}//2
public class demo1 {public static void main(String[] args) throws IOException {FileReader fr new FileReader(a.txt);StringBuilder sb new StringBuilder();int ch;while((ch fr.read()) ! -1) {sb.append((char) ch);}fr.close();Integer[] array Arrays.stream(sb.toString().split(-)).map(new FunctionString, Integer() {Overridepublic Integer apply(String s) {return Integer.parseInt(s);}}).sorted().toArray(new IntFunctionInteger[]() {Overridepublic Integer[] apply(int value) {return new Integer[value];}});FileWriter fw new FileWriter(a.txt);for (int i 0; i array.length; i) {if(i array.length - 1)fw.write(array[i] );elsefw.write(array[i] -);}fw.close();}
}缓冲流
字节缓冲流
方法名称说明public BufferedInputStream(InputStream is)把基本流包装成高级流提高读取数据的性能public BufferedOutputStream(OutputStream os)把基本流包装成高级流提高写出数据的性能
原理 底层自带了长度为8192的缓冲区提高性能
字符缓冲流
方法名称说明public BufferedReader(Reader r)把基本流变成高级流public BufferedWriter(Writer w)把基本流变成高级流
方法名称说明public String readLine()读取一行数据如果没有数据可读了会返回 nullpublic void newLine()跨平台的换行
例题
只能使用三次的程序
public class demo2 {public static void main(String[] args) throws IOException {BufferedReader br new BufferedReader(new FileReader(a.txt));String s br.readLine();br.close();System.out.println(s);int count Integer.parseInt(s);count;if(count 3){System.out.println(第 count 免费试用~~~);}else {System.out.println(使用结束啦请充值会员继续使用~~);}BufferedWriter bw new BufferedWriter(new FileWriter(a.txt));bw.write(count );bw.close();}
}转换流 作用1 指定字符集读写 作用2 字节流想要使用字符流中的方法
public class demo3 {public static void main(String[] args) throws IOException {FileReader fr new FileReader(a.txt, Charset.forName(UTF-8));int ch;while((ch fr.read())! -1)System.out.print((char)ch);fr.close();}
}字符转换输入流InputStreamReader 字符转换输出流0utputStreamWriter
序列化流
可以把Java中的对象写到本地文件中
序列化流/对象操作输出流
一、构造方法
构造方法说明public ObjectOutputStream(0utputStream out)把基本流包装成高级流
二、成员方法
成员方法说明public final void writeObject(Object obj)把对象序列化写出到文件中去
public class demo4 {public static void main(String[] args) throws IOException {Student stu new Student(xiaodu, 18);ObjectOutputStream oos new ObjectOutputStream(new FileOutputStream(a.txt));oos.writeObject(stu);oos.close();}
}使用对象输出流将对象保存到文件时会出现NotserializableException异常
解决方案需要让Javabean类实现Serializable接口
public class Student implements Serializable{}反序列化流
一、构造方法
public ObjectInputStream(InputStream in)把基本流变成高级流
二、成员方法
public Object readObject()把序列化到本地文件中的对象读取到程序中来。
public class demo5 {public static void main(String[] args) throws IOException, ClassNotFoundException {ObjectInputStream ois new ObjectInputStream(new FileInputStream(a.txt));Object o ois.readObject();System.out.println(o);}
}transient关键字该关键字标记的成员不参与序列化过程。
序列化流/反序列化流的细节汇总
使用序列化流将对象写到文件时需要让Javabean类实现Serializable接口。 否则会出现NotSerializableException异常序列化流写到文件中的数据是不能修改的一旦修改就无法再次读回来了
打印流
分类打印流一般是指PrintStreamPrintWriter两个类 特点1打印流只操作文件目的地不操作数据源 特点2特有的写出方法可以实现数据原样写出 例如 打印97------文件中97 打印true------文件中true 特点3特有的写出方法可以实现自动刷新自动换行 打印一次数据 写出 换行 刷新
字节打印流
一、构造方法
构造方法说明public PrintStream(OutputStream/File/String)关联字节输出流/文件/文件路径public PrintStream(String fileName, Charset charset)指定字符编码public PrintStream(OutputStream out, boolean autoFlush)自动刷新public PrintStream(OutputStream out, boolean autoFlush, String encoding)指定字符编码且自动刷新
二、成员方法
成员方法说明public void write(int b)常规方法: 将指定的字节写出public void println(Xxx xx)特有方法: 打印任意数据自动刷新自动换行public void print(Xxx xx)特有方法: 打印任意数据不换行public void printf(String format, Object... args)特有方法: 带有占位符的打印语句不换行
public class demo6 {public static void main(String[] args) throws FileNotFoundException {PrintStream ps new PrintStream(new FileOutputStream(a.txt), true, Charset.forName(UTF-8));ps.println(97);ps.println(true);ps.println(嘿嘿嘿);ps.close();}
}字符打印流
一、构造方法
构造方法说明public PrintWriter(Writer/File/String)关联字节输出流/文件/文件路径public PrintWriter(String fileName, Charset charset)指定字符编码public PrintWriter(Writer w, boolean autoFlush)自动刷新public PrintWriter(OutputStream out, boolean autoFlush, Charset charset)指定字符编码且自动刷新
二、成员方法
成员方法说明public void write(...)常规方法: 规则跟之前一样写出字节或者字符串public void println(Xxx xx)特有方法: 打印任意类型的数据并且换行public void print(Xxx xx)特有方法: 打印任意类型的数据不换行public void printf(String format, Object... args)特有方法: 带有占位符的打印语句
public class demo7 {public static void main(String[] args) throws IOException {PrintWriter pw new PrintWriter(new FileWriter(a.txt), true);pw.println(111);pw.println(222);pw.println(333);pw.close();}
}解压缩流
public class demo8 {public static void main(String[] args) throws IOException {File src new File(E:\\aaa.zip);File dest new File(E:\\);unzip(src, dest);}private static void unzip(File src, File dest) throws IOException {ZipInputStream zip new ZipInputStream(new FileInputStream(src));ZipEntry entry;while((entry zip.getNextEntry()) ! null) {if(entry.isDirectory()) {File file new File(dest, entry.toString());file.mkdirs();}else {FileOutputStream fos new FileOutputStream(new File(dest, entry.toString()));int b;while ((b zip.read()) ! -1) {fos.write(b);}fos.close();zip.closeEntry();}}zip.close();}
}压缩流
public class ZipStreamDemo2 {public static void main(String[] args) throws IOException {// 源文件路径File src new File(E:\\TestCode\\testFile.txt);// 压缩文件存储路径File dest new File(E:\\TestCode);// 执行文件压缩toZip(src, dest);}private static void toZip(File src, File dest) throws IOException {// 创建压缩输出流ZipOutputStream zos new ZipOutputStream(new FileOutputStream(new File(dest, testFile.zip)));// 创建一个zip文件实体并添加到压缩流中ZipEntry entry new ZipEntry(src.getName());zos.putNextEntry(entry);// 读取源文件并写入压缩流FileInputStream fis new FileInputStream(src);int b;while ((b fis.read()) ! -1) {zos.write(b);}// 关闭当前zip条目并压缩流zos.closeEntry();zos.close();}
}Commons-io
Commons-io是apache开源基金组织提供的一组有关io操作的开源工具包 在项目中创建一个文件夹:lib将jar包复制粘贴到lib文件夹右键点击jar包选择 Add as Library -点击OK在类中导包使用
常见方法
FileUtils类(文件/文件夹相关)说明static void copyFile(File srcFile, File destFile)复制文件static void copyDirectory(File srcDir, File destDir)复制文件夹static void copyDirectoryToDirectory(File srcDir, File destDir)复制文件夹static void deleteDirectory(File directory)删除文件夹static void cleanDirectory(File directory)清空文件夹static String readFileToString(File file, Charset encoding)读取文件中的数据变成成字符串static void write(File file, CharSequence data, String encoding)写出数据
构造方法说明public static int copy(InputStream input, OutputStream output)复制文件public static int copyLarge(Reader input, Writer output)复制大文件public static String readLines(Reader input)读取数据public static void write(String data, OutputStream output)写出数据
Hutool工具包
相关类说明IoUtil流操作工具类FileUtil文件读写和操作的工具类FileTypeUtil文件类型判断工具类WatchMonitor目录、文件监听ClassPathResource针对ClassPath中资源的访问封装FileReader封装文件读取FileWriter封装文件写入
多线程
线程是操作系统能够进行运算调度的最小单位。它被包含在进程之中是进程中的实际运作单位。
并发和并行
并发在同一时刻有多个指令在单个CPU上交替执行
并行在同一时刻有多个指令在多个CPU上同时执行
实现方式 继承Thread类的方式进行实现 自己定义一个类继承Thread重写run方法创建子类的对象并启动线程 public class demo1 {public static void main(String[] args) {MyThread t1 new MyThread();MyThread t2 new MyThread();t1.setName(现成1);t2.setName(现成2);t1.start();t2.start();}
}
public class MyThread extends Thread{Overridepublic void run() {for (int i 0; i 100; i) {System.out.println(getName() HelloWorld);}}
} 实现Runnable接口的方式进行实现 自己定义一个类实现Runnable接口重写里面的run方法创建自己的类的对象创建一个Thread类的对象并开启线程 public class demo1 {public static void main(String[] args) {myRun mr new myRun();Thread t1 new Thread(mr);Thread t2 new Thread(mr);t1.setName(现成1);t2.setName(现成2);t1.start();t2.start();}
}
public class myRun implements Runnable{Overridepublic void run() {for (int i 0; i 100; i) {System.out.println(Thread.currentThread().getName() HelloWorld);}}
}利用Callable接口和Future接口方式实现可以获取到多线程运行的结果 创建一个类MyCallable实现Callable接口 重写ca11是有返回值的表示多线程运行的结果 创建MyCa1lable的对象表示多线程要执行的任务 创建FutureTask的对象作用管理多线程运行的结果 创建Thread类的对象并启动表示线程 public class demo1 {public static void main(String[] args) throws ExecutionException, InterruptedException {MyCallable mc new MyCallable();FutureTaskInteger ft new FutureTask(mc);Thread t1 new Thread(ft);t1.start();Integer result ft.get();System.out.println(result);}
}
public class MyCallable implements CallableInteger {Overridepublic Integer call() throws Exception {int sum 0;for (int i 0; i 100; i) {sum i;}return sum;}
}对比
成员方法
方法名称说明String getName()返回此线程的名称void setName(String name)设置线程的名字构造方法也可以设置名字static Thread currentThread()获取当前线程的对象static void sleep(long time)让线程休眠指定的时间单位为毫秒setPriority(int newPriority)设置线程的优先级final int getPriority()获取线程的优先级final void setDaemon(boolean on)设置为守护线程public static void yield()出让线程/礼让线程public static void join()插入线程/插队线程
生命周期 同步代码块
格式
synchronized{}特点1锁默认打开有一个线程进去了锁自动关闭 特点2里面的代码全部执行完毕线程出来锁自动打开
public class MyThread1 extends Thread{static int ticket 0;static Object obj new Object();Overridepublic void run() {while (true) {synchronized (obj) {if(ticket 100) {try {Thread.sleep(10);} catch (InterruptedException e) {throw new RuntimeException(e);}ticket;System.out.println(getName() 正在卖 ticket 票);} else {break;}}}}
}
public class demo1 {public static void main(String[] args) {MyThread1 t1 new MyThread1();MyThread1 t2 new MyThread1();t1.setName(111);t2.setName(222);t1.start();t2.start();}
}同步方法
就是把synchronized关键字加到方法上
特点1 同步方法是锁住方法里面所有的代码
特点2锁对象不能自己指定
非静态this
静态当前类的字节码文件对象
public class MyRunnable implements Runnable{int ticket 0;Overridepublic void run() {while (true) {try {if(method())break;} catch (InterruptedException e) {throw new RuntimeException(e);}}}private synchronized boolean method() throws InterruptedException {if(ticket 100)return true;else{Thread.sleep(10);ticket;System.out.println(Thread.currentThread().getName() ticket);}return false;}
}
public class demo1 {public static void main(String[] args) {MyRunnable mr new MyRunnable();Thread t1 new Thread(mr);Thread t2 new Thread(mr);Thread t3 new Thread(mr);t1.setName(窗口1);t2.setName(窗口2);t3.setName(窗口3);t1.start();t2.start();t3.start();}
}Lock锁
虽然我们可以理解同步代码块和同步方法的锁对象问题 但是我们并没有直接看到在哪里加上了锁在哪里释放了锁 为了更清晰的表达如何加锁和释放锁JDK5以后提供了一个新的锁对象Lock
Lock实现提供比使用synchronized方法和语句可以获得更广泛的锁定操作 Lock中提供了获得锁和释放锁的方法 void lock()获得锁 void unlock()释放锁
手动上锁、手动释放锁
Lock是接口不能直接实例化这里采用它的实现类ReentrantLock来实例化 ReentrantLock的构造方法 ReentrantLock()创建一个ReentrantLock的实例
public class LockExample { private Lock lock new ReentrantLock(); public void doSomething() { lock.lock(); // 手动上锁 try { // 访问或修改共享资源的代码 // ... } finally { lock.unlock(); // 手动释放锁确保在finally块中执行以处理异常 } }
}等待唤醒机制生产者和消费者
方法名称说明void wait()当前线程等待直到被其他线程唤醒void notify()随机唤醒单个线程void notifyAll()唤醒所有线程
已经到底啦 文章转载自: http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.mkxxk.cn.gov.cn.mkxxk.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.zwxfj.cn.gov.cn.zwxfj.cn http://www.morning.czwed.com.gov.cn.czwed.com http://www.morning.znqztgc.cn.gov.cn.znqztgc.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.wktbz.cn.gov.cn.wktbz.cn http://www.morning.mztyh.cn.gov.cn.mztyh.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.wskn.cn.gov.cn.wskn.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.wmnpm.cn.gov.cn.wmnpm.cn http://www.morning.dhtdl.cn.gov.cn.dhtdl.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.snnwx.cn.gov.cn.snnwx.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.zrmxp.cn.gov.cn.zrmxp.cn http://www.morning.flmxl.cn.gov.cn.flmxl.cn http://www.morning.xfhms.cn.gov.cn.xfhms.cn http://www.morning.nqyzg.cn.gov.cn.nqyzg.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.nqmkr.cn.gov.cn.nqmkr.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.tqlhn.cn.gov.cn.tqlhn.cn http://www.morning.zqfz.cn.gov.cn.zqfz.cn http://www.morning.mhdwp.cn.gov.cn.mhdwp.cn http://www.morning.ysqb.cn.gov.cn.ysqb.cn http://www.morning.hrzky.cn.gov.cn.hrzky.cn http://www.morning.kjksn.cn.gov.cn.kjksn.cn http://www.morning.dfndz.cn.gov.cn.dfndz.cn http://www.morning.lxjcr.cn.gov.cn.lxjcr.cn http://www.morning.qmwzr.cn.gov.cn.qmwzr.cn http://www.morning.tsrg.cn.gov.cn.tsrg.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.nqcts.cn.gov.cn.nqcts.cn http://www.morning.rlrxh.cn.gov.cn.rlrxh.cn http://www.morning.xhhzn.cn.gov.cn.xhhzn.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.cmqrg.cn.gov.cn.cmqrg.cn http://www.morning.amonr.com.gov.cn.amonr.com http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.ctfh.cn.gov.cn.ctfh.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.yqrfn.cn.gov.cn.yqrfn.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.qbpqw.cn.gov.cn.qbpqw.cn http://www.morning.tpnch.cn.gov.cn.tpnch.cn http://www.morning.fncgw.cn.gov.cn.fncgw.cn http://www.morning.nlqmp.cn.gov.cn.nlqmp.cn http://www.morning.ljxps.cn.gov.cn.ljxps.cn http://www.morning.cczrw.cn.gov.cn.cczrw.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.lqqqh.cn.gov.cn.lqqqh.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.hmxrs.cn.gov.cn.hmxrs.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.gfznl.cn.gov.cn.gfznl.cn http://www.morning.nqnqz.cn.gov.cn.nqnqz.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.sjwqr.cn.gov.cn.sjwqr.cn http://www.morning.tsdjj.cn.gov.cn.tsdjj.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn