欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现变更文件查询的方法

程序员文章站 2024-03-05 17:27:37
本文实例讲述了java实现变更文件查询的方法。分享给大家供大家参考。具体如下: 自己经常发布包时需要查找那些文件时上次发包后更新的数据文件,所以写了这个发布包, 拷贝输...

本文实例讲述了java实现变更文件查询的方法。分享给大家供大家参考。具体如下:

自己经常发布包时需要查找那些文件时上次发包后更新的数据文件,所以写了这个发布包,
拷贝输出的命令,dos窗口下执行,
为啥不直接复制文件,因为java拷贝文件会修改文件最后修改日期,所以采用dos下的拷贝。

/*
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > java > 代码生成 > 代码和注释
 */
package com.cn.wangk.tools;
import java.awt.borderlayout;
import java.awt.color;
import java.awt.component;
import java.awt.container;
import java.awt.flowlayout;
import java.awt.gridlayout;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.util.calendar;
import java.util.gregoriancalendar;
import javax.swing.borderfactory;
import javax.swing.jbutton;
import javax.swing.jcombobox;
import javax.swing.jframe;
import javax.swing.jpanel;
/** *//**
 * bean to display a month calendar in a jpanel. only works for the western
 * calendar.
 * 
 * @author ian f. darwin, http://www.darwinsys.com/
 * @version $id: cal.java,v 1.5 2004/02/09 03:33:45 ian exp $
 */
