文件3
来源:互联网 发布:开源的数据库 编辑:程序博客网 时间:2024/05/08 22:27
import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.logging.LogFactory;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;public class FileOperate{public static String mesagePath = "d:\\"; // 日志 public static String nowDateStr = new SimpleDateFormat("yyyyMMdd").format(new Date());/** * 写文件 * * @param filename * @param str * @throws Exception */public static boolean write1(String filename, String str) {try {BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename), false));bw.write(str);bw.flush();bw.close();return true;} catch (IOException e) {// TODO Auto-generated catch blockLogFactory.getLog(FileOperate.class).info("写文件异常"+e.getMessage());return false;}}public static boolean write(String filename, String str) {try {BufferedWriter bw = new BufferedWriter(new FileWriter(new File(filename), true));bw.write(str);bw.flush();bw.close();return true;} catch (IOException e) {// TODO Auto-generated catch blockreturn false;}}/** * 根据字符串写成xml文件 * @param fileName * @param xml * @return */public File writeXmlFile(String fileName , String xml){File file = new File(fileName);OutputStream os = null;try {os = new FileOutputStream(file);os.write(xml.getBytes());os.flush();} catch (Exception e) {} finally {try {os.close();} catch (Exception e) {}}return file;}/** * 从InputStream写文件 * @param fileName * @param in * @return */public File writeFromIn(String fileName , InputStream in){File file = new File(fileName);OutputStream os = null;try {os = new FileOutputStream(file);byte buffer[] = new byte[in.available()];while ((in.read(buffer)) != -1) {os.write(buffer);}os.flush();} catch (Exception e) {} finally {try {os.close();} catch (Exception e) {}}return file;}/** * 删除某个文件夹下的所有文件夹和文件 * @param delpath String * @throws FileNotFoundException * @throws IOException * @return boolean */public static boolean deletefile(String delpath){ try { File file = new File(delpath); if (!file.isDirectory()) { file.delete(); } else if (file.isDirectory()) { String[] filelist = file.list(); for (int i = 0; i < filelist.length; i++){File delfile = new File(delpath + "\\" + filelist[i]);if (!delfile.isDirectory()) {delfile.delete(); } else if (delfile.isDirectory()) { deletefile(delpath + "\\" + filelist[i]); } } //file.delete(); } } catch (Exception e){ e.getMessage(); } return true; } /** * 复制整个文件夹内容 * @param oldPath String 原文件路径 : * @param newPath String 复制后路径 * @return boolean */ public static void copyFolder(String oldPath, String newPath) { FileOutputStream output = null; FileInputStream input = null; try { (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 File a=new File(oldPath); String[] file=a.list(); File temp=null; for (int i = 0; i < file.length; i++) { if(oldPath.endsWith(File.separator)){ temp=new File(oldPath+file[i]); } else{ temp=new File(oldPath+File.separator+file[i]); } if(temp.isFile()){ input = new FileInputStream(temp); output = new FileOutputStream(newPath + "/" + (temp.getName()).toString()); byte[] b = new byte[1024 * 5]; int len; while ( (len = input.read(b)) != -1) { output.write(b, 0, len); } } if(temp.isDirectory()){//如果是子文件夹 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); } } } catch (Exception e) { }finally{ try { if(output != null && input != null) {output.flush();output.close(); input.close(); } } catch (IOException e) { } } } /** * 删除文件 * @param fileFath */ public static void delete(String fileFath){ if(fileFath == null || "".equals(fileFath)){ return ; } File file = new File(fileFath); if(file.isFile() && file.exists()){ file.delete(); } } /** * <p>将文件转成base64 字符串</p> * @param path 文件路径 * @return * @throws Exception */ public static String encodeBase64File(String path) throws Exception { File file = new File(path); FileInputStream inputFile = new FileInputStream(file); byte[] buffer = new byte[(int)file.length()]; inputFile.read(buffer); inputFile.close(); return new BASE64Encoder().encode(buffer); } /** * <p>将base64字符解码保存文件</p> * @param base64Code * @param targetPath * @throws Exception */ public static void decoderBase64File(String base64Code,String targetPath) throws Exception { byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code); FileOutputStream out = new FileOutputStream(targetPath); out.write(buffer); out.close(); } /** * <p>将base64字符保存文本文件</p> * @param base64Code * @param targetPath * @throws Exception */ public static void toFile(String base64Code,String targetPath) throws Exception { byte[] buffer = base64Code.getBytes(); FileOutputStream out = new FileOutputStream(targetPath); out.write(buffer); out.close(); } public static void main(String[] args) { try { String base64Code =encodeBase64File("C:\\123.pdf"); System.out.println(base64Code); decoderBase64File(base64Code, "D:\\456.PDF"); toFile(base64Code, "D:\\three.txt"); } catch (Exception e) { e.printStackTrace(); } } }
0 0
- 文件3
- 文件3
- 3D .mtl 材质库文件 文件说明
- 文件编程(3)文件锁
- php文件3种读文件方式
- PHP---文件操作3---写文件
- 文件上传下载(3)
- JspsmartUpload上传文件(3)
- 读取3ds文件
- 读取3DS文件
- 文件上传3
- 文件读写(3)
- 读取3DS文件
- QFile(3) - 文件读写
- 3DS文件结构
- ftp文件上传3
- webservice上传下载文件3
- 3DS文件结构
- Linux——用户及文件权限
- DHCP的防欺骗
- leetcode之Pascal's Triangle
- Hibernate常用方法之update
- Opencv--关于角点检测
- 文件3
- cookie劫持
- 逃逸攻击
- 40节课后再解鸡兔共笼and找数字等
- Mac OS 下统计代码行数的工具
- NS3(2)---ns3中的packet类
- struts2与spring的简单集成
- FPGA双线性插值算法(图像的缩放)
- Hadoop 单机部署与eclipse开发