企业网站备个人,做网站需要买ip地址吗,wordpress修改固定链接404,山东正元建设网站目录 打印流概述字节打印流构造方法成员方法代码示例 字符打印流构造方法成员方法代码示例 打印流的应用场景 解压缩/压缩流解压缩流压缩流 Commons-io 工具包概述Commons-io 使用步骤Commons-io 常见方法代码示例 Hutool 工具包 打印流
概述
分类#xff1a;打印流一般是指… 目录 打印流概述字节打印流构造方法成员方法代码示例 字符打印流构造方法成员方法代码示例 打印流的应用场景 解压缩/压缩流解压缩流压缩流 Commons-io 工具包概述Commons-io 使用步骤Commons-io 常见方法代码示例 Hutool 工具包 打印流
概述
分类打印流一般是指字节打印流 PrintStream、字符打印流 PrintWriter 两个类打印流只有输出流没有输入流
特点
打印流只操作文件的目的地不操作数据源特有的写出方法可以实现数据原样写出 文件中是 97打印 97文件中是 true打印 true 特有的写出方法可以实现自动刷新自动换行 打印一次数据 写出 换行 刷新
字节打印流
构造方法
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) 特有方法带有占位符的打印语句不换行
代码示例 import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;public class printStream {public static void main(String[] args) throws IOException, FileNotFoundException {// 创建对象PrintStream ps new PrintStream(new FileOutputStream(bbb.txt),true,UTF-8);// 写出数据ps.println(97);ps.print(true);ps.println();ps.printf(%s爱上了%s, 阿珍, 阿强);// 释放资源ps.close();}
}字符打印流
构造方法
public PrintWriter(Write/File/String) 关联字节输出流、文件或文件路径public PrintWriter(String fileName, Charset charset) 指定字符集public PrintWriter(Write 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) 特有方法是带有占位符的打印语句 。
代码示例
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;public class printStream {public static void main(String[] args) throws IOException {// 创建对象PrintWriter pw new PrintWriter(new FileWriter(bbb.txt),true);// 写出数据pw.println(阿珍爱上了阿强!);pw.print(阿强爱上了阿珍);pw.printf(%s爱上了%s, 阿珍, 阿强);// 释放资源pw.close();}
}打印流的应用场景
System.out.println(123);平时用的这个输出语句的完整形式其实是以下样子
PrintStream ps System.out;
ps.println(123);获取打印流的对象此打印流在虚拟机启动的时候由虚拟机创建默认指向控制台
这是特殊的打印流也叫系统中的标准输出流是不能关闭的因为在系统中是唯一的
解压缩/压缩流
解压缩流
ZipInputStream 是 Java 中用于读取 ZIP 文件的输入流它继承自 InflaterInputStream。
解压本质把压缩包里面的每一个文件或者文件夹读取出来按照层级拷贝到目的地当中压缩包里面的每一个文件是一个 ZipEntry 对象所以也可以说是把每一个 ZipEntry 对象按照层级拷贝到本地另一个文件夹中
构造方法ZipInputStream(InputStream in)创建一个新的 ZipInputStream用于从指定的输入流中读取数据。
代码示例
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;public class ZipStreamDemo {public static void main(String[] args) throws IOException {// 创建一个 File 表示要解压的压缩包File src new File(C:\\aaa.zip);// 创建一个 File 表示解压的目的地File dest new File(C:\\);unzip(src,dest);}//定义一个方法来解压文件public static void unzip(File src,File dest) throws IOException {// 解压本质把压缩包里面的每一个文件或者文件夹读取出来按照层级拷贝到目的地当中// 创建一个解压缩流用来读取压缩包中的数据ZipInputStream zip new ZipInputStream(new FileInputStream(src));// 先获取到压缩包里面的每一个 zipentry 对象ZipEntry entry;while((entry zip.getNextEntry()) ! null) {System.out.println(entry);if(entry.isDirectory()) {// 文件夹需要在目的地 dest 处创建一个同样的文件夹File file new File(dest,entry.toString());file.mkdirs();}else {// 文件需要读取到压缩包中的文件并把它存放到目的地 dest 文件夹中按照层级目录进行存放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();}
}压缩流
ZipOutputStream 用于将数据压缩成 ZIP 格式它继承自 DeflaterOutputStream。
压缩本质把每一个文件或者文件夹看成一个 ZipEntry 对象放到压缩包中
构造方法ZipOutputStream(OutputStream out)创建一个新的 ZipOutputStream将压缩数据写入指定的输出流。
代码示例
压缩一个文件
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class ZipStreamDemo {public static void main(String[] args) throws IOException {// 创建 File 对象表示要压缩的文件File src new File(C:\\aaa.txt);// 创建 File 对象表示压缩包的位置File dest new File(C:\\);toZip(src,dest);}/** 作用压缩 * 参数一表示要压缩的文件* 参数二表示压缩包的位置* */public static void toZip(File src,File dest) throws IOException {// 创建压缩流关联压缩包ZipOutputStream zos new ZipOutputStream(new FileOutputStream(new File(dest,aaa.zip)));// 创建 ZipEntry 对象表示压缩包里的每一个文件和文件夹ZipEntry entry new ZipEntry(aaa.txt);//把 ZipEntry 对象放到压缩包里zos.putNextEntry(entry);// 把 src 中的数据写到压缩包中FileInputStream fis new FileInputStream(src);int b;while((b fis.read()) ! -1) {zos.write(b);}zos.closeEntry();zos.close();}
}压缩一个文件夹
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class ZipStreamDemo {public static void main(String[] args) throws IOException {// 创建 File 对象表示要压缩的文件File src new File(C:\\aaa);// 创建 File 对象表示压缩包放在哪里压缩包的父路径File destParent src.getParentFile();// 创建 File 对象表示压缩包的位置File dest new File(destParent,src.getName() .zip);// 创建压缩流关联压缩包ZipOutputStream zos new ZipOutputStream(new FileOutputStream(dest));toZip(src,zos,src.getName());zos.close();}/** 作用获取 src 里面的每一个文件变成 ZipEntry 对象放入压缩包中* 参数一数据源* 参数二压缩流* 参数三压缩包内部的路径* */public static void toZip(File src,ZipOutputStream zos,String name) throws IOException {//1.进入src文件夹File[] files src.listFiles();//2.遍历数组for (File file : files) {if(file.isFile()){//3.判断-文件变成ZipEntry对象放入到压缩包当中ZipEntry entry new ZipEntry(name \\ file.getName());zos.putNextEntry(entry);//读取文件中的数据写到压缩包FileInputStream fis new FileInputStream(file);int b;while((b fis.read()) ! -1){zos.write(b);}fis.close();zos.closeEntry();}else{//4.判断-文件夹递归toZip(file,zos, name \\ file.getName());}}}
}Commons-io 工具包
概述
Commons-io 是 apache 开源基金组织提供的一组有关 IO 操作的开源工具包
作用提高 IO 流的开发效率
Commons-io 的工具包是以 “.jar” 为后缀所以也叫 jar 包
Commons-io 使用步骤
在项目中创建名为 “lib” 的文件夹。把 jar 包复制粘贴到 “lib” 文件夹。右键点击 jar 包选择 “Add as Library”然后点击 “OK” 。在类中导包使用。
Commons-io 常见方法
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)写出数据
IOUtils 类流相关说明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)写出数据
代码示例
import java.io.File;
import java.io.IOException;import org.apache.commons.io.FileUtils;public class commonsio {public static void main(String[] args) throws IOException {File src new File(C:\\aaa.txt);File dest new File(C:\\copy.txt);FileUtils.copyFile(src, dest);// 将原文件夹内的内容全部复制到新文件夹内File src new File(C:\\aaa); File dest new File(C:\\bbb);FileUtils.copyDirectory(src, dest);// 将原文件夹直接复制到新文件夹内File src new File(C:\\aaa); File dest new File(C:\\bbb);FileUtils.copyDirectoryToDirectory(src, dest);}
}关于 Commons-io 工具包的更多使用方法可以前往 【Apache Commons IO 工具包常用方法】
Hutool 工具包
Hutool 是一个开源的全能型 Java 工具类库旨在简化 Java 开发中的常见操作被称为 Java 开发的 “瑞士军刀”。以下是关于它的详细介绍
特点 模块化设计被拆分成多个模块如 core、crypto、http 等开发者可按需引入避免项目依赖臃肿。简单易用相比于 Java 原生工具类其 API 更加简洁能大幅减少开发者编写的代码量降低开发成本。功能全面涵盖字符串、日期、文件操作、加密、HTTP 请求等开发中几乎所有常见的工具类操作减少开发者重复造轮子。 常见工具类及功能 文件操作FileUtil 类可简化文件的读写、压缩解压、目录管理等操作如一行代码读取文件内容或递归遍历目录。日期处理DateUtil 模块提供丰富的日期工具方法可进行日期格式化、时间差计算、获取当前时间等操作 。字符串处理StrUtil 类让字符串判断、拼接、分割、过滤等操作变得简洁高效。网络请求HttpUtil 类支持快速发起 HTTP 请求支持 GET、POST 等常见操作。加密解密SecureUtil 模块提供常见的加密解密算法如 MD5、AES、RSA 等满足处理用户密码或敏感数据时的安全需求。 应用场景适用于企业级项目、微服务开发、第三方接口调用等多种场景无论是数据处理、文件管理还是网络交互等方面都能发挥作用。与其他工具库对比和主要竞争对手 Apache Commons、Guava 相比Hutool 功能覆盖范围最广API 设计简洁使用体验佳社区较为活跃更新频繁。不过Apache Commons 模块化明确Guava 在集合处理和函数式编程上表现出色且这两者更新速度虽慢但稳定性更好。使用注意事项使用时需按需引入模块由于版本更新较快升级时要注意 API 的兼容性部分复杂操作可能会带来一定性能开销要根据实际场景权衡在关键逻辑中建议掌握 Java 原生工具类避免过度依赖。
关于 Hutool 工具包的更多使用方法可以前往 Hutool 官方 进行学习。 文章转载自: http://www.morning.gbxxh.cn.gov.cn.gbxxh.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.ltpmy.cn.gov.cn.ltpmy.cn http://www.morning.wwgpy.cn.gov.cn.wwgpy.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.chzqy.cn.gov.cn.chzqy.cn http://www.morning.yrjym.cn.gov.cn.yrjym.cn http://www.morning.lnyds.cn.gov.cn.lnyds.cn http://www.morning.zqwqy.cn.gov.cn.zqwqy.cn http://www.morning.pmmrb.cn.gov.cn.pmmrb.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.rmdsd.cn.gov.cn.rmdsd.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.stpkz.cn.gov.cn.stpkz.cn http://www.morning.dfygx.cn.gov.cn.dfygx.cn http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.dfkby.cn.gov.cn.dfkby.cn http://www.morning.xprq.cn.gov.cn.xprq.cn http://www.morning.pcrzf.cn.gov.cn.pcrzf.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.ybhrb.cn.gov.cn.ybhrb.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.mlcnh.cn.gov.cn.mlcnh.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.lmxzw.cn.gov.cn.lmxzw.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.dsncg.cn.gov.cn.dsncg.cn http://www.morning.ykrkq.cn.gov.cn.ykrkq.cn http://www.morning.rtkgc.cn.gov.cn.rtkgc.cn http://www.morning.datadragon-auh.cn.gov.cn.datadragon-auh.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.wlddq.cn.gov.cn.wlddq.cn http://www.morning.mcbqq.cn.gov.cn.mcbqq.cn http://www.morning.rgpy.cn.gov.cn.rgpy.cn http://www.morning.mjbjq.cn.gov.cn.mjbjq.cn http://www.morning.fpzpb.cn.gov.cn.fpzpb.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.tsnq.cn.gov.cn.tsnq.cn http://www.morning.ejknty.cn.gov.cn.ejknty.cn http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.ghkgl.cn.gov.cn.ghkgl.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.rgpsq.cn.gov.cn.rgpsq.cn http://www.morning.tpdg.cn.gov.cn.tpdg.cn http://www.morning.dbsch.cn.gov.cn.dbsch.cn http://www.morning.yhyqg.cn.gov.cn.yhyqg.cn http://www.morning.rzysq.cn.gov.cn.rzysq.cn http://www.morning.fnjrh.cn.gov.cn.fnjrh.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.wzdjl.cn.gov.cn.wzdjl.cn http://www.morning.jrlxz.cn.gov.cn.jrlxz.cn http://www.morning.hhpkb.cn.gov.cn.hhpkb.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.wrtpk.cn.gov.cn.wrtpk.cn http://www.morning.ljwyc.cn.gov.cn.ljwyc.cn http://www.morning.hqbk.cn.gov.cn.hqbk.cn http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.lsnhs.cn.gov.cn.lsnhs.cn http://www.morning.mspkz.cn.gov.cn.mspkz.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.mlnbd.cn.gov.cn.mlnbd.cn http://www.morning.brcdf.cn.gov.cn.brcdf.cn http://www.morning.dqkrf.cn.gov.cn.dqkrf.cn http://www.morning.xmtzk.cn.gov.cn.xmtzk.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.rckmz.cn.gov.cn.rckmz.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.qrzqd.cn.gov.cn.qrzqd.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.hcqd.cn.gov.cn.hcqd.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn