ftp下载,TXT文件操作相关

/**
     * 获取文件创建最后修改时间
     * @param folderPath
     * @return
     */
    public static String getFolderCreateDate(String folderPath){
        String cTime="文件夹不存在";
        boolean isExists=false;
        File file=new File(folderPath);
        if(file.isDirectory() && file.exists()){
            isExists=true;
        }
        if(isExists){
            long time=file.lastModified();//返回文件最后修改时间
            cTime =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(time));
        }
        return cTime;

    }

/**
    * Description: 从FTP服务器下载文件
    * @Version1.0 Jul 27, 2008 5:32:36 PM by 崔红保(cuihongbao@d-heaven.com)创建
    * @param remotePath FTP服务器上的相对路径
    * @param fileName 要下载的文件名
    * @param localPath 下载后保存到本地的路径
    * @return  如果下载失败则返回默认值  如果下载成功,则返回下载文件的路径和文件名
    */
    public static String downFile(String remotePath,String fileName,String localPath) {
//        boolean success = false;
        String success="!@#$%^&*()";
        FTPClient ftp = new FTPClient();
        try {
            int reply;
            ftp.connect(url, port);
            //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
            ftp.login(username, password);//登录
            reply = ftp.getReplyCode();
            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                return success;
            }
            ftp.changeWorkingDirectory(gameName+"\\"+remotePath);//转移到FTP服务器目录
            FTPFile[] fs = ftp.listFiles();
            for(FTPFile ff:fs){
                if(ff.getName().equals(fileName)){
                    File localFile = new File(localPath+"/"+ff.getName());
                     
                    OutputStream is = new FileOutputStream(localFile);  
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }
             
            ftp.logout();
            File file=new File(localPath+"\\"+remotePath);
            if(!file.exists() && !file.isDirectory()){
                file.mkdirs();
            }
            success = localPath+"\\"+remotePath+"\\"+fileName;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftp.isConnected()) {
                try {
                    ftp.disconnect();
                } catch (IOException ioe) {
                }
            }
        }
        return success;
    }


/**
     *
     * @param filePath  文件路径
     * @param content  写入内容
     * @throws Exception
     */
    public static void writeTxt(String filePath, String content) throws Exception{
        FileWriter writer;
        try {
            writer = new FileWriter(filePath);
            writer.write(content);
            writer.flush();
            writer.close();
        } catch (Exception e) {
            System.out.println("writeTxt() Exception:" + e.getMessage());
            throw e;
        }
    }



/**    
     * 复制单个文件    
     * @param oldPath String 原文件路径 如:c:/fqf.txt    
     * @param newPath String 复制后路径 如:f:/fqf.txt    
     * @return boolean    
     */     
    public static void copyFile(String oldPath, String newPath) throws Exception{
       try {     
           int bytesum = 0;     
           int byteread = 0;     
           File oldfile = new File(oldPath);     
           if (oldfile.exists()) { //文件存在时     
               InputStream inStream = new FileInputStream(oldPath); //读入原文件
               FileOutputStream fs = new FileOutputStream(newPath);           
               byte[] buffer = new byte[1444];     
               while ( (byteread = inStream.read(buffer)) != -1) {     
                   bytesum += byteread; //字节数 文件大小     
                   fs.write(buffer, 0, byteread);     
               }     
               fs.close();
               inStream.close();     
           }     
       } catch (Exception e) {     
            System.out.println("copyFile() Exception:" + e.getMessage());
            throw e;
       }     
   }

   /**    
     * 删除文件    
     * @param filePathAndName String 文件路径及名称 如c:/fqf.txt    
     * @param fileContent String    
     * @return boolean    
     */     
    public static void delFile(String filePathAndName) throws Exception{
        try {     
            String filePath = filePathAndName;     
            filePath = filePath.toString();     
            java.io.File myDelFile = new java.io.File(filePath);     
            myDelFile.delete();     
        } catch (Exception e) {     
            System.out.println("delFile() Exception:" + e.getMessage());
            throw e;    
        }     
    }

    /**    
     * 移动文件到指定目录    
     * @param oldPath String 如:c:/fqf.txt    
     * @param newPath String 如:d:/fqf.txt    
     */     
    public static void moveFile(String oldPath, String newPath) throws Exception{
        try {
            copyFile(oldPath, newPath);     
            delFile(oldPath);
        } catch (Exception e) {     
            System.out.println("moveFile() Exception:" + e.getMessage());
            throw e;    
        }     
    }
  

