Java文件操作类

  1. package cn.edu.hbcit.jsj.accp09.bean;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintWriter;  
  5. import java.io.*;  
  6. import java.util.*;  
  7. import java.awt.*;  
  8. import java.awt.event.*;  
  9. import java.awt.image.*;  
  10. import javax.imageio.*;  
  11. import com.sun.image.codec.jpeg.*;  
  12.   
  13. import org.apache.log4j.Logger;  
  14. /** 
  15.  * <p>Description: 文件操作</p> 
  16.  * 
  17.  * <p>Copyright: Copyright (c) 2009</p> 
  18.  * 
  19.  */  
  20.   
  21. public class FileOperate  {  
  22.     protected final Logger log = Logger.getLogger(FileOperate.class.getName());  
  23.   
  24.     public FileOperate() {  
  25.         // TODO Auto-generated constructor stub  
  26.     }  
  27.     /* 
  28.      * 生成随机文件名 
  29.      */  
  30.     public String generateRandomFilename(){  
  31.         String RandomFilename = "";  
  32.         Random rand = new Random();//生成随机数  
  33.         int random = rand.nextInt();  
  34.           
  35.         Calendar calCurrent = Calendar.getInstance();  
  36.         int intDay = calCurrent.get(Calendar.DATE);  
  37.         int intMonth = calCurrent.get(Calendar.MONTH) + 1;  
  38.         int intYear = calCurrent.get(Calendar.YEAR);  
  39.         String now = String.valueOf(intYear) + "_" + String.valueOf(intMonth) + "_" +  
  40.             String.valueOf(intDay) + "_";  
  41.         log.debug("生成于今日的文件名前缀为:"+now);  
  42.           
  43.         RandomFilename = now + String.valueOf(random > 0 ? random : ( -1) * random) + ".";  
  44.           
  45.         return RandomFilename;  
  46.     }  
  47.       
  48.     /** 
  49.      * 删除文件 
  50.      * @param fileName 
  51.      * @return boolean 
  52.      */  
  53.     public boolean deleteFile(String fileName){  
  54.         boolean flag = false;  
  55.         try{  
  56.             File delFile = new File(fileName);  
  57.             log.debug("尝试删除:"+fileName);  
  58.             flag = delFile.delete();  
  59.         }catch(Exception e){  
  60.             log.error(e);  
  61.         }  
  62.           
  63.         return flag;  
  64.     }  
  65.       
  66.     /** 
  67.      * 为图片添加水印 
  68.      * @param s 要添加的水印 
  69.      * @param ImgName 要修改的图片名称及路径 
  70.      * @param bottom 水印距底边的距离 
  71.      * @param right 水印距右边的距离 
  72.      */  
  73.     public void imageWatermark(String s,String ImgName, int right, int bottom){   
  74.         try{   
  75.         File _file = new File(ImgName);   
  76.         Image src = ImageIO.read(_file);   
  77.         File img = new File(s);   
  78.         Image imgsrc = ImageIO.read(img);   
  79.         int wideth=src.getWidth(null);   
  80.         int height=src.getHeight(null);   
  81.         BufferedImage image=new BufferedImage(wideth,height,BufferedImage.TYPE_INT_RGB);   
  82.         Graphics g=image.createGraphics();   
  83.         g.drawImage(src,0,0,wideth,height,null);   
  84.         //String s="要加的水印";   
  85.         g.setColor(Color.LIGHT_GRAY);   
  86.           
  87.         g.setFont(new Font("Britannic Bold",Font.ITALIC,18));   
  88.         //g.setFont(new Font("Arial Black",Font.ITALIC,18));   
  89.         //Font aa=new Font("宋体",Font.PLAIN,20);   
  90.   
  91.         //g.drawString(s,wideth-right,height-bottom);  
  92.         g.drawImage(imgsrc, wideth-right,height-bottom,null);  
  93.         g.dispose();   
  94.         FileOutputStream out=new FileOutputStream(ImgName);   
  95.         JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);   
  96.         encoder.encode(image);   
  97.         out.close();   
  98.         }   
  99.         catch(Exception e){   
  100.         System.out.println(e);   
  101.         }   
  102.         }   
  103.       
  104. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值