java写入文件文档工具类--覆盖、复制、加入末尾

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.znk.dao.JavaScriptServlet;

@Component
public class WriteFileUtil {
	
	//static Logger logger = Logger.getLogger(WriteFileUtil.class);
	/*
	 * 写入文档中,以覆盖形式写入
	 * encoding "UTF-8"
	 */
    public static void writeInFile(String filePath,String content,String encoding) {
    	 File file = new File(filePath);
         if (!file.exists()) {
             try {
                 file.createNewFile(); //如果文件不存在则创建文件
             } catch (IOException e) {
                 e.printStackTrace();
             }  
         }
        
         try {
        	  FileOutputStream fos = new FileOutputStream(file);
              OutputStreamWriter osw = new OutputStreamWriter(fos, encoding);
              osw.write(content);
              osw.flush();
              osw.close();
         } catch (IOException e) {
             e.printStackTrace();
         } 
	}
	/*
	 * 写入文档中,以加入原文档最末尾的形式写入
	 */
    public static void appendInFile(String filePath,String content,String encoding) {
   	 File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile(); //如果文件不存在则创建文件
                FileOutputStream fos = new FileOutputStream(file,true);
                OutputStreamWriter osw = new OutputStreamWriter(fos,encoding);
                osw.write(content);
                osw.flush();
                osw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }  
           
        }
        else {
        	 try {
        		  FileWriter writer = new FileWriter(filePath, true);
                  writer.write(content);
                  writer.close();
             } catch (IOException e) {
                 e.printStackTrace();
             } 
		}
       
      
	}
	    /*
	 *复制文件
	 */
    public static void copyFileUsingFileStreams(File src, File dest) throws IllegalAccessException, IOException {
		/*此处用来判断文件是否存在*/
    	if (src.exists() ) {
    		if(!dest.exists()) {
    			dest.createNewFile();
    		}
		/*此处用来判断文件是否是文件类型*/	
    		if (src.isFile() && dest.isFile()) {
				/*此处用来声明需要copy的文件*/
    			FileInputStream input = new FileInputStream(src);
			/*此处用来声明需要copy到的文件*/	
    			FileOutputStream out = new FileOutputStream(dest,false);
			/*此处用来定义一个缓冲区存放读取的文件*/	
    			byte buf[]=new byte[8*1024];
				int b;
			/*此处用来判断文件是否读完,注意点
            (b=input.read(buf,0,buf.length))!=-1:表示读取到buf缓冲区,从文件的0号位开始读取,最多只能暂存buf.length个字节
            */	while((b=input.read(buf,0,buf.length))!=-1) {
				/*此处用来将缓冲区的内容写入到out对应的文件*/	out.write(buf, 0, b);
					out.flush();//最好加上
					//关闭(close)输出流时,应先刷新(flush)换冲的输出流,话句话说就是:“迫使所有缓冲的输出数据被写出到底层输出流中”。
				}
				input.close();
				out.close();
			} else {
				throw new IllegalArgumentException("有一个或者多个文件文件是文件夹");
			}
		} else {
			throw new IllegalArgumentException("有一个或者多个文件文件不存在");
		}

	}
  
}

发布了14 篇原创文章 · 获赞 15 · 访问量 1640

猜你喜欢

转载自blog.csdn.net/u011323200/article/details/103710936