public class cal extends jpanel{
 /** *//** the currently-interesting year (not modulo 1900!) */
 protected int yy;
 /** *//** currently-interesting month and day */
 protected int mm, dd;
 /** *//** the buttons to be displayed */
 protected jbutton labs[][];
 /** *//** the number of day squares to leave blank at the start of this month */
 protected int leadgap = 0;
 /** *//** a calendar object used throughout */
 calendar calendar = new gregoriancalendar();
 /** *//** today's year */
 protected final int thisyear = calendar.get(calendar.year);
 /** *//** today's month */
 protected final int thismonth = calendar.get(calendar.month);
 /** *//** one of the buttons. we just keep its reference for getbackground(). */
 private jbutton b0;
 /** *//** the month choice */
 private jcombobox monthchoice;
 /** *//** the year choice */
 private jcombobox yearchoice;
 /** *//**
  * construct a cal, starting with today.
  */
 cal(){
  super();
  setyymmdd(calendar.get(calendar.year), calendar.get(calendar.month),
    calendar.get(calendar.day_of_month));
  buildgui();
  recompute();
 }
 /** *//**
  * construct a cal, given the leading days and the total days
  * 
  * @exception illegalargumentexception
  *        if year out of range
  */
 cal(int year, int month, int today){
  super();
  setyymmdd(year, month, today);
  buildgui();
  recompute();
 }
 private void setyymmdd(int year, int month, int today){
  yy = year;
  mm = month;
  dd = today;
 }
 string[] months ={ "january", "february", "march", "april", "may", "june",
   "july", "august", "september", "october", "november", "december" };
 /** *//** build the gui. assumes that setyymmdd has been called. */
 private void buildgui(){
  getaccessiblecontext().setaccessibledescription(
    "calendar not accessible yet. sorry!");
  setborder(borderfactory.createetchedborder());
  setlayout(new borderlayout());
  jpanel tp = new jpanel();
  tp.add(monthchoice = new jcombobox());
  for (int i = 0; i < months.length; i++)
   monthchoice.additem(months[i]);
  monthchoice.setselecteditem(months[mm]);
  monthchoice.addactionlistener(new actionlistener(){
   public void actionperformed(actionevent ae){
    int i = monthchoice.getselectedindex();
    if (i >= 0){
     mm = i;
     // system.out.println("month=" + mm);
     recompute();
    }
   }
  });
  monthchoice.getaccessiblecontext().setaccessiblename("months");
  monthchoice.getaccessiblecontext().setaccessibledescription(
    "choose a month of the year");
  tp.add(yearchoice = new jcombobox());
  yearchoice.seteditable(true);
  for (int i = yy - 5; i < yy + 5; i++)
   yearchoice.additem(integer.tostring(i));
  yearchoice.setselecteditem(integer.tostring(yy));
  yearchoice.addactionlistener(new actionlistener(){
   public void actionperformed(actionevent ae){
    int i = yearchoice.getselectedindex();
    if (i >= 0){
     yy = integer.parseint(yearchoice.getselecteditem()
       .tostring());
     // system.out.println("year=" + yy);
     recompute();
    }
   }
  });
  add(borderlayout.center, tp);
  jpanel bp = new jpanel();
  bp.setlayout(new gridlayout(7, 7));
  labs = new jbutton[6][7]; // first row is days
  bp.add(b0 = new jbutton("s"));
  bp.add(new jbutton("m"));
  bp.add(new jbutton("t"));
  bp.add(new jbutton("w"));
  bp.add(new jbutton("r"));
  bp.add(new jbutton("f"));
  bp.add(new jbutton("s"));
  actionlistener datesetter = new actionlistener(){
   public void actionperformed(actionevent e){
    string num = e.getactioncommand();
    if (!num.equals("")){
     // set the current day highlighted
     setdayactive(integer.parseint(num));
     // when this becomes a bean, you can
     // fire some kind of datechanged event here.
     // also, build a similar daysetter for day-of-week btns.
    }
   }
  };
  // construct all the buttons, and add them.
  for (int i = 0; i < 6; i++)
   for (int j = 0; j < 7; j++){
    bp.add(labs[i][j] = new jbutton(""));
    labs[i][j].addactionlistener(datesetter);
   }
  add(borderlayout.south, bp);
 }
 public final static int dom[] ={ 31, 28, 31, 30, /**//* jan feb mar apr */
 31, 30, 31, 31, /**//* may jun jul aug */
 30, 31, 30, 31 /**//* sep oct nov dec */
 };
 /** *//** compute which days to put where, in the cal panel */
 protected void recompute(){
  // system.out.println("cal::recompute: " + yy + ":" + mm + ":" + dd);
  if (mm < 0 || mm > 11)
   throw new illegalargumentexception("month " + mm
     + " bad, must be 0-11");
  cleardayactive();
  calendar = new gregoriancalendar(yy, mm, dd);
  // compute how much to leave before the first.
  // getday() returns 0 for sunday, which is just right.
  leadgap = new gregoriancalendar(yy, mm, 1).get(calendar.day_of_week) - 1;
  // system.out.println("leadgap = " + leadgap);
  int daysinmonth = dom[mm];
  if (isleap(calendar.get(calendar.year)) && mm > 1)
   ++daysinmonth;
  // blank out the labels before 1st day of month
  for (int i = 0; i < leadgap; i++){
   labs[0][i].settext("");
  }
  // fill in numbers for the day of month.
  for (int i = 1; i <= daysinmonth; i++){
   jbutton b = labs[(leadgap + i - 1) / 7][(leadgap + i - 1) % 7];
   b.settext(integer.tostring(i));
  }
  // 7 days/week * up to 6 rows
  for (int i = leadgap + 1 + daysinmonth; i < 6 * 7; i++){
   labs[(i) / 7][(i) % 7].settext("");
  }
  // shade current day, only if current month
  if (thisyear == yy && mm == thismonth)
   setdayactive(dd); // shade the box for today
  // say we need to be drawn on the screen
  repaint();
 }
 /** *//**
  * isleap() returns true if the given year is a leap year.
  * 
  * "a year is a leap year if it is divisible by 4 but not by 100, except
  * that years divisible by 400 *are* leap years." -- kernighan & ritchie,
  * _the c programming language_, p 37.
  */
 public boolean isleap(int year){
  if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
   return true;
  return false;
 }
 /** *//** set the year, month, and day */
 public void setdate(int yy, int mm, int dd){
  // system.out.println("cal::setdate");
  this.yy = yy;
  this.mm = mm; // starts at 0, like date
  this.dd = dd;
  recompute();
 }
 /** *//** unset any previously highlighted day */
 private void cleardayactive(){
  jbutton b;
  // first un-shade the previously-selected square, if any
  if (activeday > 0){
   b = labs[(leadgap + activeday - 1) / 7][(leadgap + activeday - 1) % 7];
   b.setbackground(b0.getbackground());
   b.repaint();
   activeday = -1;
  }
 }
 private int activeday = -1;
 /** *//** set just the day, on the current month */
 public void setdayactive(int newday){
  cleardayactive();
  // set the new one
  if (newday <= 0)
   dd = new gregoriancalendar().get(calendar.day_of_month);
  else
   dd = newday;
  // now shade the correct square
  component square = labs[(leadgap + newday - 1) / 7][(leadgap + newday - 1) % 7];
  square.setbackground(color.red);
  square.repaint();
  activeday = newday;
 }
 /** *//** for testing, a main program */
 public static void main(string[] av){
  jframe f = new jframe("cal");
  container c = f.getcontentpane();
  c.setlayout(new flowlayout());
  // for this test driver, hardcode 1995/02/10.
  c.add(new cal(1995, 2 - 1, 10));
  // and beside it, the current month.
  c.add(new cal());
  f.pack();
  f.setvisible(true);
 }
}

希望本文所述对大家的java程序设计有所帮助。