package com.omg.web.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.omg.soap.service.EBookingMethod;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class FileUtil {
private static final Log LOG = LogFactory.getLog(FileUtil.class);
/**
* 取得文件后缀名
*
* @param fileName
* 文件名
* @return
*/
public static String getFileExtention(String fileName) {
if (null == fileName || fileName.equals("")) {
return "";
}
int i = fileName.lastIndexOf(".");
if (i > 0 && i < fileName.length()) {
return fileName.substring(i);
}
return "";
}
/**
* 文件是否存在
*
* @param filePath
* 文件物理地址
* @return
*/
public static boolean fileExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
/**
* 建立文件夹
*
* @param filePath
* 文件夹路径
* @return
*/
public static File createDirectory(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
/**
* 复制文件
*
* @param src
* 源文件
* @param dst
* 目标文件
* @throws IOException
*/
public static void copy(File src, File dst) throws IOException {
FileChannel srcChannel = new FileInputStream(src).getChannel();
FileChannel destChannel = new FileOutputStream(dst).getChannel();
try {
srcChannel.transferTo(0, srcChannel.size(), destChannel);
} finally {
srcChannel.close();
destChannel.close();
}
}
/**
* 下载网络文件保存到本地
*
* @param path
* 网络url
* @param filepath
* 本地保存地址
* @throws Exception
*/
public static boolean downloadFile(String path, String filepath)
throws Exception {
LOG.info("下载图片路径:【" + path + "】");
LOG.info("保存文件路径:【" + filepath + "】");
int line;
byte[] read = new byte[1024];
InputStream in = null;
FileOutputStream out = null;
try {
URL url = new URL(path);
in = url.openStream();
out = new FileOutputStream(filepath);
while ((line = in.read(read)) > 0) {
out.write(read, 0, line);
}
return true;
} catch (Exception e) {
LOG.error(new FileUtil(), e);
LOG.error("下载图片路径:【" + path + "】 failure");
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
return false;
}
/**
* 读文件
*
* @param filePath
* 文件地址
* @return
* @throws Exception
*/
public static String readFromFile(String filePath) throws Exception {
String content = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
int length = fis.available();
LOG.debug(filePath + "-->file size : " + length);
byte bytes[] = new byte[length];
fis.read(bytes);
content = new String(bytes, "utf-8");
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (fis != null) {
fis.close();
}
}
LOG.debug("###### " + filePath + "读取完成。######");
return content;
}
/**
* 写文件
*
* @param content
* 内容
* @param filePath
* 文件地址
* @throws Exception
*/
public static void writeToFile(String content, String filePath)
throws Exception {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
byte tag_bytes[] = content.getBytes("utf-8");
fos.write(tag_bytes);
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (fos != null) {
fos.close();
}
}
LOG.debug("###### " + filePath + "写入完成。######");
}
/**
* 文件流保存
*
* @param io
* base64文件流
* @param filePath
* @throws Exception
*/
public static void base64ToFile(String io, String filePath)
throws Exception {
FileOutputStream fos = null;
try {
BASE64Decoder decode = new BASE64Decoder();
fos = new FileOutputStream(filePath);
fos.write(decode.decodeBuffer(io));
fos.close();
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (null != fos) {
fos.close();
}
}
}
/**
* 生成BASE64文件流
*
* @param filePath
* @return
* @throws Exception
*/
public static String readFileToBase64(String filePath) throws Exception {
FileInputStream io = null;
try {
io = new FileInputStream(new File(filePath));
byte[] b = new byte[io.available()];
io.read(b);
BASE64Encoder encode = new BASE64Encoder();
return encode.encode(b);
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (null != io) {
io.close();
}
}
}
public static void deleteFile(String filePath) throws Exception {
try {
File f = new File(filePath);
if (f.exists()) {
f.delete();
}
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
}
}
/**
* 下载文件
*
* @param file
* @param resp
* @throws Exception
*/
public static void getFileIO(File file, HttpServletResponse resp)
throws Exception {
InputStream fis = null;
OutputStream toClient = null;
try {
// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
resp.reset();
// 设置response的Header
resp.addHeader("Content-Disposition", "attachment;filename="
+ new String(file.getName().getBytes()));
resp.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(resp.getOutputStream());
resp.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (null != fis) {
fis.close();
}
if (null != toClient) {
toClient.close();
}
}
}
/*
* public static void main(String[] args) { try { FileInputStream io = new
* FileInputStream(new File("f:/d.png")); byte[] b = new
* byte[io.available()]; io.read(b); BASE64Encoder encode = new
* BASE64Encoder(); FileUtil.writeToFile(encode.encode(b), "f:/2.txt");
* String s = EBookingMethod.getStringResult(
* "E:\\xml\\getProcessTaskGraphic_3_response.xml");
* FileUtil.base64ToFile(s, "f:/003.jpg"); } catch (Exception e) { // TODO
* Auto-generated catch block e.printStackTrace(); } }
*/
}
读取text文件:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class ReadTextFile {
public BufferedReader bufread;
public BufferedWriter bufwriter;
File writefile;
String filepath, filecontent, read;
String readStr = "" ;
// 从文本文件中读取内容
public String readfile(String path)
{
try {
filepath = path; // 得到文本文件的路径
File file = new File(filepath);
FileReader fileread = new FileReader(file);
bufread = new BufferedReader(fileread);
while ((read = bufread.readLine()) != null ) {
read = read + " \r\n " ;
readStr = readStr + read;
}
} catch (Exception d) {
System.out.println(d.getMessage());
}
return readStr; // 返回从文本文件中读取内容
}
// 向文本文件中写入内容
public void writefile(String path, String content, boolean append) {
try {
boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
filepath = path; // 得到文本文件的路径
filecontent = "s";//content; // 需要写入的内容
writefile = new File(filepath);
if (writefile.exists() == false ) // 如果文本文件不存在则创建它
{
writefile.createNewFile();
writefile = new File(filepath); // 重新实例化
}
FileWriter filewriter = new FileWriter(writefile, addStr);
// 删除原有文件的内容
java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, "rw" );
file.setLength( 0 );
// 写入新的文件内容
filewriter.write(filecontent);
filewriter.close();
filewriter.flush();
} catch (Exception d) {
System.out.println(d.getMessage());
}
}
public static void main(String[] args) throws Exception {
ReadTextFile parse = new ReadTextFile();
String filecontent = parse.readfile( "E:/ACOM20121113.txt" );
System.out.println(filecontent);
parse.writefile( "f:/success.txt" ,filecontent, true );
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.omg.soap.service.EBookingMethod;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class FileUtil {
private static final Log LOG = LogFactory.getLog(FileUtil.class);
/**
* 取得文件后缀名
*
* @param fileName
* 文件名
* @return
*/
public static String getFileExtention(String fileName) {
if (null == fileName || fileName.equals("")) {
return "";
}
int i = fileName.lastIndexOf(".");
if (i > 0 && i < fileName.length()) {
return fileName.substring(i);
}
return "";
}
/**
* 文件是否存在
*
* @param filePath
* 文件物理地址
* @return
*/
public static boolean fileExists(String filePath) {
File file = new File(filePath);
return file.exists();
}
/**
* 建立文件夹
*
* @param filePath
* 文件夹路径
* @return
*/
public static File createDirectory(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
/**
* 复制文件
*
* @param src
* 源文件
* @param dst
* 目标文件
* @throws IOException
*/
public static void copy(File src, File dst) throws IOException {
FileChannel srcChannel = new FileInputStream(src).getChannel();
FileChannel destChannel = new FileOutputStream(dst).getChannel();
try {
srcChannel.transferTo(0, srcChannel.size(), destChannel);
} finally {
srcChannel.close();
destChannel.close();
}
}
/**
* 下载网络文件保存到本地
*
* @param path
* 网络url
* @param filepath
* 本地保存地址
* @throws Exception
*/
public static boolean downloadFile(String path, String filepath)
throws Exception {
LOG.info("下载图片路径:【" + path + "】");
LOG.info("保存文件路径:【" + filepath + "】");
int line;
byte[] read = new byte[1024];
InputStream in = null;
FileOutputStream out = null;
try {
URL url = new URL(path);
in = url.openStream();
out = new FileOutputStream(filepath);
while ((line = in.read(read)) > 0) {
out.write(read, 0, line);
}
return true;
} catch (Exception e) {
LOG.error(new FileUtil(), e);
LOG.error("下载图片路径:【" + path + "】 failure");
} finally {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
}
return false;
}
/**
* 读文件
*
* @param filePath
* 文件地址
* @return
* @throws Exception
*/
public static String readFromFile(String filePath) throws Exception {
String content = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(filePath);
int length = fis.available();
LOG.debug(filePath + "-->file size : " + length);
byte bytes[] = new byte[length];
fis.read(bytes);
content = new String(bytes, "utf-8");
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (fis != null) {
fis.close();
}
}
LOG.debug("###### " + filePath + "读取完成。######");
return content;
}
/**
* 写文件
*
* @param content
* 内容
* @param filePath
* 文件地址
* @throws Exception
*/
public static void writeToFile(String content, String filePath)
throws Exception {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(filePath);
byte tag_bytes[] = content.getBytes("utf-8");
fos.write(tag_bytes);
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (fos != null) {
fos.close();
}
}
LOG.debug("###### " + filePath + "写入完成。######");
}
/**
* 文件流保存
*
* @param io
* base64文件流
* @param filePath
* @throws Exception
*/
public static void base64ToFile(String io, String filePath)
throws Exception {
FileOutputStream fos = null;
try {
BASE64Decoder decode = new BASE64Decoder();
fos = new FileOutputStream(filePath);
fos.write(decode.decodeBuffer(io));
fos.close();
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (null != fos) {
fos.close();
}
}
}
/**
* 生成BASE64文件流
*
* @param filePath
* @return
* @throws Exception
*/
public static String readFileToBase64(String filePath) throws Exception {
FileInputStream io = null;
try {
io = new FileInputStream(new File(filePath));
byte[] b = new byte[io.available()];
io.read(b);
BASE64Encoder encode = new BASE64Encoder();
return encode.encode(b);
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (null != io) {
io.close();
}
}
}
public static void deleteFile(String filePath) throws Exception {
try {
File f = new File(filePath);
if (f.exists()) {
f.delete();
}
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
}
}
/**
* 下载文件
*
* @param file
* @param resp
* @throws Exception
*/
public static void getFileIO(File file, HttpServletResponse resp)
throws Exception {
InputStream fis = null;
OutputStream toClient = null;
try {
// 以流的形式下载文件。
fis = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
resp.reset();
// 设置response的Header
resp.addHeader("Content-Disposition", "attachment;filename="
+ new String(file.getName().getBytes()));
resp.addHeader("Content-Length", "" + file.length());
toClient = new BufferedOutputStream(resp.getOutputStream());
resp.setContentType("application/octet-stream");
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (Exception e) {
LOG.error(e);
throw new Exception(e);
} finally {
if (null != fis) {
fis.close();
}
if (null != toClient) {
toClient.close();
}
}
}
/*
* public static void main(String[] args) { try { FileInputStream io = new
* FileInputStream(new File("f:/d.png")); byte[] b = new
* byte[io.available()]; io.read(b); BASE64Encoder encode = new
* BASE64Encoder(); FileUtil.writeToFile(encode.encode(b), "f:/2.txt");
* String s = EBookingMethod.getStringResult(
* "E:\\xml\\getProcessTaskGraphic_3_response.xml");
* FileUtil.base64ToFile(s, "f:/003.jpg"); } catch (Exception e) { // TODO
* Auto-generated catch block e.printStackTrace(); } }
*/
}
读取text文件:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class ReadTextFile {
public BufferedReader bufread;
public BufferedWriter bufwriter;
File writefile;
String filepath, filecontent, read;
String readStr = "" ;
// 从文本文件中读取内容
public String readfile(String path)
{
try {
filepath = path; // 得到文本文件的路径
File file = new File(filepath);
FileReader fileread = new FileReader(file);
bufread = new BufferedReader(fileread);
while ((read = bufread.readLine()) != null ) {
read = read + " \r\n " ;
readStr = readStr + read;
}
} catch (Exception d) {
System.out.println(d.getMessage());
}
return readStr; // 返回从文本文件中读取内容
}
// 向文本文件中写入内容
public void writefile(String path, String content, boolean append) {
try {
boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
filepath = path; // 得到文本文件的路径
filecontent = "s";//content; // 需要写入的内容
writefile = new File(filepath);
if (writefile.exists() == false ) // 如果文本文件不存在则创建它
{
writefile.createNewFile();
writefile = new File(filepath); // 重新实例化
}
FileWriter filewriter = new FileWriter(writefile, addStr);
// 删除原有文件的内容
java.io.RandomAccessFile file = new java.io.RandomAccessFile(path, "rw" );
file.setLength( 0 );
// 写入新的文件内容
filewriter.write(filecontent);
filewriter.close();
filewriter.flush();
} catch (Exception d) {
System.out.println(d.getMessage());
}
}
public static void main(String[] args) throws Exception {
ReadTextFile parse = new ReadTextFile();
String filecontent = parse.readfile( "E:/ACOM20121113.txt" );
System.out.println(filecontent);
parse.writefile( "f:/success.txt" ,filecontent, true );
}
}