zoukankan      html  css  js  c++  java
  • 爪哇国新游记之三十三----目录文件操作

    1.判断路径是文件还是目录

    File subDir=new File("c:\mp3");
    if(subDir.isDirectory()){
       // 是目录
    }
    
    File mp3=new File("c:\mp3\avemaria.mp3");
                        
    if(mp3.isFile()){
       // 是文件
    }

    2.列出目录下的文件和子目录

    File dir = new File(fromDir);
    String[] children = dir.list();
    
    for (int i=0; i<children.length; i++) {
        String filename = children[i];
        ...
    }

    3.文件拷贝

    public static void copyFile(File sourceFile, File targetFile) throws IOException {
            BufferedInputStream inBuff = null;
            BufferedOutputStream outBuff = null;
            try {
                // 新建文件输入流并对它进行缓冲
                inBuff = new BufferedInputStream(new FileInputStream(sourceFile));
    
                // 新建文件输出流并对它进行缓冲
                outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));
    
                // 缓冲数组
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = inBuff.read(b)) != -1) {
                    outBuff.write(b, 0, len);
                }
                // 刷新此缓冲的输出流
                outBuff.flush();
            } finally {
                // 关闭流
                if (inBuff != null)
                    inBuff.close();
                if (outBuff != null)
                    outBuff.close();
            }
        }

    2016年8月26日23:39:27改版

    /**
         * 将文件上传到服务器,返回在服务器的路径文件名
         * @param in
         * @param filename
         * @return
         * @throws Exception
         */
        public String upload2Server(InputStream in, String filename) throws Exception {
            BufferedInputStream inBuff = null;
            BufferedOutputStream outBuff = null;
            try {
                // 新建文件输入流并对它进行缓冲
                inBuff = new BufferedInputStream(in);
    
                // 新建文件输出流并对它进行缓冲
                String filePathname=uploadPath+getTimePrefix()+filename;
                outBuff = new BufferedOutputStream(new FileOutputStream(new File(filePathname)));
    
                // 缓冲数组
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = inBuff.read(b)) != -1) {
                    outBuff.write(b, 0, len);
                }
                // 刷新此缓冲的输出流
                outBuff.flush();
                
                return filePathname;
            } catch(Exception ex){
                logger.error(ex);
                throw ex;
            }
            finally {
                // 关闭流
                if (inBuff != null)
                    inBuff.close();
                if (outBuff != null)
                    outBuff.close();
            }
        }

    4.取得操作系统的临时目录

    String folder=System.getProperty("java.io.tmpdir");
  • 相关阅读:
    什么是缓存?
    什么是反射?
    Nginx配置文件详解
    数据库插入,修改出现中文乱码解决办法
    hadoop的HA机制+zookeeper
    cascading--wordcount
    心情3
    完成用户登录注册功能
    感慨1
    Swing程序设计-初级
  • 原文地址:https://www.cnblogs.com/heyang78/p/4195966.html
Copyright © 2011-2022 走看看