zoukankan      html  css  js  c++  java
  • 打印流,File工具类

    1  打印流

    打印流添加输出数据的功能,使它们能够方便地打印各种数据值表示形式.

    打印流根据流的分类:

    l  字节打印流  PrintStream

    l  字符打印流  PrintWriter

    l  方法:

    void print(String str): 输出任意类型的数据,

    void println(String str): 输出任意类型的数据,自动写入换行操作

    练习:打印流复制文件

    public class Demo01 {
        //打印流:没有数据源,只有数据目的
        //永远不会抛出io异常,但是可能会抛出其他异常
        //printStream
        //PrintWrite
        public static void main(String[] args) throws IOException {
            //m1();
            //m2();
            copy();
        }
        public static void m1() throws FileNotFoundException{
            PrintWriter pw=new PrintWriter("D:\test\print.txt");
            pw.print(100);
            pw.flush();
            pw.println(true);
            pw.flush();
            pw.print("aaa");
            pw.flush();
            //释放资源
            pw.close();
        }
        public static void m2() throws IOException{
            FileWriter fw=new FileWriter("D:\test\print.txt");
            //创建打印流
            PrintWriter pw=new PrintWriter(fw,true);
            pw.println("小猪");
            pw.println("佩琪");
            pw.close();
        }
        public static void copy() throws IOException{
            //明确数据源
            FileReader fr=new FileReader("D:\test\print.txt");
            //添加缓冲流
            BufferedReader br=new BufferedReader(fr);
            //明确目的地
            FileWriter fw=new FileWriter("D:\test\e\print.txt");
            //添加打印流
            PrintWriter pw=new PrintWriter(fw,true);
            //开始复制
            String line=null;
            while((line=br.readLine())!=null){
                pw.println(line);
            }
            //释放资源
            br.close();
            pw.close();
        }
    }

    2    commons-IO

    2.1  FilenameUtils

    这个工具类是用来处理文件名(译者注:包含文件路径)的,他可以轻松解决不同操作系统文件名称规范不同的问题

    l  常用方法:

    getExtension(String path):获取文件的扩展名;

    getName(String filename):获取文件名;

    isExtension(String fileName,String ext):判断fileName是否是ext后缀名;

    注意:先导包,将commons-io.jar拷贝到lib文件夹

    右键点击commons-io.jar,Build Path→Add to Build Path

    public class Demo02 {
        public static void main(String[] args) throws IOException {
            //m1();
            //m2();
            m3();
        }
        public static void m1(){
            //获取文件的扩展名
            String ext=FilenameUtils.getExtension("D:\test\a.java");
            System.out.println(ext);
            //获取文件名
            String fileName=FilenameUtils.getName("D:\test\a.java");
            System.out.println(fileName);
            //判定文件是否以java为结尾
            boolean flag=FilenameUtils.isExtension("D:\test\a.java", "java");
            System.out.println(flag);
        }
        public static void m2() throws IOException{
            //数据源
            File file=new File("D:\test\x.txt");
            //从文件中读取内容
            String content=FileUtils.readFileToString(file);
            System.out.println(content);
        }
        public static void m3() throws IOException{
            //从文件夹复制
            File f1=new File("D:\test");
            File f2=new File("F:\");
            FileUtils.copyDirectoryToDirectory(f1, f2);
        }
    }

    路径分隔符:

    public class Demo03 {
        public static void main(String[] args) {
            //路径分隔符 Windows ;   Linux   :
            System.out.println(File.pathSeparator);
            //名称分隔符 Windows          Linux  /
            System.out.println(File.separator);
        }
    }
  • 相关阅读:
    elk+redis
    elk7.4+filebeat收集日志
    k8s-高可用集群实现(keepalived+haproxy)
    k8s-高可用集群实现(keepalived)
    keepalived(双主模式)+haproxy+mysql_slave
    haproxy-实现mysql多slave读负载均衡
    MySQL数据库的配置
    前端模块化(AMD和CMD、CommonJs)
    一分钟配置jdk
    MySQL基础语法
  • 原文地址:https://www.cnblogs.com/quanjunkang/p/10656107.html
Copyright © 2011-2022 走看看