//取得文件的创建时间
    public static Date getFileCreateDate(File _file) {
        File file = _file;
        try {
            //java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令。
            //cmd /c dir 是执行完dir命令后关闭命令窗口。
            Process ls_proc = Runtime.getRuntime().exec("cmd.exe /c dir " + file.getAbsolutePath() + " /tc");//getAbsolutePath():返回抽象路径名的绝对路径名字符串。
            BufferedReader br = new BufferedReader(new InputStreamReader(ls_proc.getInputStream()));
            for (int i = 0; i < 5; i++) {
                br.readLine();
            }
            String stuff = br.readLine();
            StringTokenizer st = new StringTokenizer(stuff);
            String dateC = st.nextToken();
            String time = st.nextToken();
            String datetime = dateC.replaceAll("/", "-").concat(" " + time + ":00");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sdf.parse(datetime);
            br.close();
            return date;
        } catch (Exception e) {
            return null;
        }
    }



 /**
     * 判断该文件是否存在
     * @param filePath
     * @return
     */
    public static boolean checkFile(String filePath){     
        String encoding=requestEncoding;//编码格式  GBK
        File file=new File(filePath);
        if(file.exists()){ //判断文件是否存在
            return true;
        }else{
            return false;
        }
    }


    /**
     * 功能:Java读取txt文件的内容
     * 步骤:1:先获得文件句柄
     * 2:获得文件句柄当做是输入一个字节码流,需要对这个输入流进行读取
     * 3:读取到输入流后,需要读取生成字节流
     * 4:一行一行的输出。readline()。
     * 备注:需要考虑的是异常情况
     * @param filePath
     */
    public static String readTxtFile(String filePath){
        String str="";
        try {
                String encoding=requestEncoding;//文件编码 'GBK'
                File file=new File(filePath);
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                int i=0;
                while((lineTxt = bufferedReader.readLine()) != null){
//                  System.out.println(lineTxt);
                    if(i==0){
                        str=lineTxt;
                    }else{
                        str=str+"~!@"+lineTxt; //每行文字以特殊字符"~!@"  进行间隔,方便后续获取字符串后进行操作。
                    }
                    i++;
                }
                read.close();
                return str;
                
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        return str;
     
    }



/**
     * 获取目录下的所有文件夹名称
     * @param path
     * @return
     */
    public static String[] getFolderNames(String path){
        File file=new File(path);
        File fa[]=file.listFiles();
        String[] folderNames=new String[fa.length];
        for(int i=0;i<fa.length;i++){
            File fs=fa[i];
            if(fs.isDirectory() && !fs.isFile()){
                    folderNames[i]=fs.getName();
            }
        }
        
        return folderNames;
    }
    
    /**
     * 获取目录下的所有文件夹数目
     * @param path
     * @return
     */
    public static int getFolderNumber(String path){
        int num=0;
        File file=new File(path);
        File fa[]=file.listFiles();
        String[] folderNames=new String[fa.length];
        for(int i=0;i<fa.length;i++){
            File fs=fa[i];
            if(fs.isDirectory()){
                folderNames[i]=fs.getName();
                num=num+1;
            }
        }
        
        return num;
    }
    /**
     * 获取目录下的所有文件名称
     * @param path
     * @return
     */
    public static String[] getFileNames(String path){
        File file=new File(path);
        File fa[]=file.listFiles();
        String[] fileNames=new String[fa.length];
        for(int i=0;i<fa.length;i++){
            File fs=fa[i];
            if(fs.isFile()){
                fileNames[i]=fs.getName();
            }
        }
        
        return fileNames;
    }
    /**
     * 火纹目录下的文件数目
     * @param path
     * @return
     */
    public static int  getFileNumber(String path){
        int num=0;
        File file=new File(path);
        File fa[]=file.listFiles();
        String[] fileNames=new String[fa.length];
        for(int i=0;i<fa.length;i++){
            File fs=fa[i];
            if(fs.isFile()){
                fileNames[i]=fs.getName();
                num=num+1;
            }
        }
        
        return num;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值