IOUtil

来源:互联网 发布:直播到底怎么赚钱知乎 编辑:程序博客网 时间:2024/05/08 03:06
package netstar.ext.cradle.laptop.utils;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.IOException;import java.io.OutputStreamWriter;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;public class Utils {/** * 备份文件 *@param in *@param out *调用:Utils.CopyFile(new File("C:\\xx.sql"), new File("C:\\copy.sql")); */public static  void   CopyFile(File   in,   File   out)   throws   Exception   {         FileInputStream   fis     =   new   FileInputStream(in);         FileOutputStream   fos   =   new   FileOutputStream(out);         byte[]   buf   =   new   byte[1024];         int   i   =   0;         while((i=fis.read(buf))!=-1)   {             fos.write(buf,   0,   i);             }         fis.close();         fos.close();         }     /** * 创建文件 * @param filePath:文件路径 * @param directoryPath:文件所在文件夹路径 * 调用:Utils.createFile("D:\\data\\Putaway_Tasks.dat", "D:\\data"); */public static File createFile(String filePath, String directoryPath) {File file = new File(directoryPath);if (!file.exists()) {System.out.println(directoryPath + "目录不存在,创建该目录");file.mkdir();// 创建目录}file = new File(filePath);if(file.exists()){file.delete();}if (!file.exists()) {try {if (file.createNewFile())System.out.println(directoryPath + "该文件成功创建");} catch (IOException e) {}}return file;}/** * 文件内容写入 *  * @param aFile * @param aContents * @throws FileNotFoundException * @throws IOException */public static void setContents(File aFile, String aContents)throws FileNotFoundException, IOException {if (aFile == null) {throw new IllegalArgumentException("File should not be null.");}if (!aFile.exists()) {throw new FileNotFoundException("File does not exist: " + aFile);}if (!aFile.isFile()) {throw new IllegalArgumentException("Should not be a directory: "+ aFile);}if (!aFile.canWrite()) {throw new IllegalArgumentException("File cannot be written: "+ aFile);}try {OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(aFile, true), "UTF-8");BufferedWriter fbw = new BufferedWriter(writer);fbw.write(aContents);fbw.newLine();fbw.close();} catch (Exception e) {System.out.println("Error: " + e.getMessage());}}/** * 读取文件内容 *  * @param fileName * @return */public static BufferedReader readFile(String fileName) {BufferedReader br;try {br = new BufferedReader(new FileReader(fileName));return br;} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}/** * 字符串转日期类型 *  * @param strDate * @return java.sql.Date */public static java.sql.Date stringConvertDate(String strDate) {java.util.Date today = new java.util.Date();DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");try {today = formatter.parse(strDate);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}long t = today.getTime();java.sql.Date sqlDate = new java.sql.Date(t);return sqlDate;}public static void main(String[] args) {File in=new File("C:\\xx.sql");File out=new File("C:\\yy.sql");try {Utils.CopyFile(new File("C:\\xx.sql"), new File("C:\\yy.sql"));} catch (Exception e) {e.printStackTrace();}}}

原创粉丝点击