平时我们在控制台打印输出,是调用print方法和println方法完成的,这两个方法都来自于java.io.PrintStream类,该类能够方便地打印各种数据类型的值,是一种便捷的输出方式。
public PrintStream(String fileName): 使用指定的文件名创建一个新的打印流。
构造举例,代码如下:
PrintStream ps = new PrintStream("ps.txt");
System.out就是PrintStream类型的,只不过它的流向是系统规定的,打印在控制台上。不过,既然是流对象,我们就可以玩一个"小把戏",改变它的流向。
- public class PrintDemo {
- public static void main(String[] args) throws IOException {
- // 调用系统的打印流,控制台直接输出97
- System.out.println(97);
-
- // 创建打印流,指定文件的名称
- PrintStream ps = new PrintStream("ps.txt");
-
- // 设置系统的打印流流向,输出到ps.txt
- System.setOut(ps);
- // 调用系统的打印流,ps.txt中输出97
- System.out.println(97);
- }
- }
压缩流:
负责压缩文件或者文件夹
解压缩流:
负责把压缩包中的文件和文件夹解压出来
- /*
- * 解压缩流
- *
- * */
- public class ZipStreamDemo1 {
- public static void main(String[] args) throws IOException {
-
- //1.创建一个File表示要解压的压缩包
- File src = new File("D:\\aaa.zip");
- //2.创建一个File表示解压的目的地
- File dest = new File("D:\\");
-
- //调用方法
- 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();
- }
- }
- public class ZipStreamDemo2 {
- public static void main(String[] args) throws IOException {
- /*
- * 压缩流
- * 需求:
- * 把D:\\a.txt打包成一个压缩包
- * */
- //1.创建File对象表示要压缩的文件
- File src = new File("D:\\a.txt");
- //2.创建File对象表示压缩包的位置
- File dest = new File("D:\\");
- //3.调用方法用来压缩
- toZip(src,dest);
- }
-
- /*
- * 作用:压缩
- * 参数一:表示要压缩的文件
- * 参数二:表示压缩包的位置
- * */
- public static void toZip(File src,File dest) throws IOException {
- //1.创建压缩流关联压缩包
- ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
- //2.创建ZipEntry对象,表示压缩包里面的每一个文件和文件夹
- //参数:压缩包里面的路径
- ZipEntry entry = new ZipEntry("aaa\\bbb\\a.txt");
- //3.把ZipEntry对象放到压缩包当中
- zos.putNextEntry(entry);
- //4.把src文件中的数据写到压缩包当中
- FileInputStream fis = new FileInputStream(src);
- int b;
- while((b = fis.read()) != -1){
- zos.write(b);
- }
- zos.closeEntry();
- zos.close();
- }
- }
- public class ZipStreamDemo3 {
- public static void main(String[] args) throws IOException {
- /*
- * 压缩流
- * 需求:
- * 把D:\\aaa文件夹压缩成一个压缩包
- * */
- //1.创建File对象表示要压缩的文件夹
- File src = new File("D:\\aaa");
- //2.创建File对象表示压缩包放在哪里(压缩包的父级路径)
- File destParent = src.getParentFile();//D:\\
- //3.创建File对象表示压缩包的路径
- File dest = new File(destParent,src.getName() + ".zip");
- //4.创建压缩流关联压缩包
- ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(dest));
- //5.获取src里面的每一个文件,变成ZipEntry对象,放入到压缩包当中
- toZip(src,zos,src.getName());//aaa
- //6.释放资源
- 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());//aaa\\no1\\a.txt
- 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());
- // no1 aaa \\ no1
- }
- }
- }
- }
介绍:
Commons是apache开源基金组织提供的工具包,里面有很多帮助我们提高开发效率的API
比如:
StringUtils 字符串工具类
NumberUtils 数字工具类
ArrayUtils 数组工具类
RandomUtils 随机数工具类
DateUtils 日期工具类
StopWatch 秒表工具类
ClassUtils 反射工具类
SystemUtils 系统工具类
MapUtils 集合工具类
Beanutils bean工具类
Commons-io io的工具类
等等.....
其中:Commons-io是apache开源基金组织提供的一组有关IO操作的开源工具包。
作用:提高IO流的开发效率。
使用方式:
1,新建lib文件夹
2,把第三方jar包粘贴到文件夹中
3,右键点击add as a library
代码示例:
- public class CommonsIODemo1 {
- public static void main(String[] args) throws IOException {
- /*
- 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) 写出数据
- */
-
-
- /* File src = new File("myio\\a.txt");
- File dest = new File("myio\\copy.txt");
- FileUtils.copyFile(src,dest);*/
-
-
- /*File src = new File("D:\\aaa");
- File dest = new File("D:\\bbb");
- FileUtils.copyDirectoryToDirectory(src,dest);*/
-
- /*File src = new File("D:\\bbb");
- FileUtils.cleanDirectory(src);*/
-
-
-
- }
- }