`
andrew.yulong
  • 浏览: 166710 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

IO操作(已封装)--java

    博客分类:
  • java
阅读更多

和大家见面了,本人言语突然很少,开门见山吧

package  cn.vetech.framework.util;

import  java.io.BufferedReader;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.IOException;
import  java.io.InputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStream;
import  java.io.PrintWriter;

import  org.apache.struts.upload.FormFile;

import  cn.vetech.framework.source.Constants;

/**
 * 用户文件的 读 写 目录的创建等
 * 
 * 
@author  zl
 * 
 
*/
public   class  FileOperate {
    
private  String message;

    
private  String FILESPARA  =   " / " ;

    
private  String spath  =  Constants.APPPRTH;

    
public  FileOperate() {
    }

    
/**
     * 强制用UTF-8 编码读 整个系统编码采用utf-8
     * 
     * 
@param  filePathAndName
     * 
@return
     
*/
    
public  String readTxt(String filePathAndName) {
        
return  readTxt(filePathAndName,  " UTF-8 " );
    }

    
/**
     * 读取文本文件内容
     * 
     
*/
    
public  String readTxt(String filePathAndName, String encoding) {
        encoding 
=  encoding.trim();
        StringBuffer str 
=   new  StringBuffer( "" );
        String st 
=   "" ;
        
try  {
            FileInputStream fs 
=   new  FileInputStream(filePathAndName);
            InputStreamReader isr;
            
if  (encoding.equals( "" )) {
                isr 
=   new  InputStreamReader(fs);
            } 
else  {
                isr 
=   new  InputStreamReader(fs, encoding);
            }
            BufferedReader br 
=   new  BufferedReader(isr);
            
try  {
                String data 
=   "" ;
                
while  ((data  =  br.readLine())  !=   null ) {
                    str.append(data 
+   "   " );
                }
            } 
catch  (Exception e) {
                str.append(e.toString());
            }
            st 
=  str.toString();
        } 
catch  (IOException es) {
            st 
=   "" ;
        }
        
return  st;
    }

    
/**
     * 强制用UTF-8 编码保存 避免因为操作系统不同而编码不同。
     * 
     * 
@param  path
     * 
@param  filename
     * 
@param  fileContent
     
*/
    
public   void  writeTxt(String path, String filename, String fileContent) {
        writeTxt(path, filename, fileContent, 
" UTF-8 " );

    }

    
/**
     * 有编码方式的文件创建
     
*/
    
public   void  writeTxt(String path, String filename, String fileContent, String encoding) {

        
try  {
            File file 
=   new  File(path);
            
if  ( ! file.exists()) {
                file.mkdirs();
            }
            file 
=   new  File(path  +  FILESPARA  +  filename);
            PrintWriter pwrite 
=   null ;
            
if  (encoding  !=   null   &&   ! "" .equals(encoding)) {
                pwrite 
=   new  PrintWriter(file, encoding);
            } 
else  {
                pwrite 
=   new  PrintWriter(file);
            }
            pwrite.println(fileContent);
            pwrite.close();
        } 
catch  (Exception e) {
            e.printStackTrace();
        }
    }

    
/**
     * 拷贝目录树 把一个目录下的所有东西包括子目录 同时拷贝到 另外一个目录
     * 
     * 
@param  sourceDir
     * 
@param  targetDir
     * 
@throws  Exception
     
*/
    
public   void  copyDir(String sourceDir, String targetDir)  throws  Exception {
        String url1 
=  sourceDir;
        String url2 
=  targetDir;
        
if  ( ! ( new  File(url2)).exists()) {
            (
new  File(url2)).mkdirs();
        }
        File[] file 
=  ( new  File(url1)).listFiles();
        
for  ( int  i  =   0 ; i  <  file.length; i ++ ) {
            
if  (file[i].isFile()) {
                file[i].toString();
                FileInputStream input 
=   new  FileInputStream(file[i]);
                FileOutputStream output 
=   new  FileOutputStream(url2  +  System.getProperty( " file.separator " )
                        
+  (file[i].getName()).toString());
                
byte [] b  =   new   byte [ 1024   *   5 ];
                
int  len;
                
while  ((len  =  input.read(b))  !=   - 1 ) {
                    output.write(b, 
0 , len);
                }
                output.flush();
                output.close();
                input.close();
            } 
else   if  (file[i].isDirectory()) {
                String url_2_dir 
=  url2  +  System.getProperty( " file.separator " +  file[i].getName();
                copyDir(file[i].getPath(), url_2_dir);
            }
        }
    }

    
/**
     * 新建目录
     * 
     * 
@param  folderPath
     *            目录
     * 
@return  返回目录创建后的路径
     
*/
    
public  String createFolder(String folderPath) {
        String txt 
=  folderPath;
        
try  {
            java.io.File myFilePath 
=   new  java.io.File(txt);
            txt 
=  folderPath;
            
if  ( ! myFilePath.exists()) {
                myFilePath.mkdirs();
            }
        } 
catch  (Exception e) {
            message 
=   " 创建目录操作出错 " ;
        }
        
return  txt;
    }

    
/**
     * 删除文件
     * 
     * 
@param  filePathAndName
     *            文本文件完整绝对路径及文件名
     * 
@return  Boolean 成功删除返回true遭遇异常返回false
     
*/
    
public   boolean  delFile(String filePathAndName) {
        
boolean  bea  =   false ;
        
try  {
            String filePath 
=  filePathAndName;
            File myDelFile 
=   new  File(filePath);
            
if  (myDelFile.exists()) {
                myDelFile.delete();
                bea 
=   true ;
            } 
else  {
                bea 
=   false ;
                message 
=  (filePathAndName  +   " <br>删除文件操作出错 " );
            }
        } 
catch  (Exception e) {
            message 
=  e.toString();
        }
        
return  bea;
    }

    
/**
     * 删除文件夹
     * 
     * 
@param  folderPath
     *            文件夹完整绝对路径
     * 
@return
     
*/
    
public   void  delFolder(String folderPath) {
        
try  {
            delAllFile(folderPath); 
//  删除完里面所有内容
            String filePath  =  folderPath;
            filePath 
=  filePath.toString();
            java.io.File myFilePath 
=   new  java.io.File(filePath);
            myFilePath.delete(); 
//  删除空文件夹
        }  catch  (Exception e) {
            message 
=  ( " 删除文件夹操作出错 " );
        }
    }

    
/**
     * 删除指定文件夹下所有文件
     * 
     * 
@param  path
     *            文件夹完整绝对路径
     * 
@return
     * 
@return
     
*/
    
public   boolean  delAllFile(String path) {
        
boolean  bea  =   false ;
        File file 
=   new  File(path);
        
if  ( ! file.exists()) {
            
return  bea;
        }
        
if  ( ! file.isDirectory()) {
            
return  bea;
        }
        String[] tempList 
=  file.list();
        File temp 
=   null ;
        
for  ( int  i  =   0 ; i  <  tempList.length; i ++ ) {
            
if  (path.endsWith(File.separator)) {
                temp 
=   new  File(path  +  tempList[i]);
            } 
else  {
                temp 
=   new  File(path  +  File.separator  +  tempList[i]);
            }
            
if  (temp.isFile()) {
                temp.delete();
            }
            
if  (temp.isDirectory()) {
                delAllFile(path 
+   " / "   +  tempList[i]); //  先删除文件夹里面的文件
                delFolder(path  +   " / "   +  tempList[i]); //  再删除空文件夹
                bea  =   true ;
            }
        }
        
return  bea;
    }

    
/**
     * 复制单个文件
     * 
     * 
@param  oldPathFile
     *            准备复制的文件源
     * 
@param  newPathFile
     *            拷贝到新绝对路径带文件名
     * 
@return
     
*/
    
public   void  copyFile(String oldPathFile, String newPathFile) {
        
try  {
            
int  bytesum  =   0 ;
            
int  byteread  =   0 ;
            File oldfile 
=   new  File(oldPathFile);
            
if  (oldfile.exists()) {  //  文件存在时
                InputStream inStream  =   new  FileInputStream(oldPathFile);  //  读入原文件
                FileOutputStream fs  =   new  FileOutputStream(newPathFile);
                
byte [] buffer  =   new   byte [ 1444 ];
                
while  ((byteread  =  inStream.read(buffer))  !=   - 1 ) {
                    bytesum 
+=  byteread;  //  字节数 文件大小
                    
//   // System.out.println(bytesum);
                    fs.write(buffer,  0 , byteread);
                }
                inStream.close();
            }
        } 
catch  (Exception e) {
            message 
=  ( " 复制单个文件操作出错 " );
        }
    }

    
/**
     * 移动文件
     * 
     * 
@param  oldPath
     * 
@param  newPath
     * 
@return
     
*/
    
public   void  moveFile(String oldPath, String newPath) {
        copyFile(oldPath, newPath);
        delFile(oldPath);
    }

    
/**
     * 移动目录
     * 
     * 
@param  oldPath
     * 
@param  newPath
     * 
@return
     * 
@throws  Exception
     
*/
    
public   void  moveFolder(String sourceDir, String targetDir)  throws  Exception {
        copyDir(sourceDir, targetDir);
        delFolder(sourceDir);
    }

    
public  String getMessage() {
        
return   this .message;
    }

    
/**
     * ybfjm@163.com 扩充
     
*/
    
/**  以下是保存文件,读取文件需要用到的方法*  */
    
public  String getStringFilePath(String id) {
        String strPath 
=   "" ;
        
//  生成一个目录,生成年度、月份目录
        String sfolder_year  =  id.substring( 0 4 );
        String sfolder_month 
=  id.substring( 4 6 );
        strPath 
=  spath  +   " /updownFiles/ "   +  sfolder_year  +  FILESPARA  +  sfolder_month  +  FILESPARA;
        
return  strPath;
    }

    
public  String getFileByPathId(String pathFile) {
        File myFile 
=   new  File(pathFile);
        
if  (myFile.exists())
            
return   new  FileOperate().readTxt(pathFile);
        
else
            
return   "" ;
    }

    
public   void  saveFileByPathId(String path, String id, String nr) {
        
new  FileOperate().writeTxt(path, id  +   " .txt " , nr);
    }

    
public   void  delFileByPathId(String pathFile) {
        File myFile 
=   new  File(pathFile);
        
if  (myFile.exists())
            
new  FileOperate().delFile(pathFile);
    }

    
/*
     * 用于上传文件保存 如果saveFileName 为空那么将以FormFile 文件名保存 不包括扩展名,扩展名自动引用 返回保存的文件名
     * 如果返回为空表示有错误
     
*/
    
public  String uploadFileSave(FormFile file, String savePath, String saveFileName) {
        String slowdamc 
=   "" ;
        
if  (file  ==   null ) {
            
return   "" ;
        }
        
try  {
            InputStream stream 
=  file.getInputStream(); //  把文件读入
        
//     ByteArrayOutputStream baos = new ByteArrayOutputStream();
            String fileName  =  file.getFileName().toLowerCase();
            
if  (fileName  ==   null   ||   "" .equals(fileName)) {
                
return   "" ;
            }
            
if  (saveFileName  ==   null   ||   "" .equals(saveFileName)) {
                slowdamc 
=  fileName;
            } 
else  {
                
if  (fileName.indexOf( " . " !=   - 1 ) {
                    slowdamc 
=  saveFileName  +   " . "   +  getFileExt(fileName);
                } 
else  {
                    slowdamc 
=  saveFileName;
                }

            }
    
//         long isize = file.getFileSize();
            
//  如果目录不存在就创建目录
            createFo
分享到:
评论

相关推荐

    Java IO commons-io-2.5.jar

    Java IO commons-io-2.5.jar。 commons-io-2.5.jar 是Java IO的增强版,功能很强大。里面封装了很多实用方便的函数,文件操作、目录操作的。 Java IO

    java工具包封装对xml的操作javamail,翻译,io操作

    封装javamail、利用谷歌在线翻译编写的jar包、 封装文件IO操作、封装dom4j,进行xml文件的操作。 压缩包内附doc文档,容易操作。

    Java-package-commons-io-2.11.0

    Java commons-io-2.11.0,第三方封装用于处理文件IO读写。 Java原生的读写处理并没有那么方便,使用上需要开发者自己注意和手写处理许多问题。 commons-io的封装可以屏蔽这些繁杂的处理,让开发者开箱即用。 注意本...

    JDBC操作封装 IO流操作封装 CRC效验码生成

    连接池的初始化、创建等仔细看构造函数的doc文档,就知道如何操作。我自己用着很方便。 有两个初始化方法,分别用于初始化多个数据库对象,或者单个数据库对象。 String[] dbPoolName = { "default", "sec" }; ...

    自己封装的IO核NIO

    封装了JavaIO和NIO的接口,使得变得简单易用,在这基础上封装了一个HTTP客户端,使发送Http报文变得容易,支持http和https,用来做微信的开发最适合不过来

    JAVA基础教程

    第 1 部分 Java基础程序设计 •Java语言介绍 •简单的 Java 程序 ...--第 10 章 文件(IO)操作 --第 11 章 Java Applet 程序 --第 12 章 Java 常用类库 --第十三章 Java 网络程序设计 附录 JAVA定义格式

    java io 实例工具(源码)

    封装好的java io实例,简单,可做学习参考,也可用于工具调用!

    rancher-java-sdk:自动生成Rancher API的客户端

    您可以使用入项目中 repositories { maven { url " https://jitpack.io " }}dependencies { compile " com.github.objectpartners:rancher-java-sdk:master-SNAPSHOT "}例子该库的入口点是io.rancher.Rancher类。...

    commons-io-2.5.jar

    commons-io-2.5.jar 是ava IO的增强版,功能很强大。里面封装了很多实用方便的函数,文件操作、目录操作的。

    dmhub-java-sdk:DM Hub OpenAPI 的 Java 封装

    dmhub-java-sdkDM Hub OpenAPI 的 Java 封装如何使用Gradlerepositories { jcenter() maven { url 'https://jitpack.io' }}将 加入仓库列表。dependencies { ... implementation '...

    commons-io-1.4.jar等

    JAVA连接FTP服务器,并上传/下载文件的,使用commons-net包实现ftp服务器的访问,commons-net包封装了一些常见的网络包:ftp,smtp,pop3等..相关包:commons-net-1.4.1.jar ; commons-io-1.4.jar;jakarta-oro-2.0.8.jar

    commons-io-2.4.jar

    项目开发中经常使用的工具包commons-io-2.4.jar,功能很...里面封装了很多实用方便的函数,文件操作、目录操作的。另外Andriod的HttpClient并不直接支持multipart,我们需要额外的jar来支持,这是其中重要的一个jar包。

    java web 及 IO 的一些代码

    几个小文件,都是一些代码,很基础的东西,刚学java的可以看看,里面还有个对IO的封装

    封装java常用工具的类

    封装java常用的类,其中有邮件发送实现带jsp,xml,导出excel,加密,时间类,数据库连接池,验证码,gui,io,SQLserver,StringUtil等等等

    java资料---Java SE.ppt

    Java发展史 面向对象基础 面向对象三大特征(封装、继承和多态) static和final 抽象类和接口 异常处理 集合框架 IO 并发编程 Java SE5中其它的重要新特性

    java用poi解析excel2003和2007并封装成对象返回

    NULL 博文链接:https://skyfar666.iteye.com/blog/1922052

    Java SE编程入门教程 java IO(共28页).pptx

    Java SE编程入门教程 java IO(共28页).pptx Java SE编程入门教程 java Math(共11页).pptx Java SE编程入门教程 java object(共9页).pptx Java SE编程入门教程 java static final(共24页).pptx Java SE编程...

    commons-io-2.6.zip jar包

    包封装。能够让JAR包里面的文件依赖于统一版本的类文件。 可移植性,能够在各种平台上直接使用。 把一个JAR文件添加到系统的classpath环境变量之后,java通常会把这个JAR文件当做一个路径来处理。通常使用jar命令...

    Java SE编程入门教程 java封装继承多态(共56页).pptx

    Java SE编程入门教程 java IO(共28页).pptx Java SE编程入门教程 java Math(共11页).pptx Java SE编程入门教程 java object(共9页).pptx Java SE编程入门教程 java static final(共24页).pptx Java SE编程...

    java从入门到精通视频教程(全94讲)学习笔记整理(齐全).docx

    -类方法、封装 -访问修饰符.重载.覆盖 -约瑟夫问题 -多态 -抽象类、接口 -final.作业评讲 -作业、测试题 -数组 -排序、查找 -二进制、位运算、位移运算 -集合 -集合 -集合 -集合补充 -异常 -面试题评讲 -布局管理器 ...

Global site tag (gtag.js) - Google Analytics