将C:\idea_pro压缩zip文件
实验证明:在c盘根目录牵扯权限问题,无法实现,尽量文件路径不要是根目录
另外,切记不可以将生成的zip放进要压缩的文件夹,否则文件夹一直在变,程序死循环,最终内存溢出
public class Test01 {
public static void main(String[] args) throws IOException {
String targetPath = "C:\\software\\idea_pro.zip";
compreSsion(targetPath, new File("C:\\idea_pro"));
//3种获取文件大小的方法
//1,使用File的length方法
System.out.println(new File(targetPath).length()+"字节");
//2,通过FileInputStream获取文件大小
File f = new File(targetPath);
FileInputStream fis = new FileInputStream(f);
System.out.println(fis.available()+"字节");
//特别注意这里,available返回的是int,如果文件大小字节数超过了Integer.MAX_VALUE,会得到错误结果
File f2 = new File(targetPath);//3,FileChannel
FileInputStream fis2 = new FileInputStream(f2);
FileChannel fc = fis2.getChannel();
System.out.println(fc.size()+"字节");
}
private static void compreSsion (String zipFileName, File target) throws IOException {
System.out.println("开始压缩文件,将:"+target.getName()+",压缩为:"+zipFileName);
long start = System.currentTimeMillis();
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedOutputStream bos = new BufferedOutputStream(out);
zip(out, target, target.getName(), bos);
bos.close();;
out.close();
long end = System.currentTimeMillis();
System.out.println("压缩完成,耗时:"+(end-start)+"毫秒,即大约"+(end-start)/1000+"秒");
}
private static void zip (ZipOutputStream zout, File target, String name, BufferedOutputStream bos) throws IOException{
//判断是不是目录
if (target.isDirectory()) {
File[] files = target.listFiles();
if (files.length == 0 ) {
//空目录
zout.putNextEntry(new ZipEntry(name+"/"));
}
/**
* 开始编写新的zip文件条目,并将流定位到条目数据的开头
* 关闭当前条目,如果仍然有效。如果没有为条目数据指定压缩方法,将使用默认压缩方法,
* 如果条目没有设置修改时间,将使用当前时间
*/
for (File f : files ) {
//递归处理
zip(zout, f, name+"/"+f.getName(), bos);
}
} else {
zout.putNextEntry(new ZipEntry(name));
InputStream inputStream = new FileInputStream(target);
BufferedInputStream bis = new BufferedInputStream(inputStream);
byte[] bytes = new byte[1024]; //一次读取1k
int len = -1;
while ( (len = bis.read(bytes))!= -1 ) {
bos.write(bytes, 0, len);
}
bis.close();
}
}
}
统计字符串中每个字符出现的次数
String str = "fgfdhhd5hb56546vg546";// 定义字符串
char arr[] = str.toCharArray();// 转换成字符数组
Arrays.sort(arr);// 数组排序
String temp = new String(arr);// 重新产生字符串
Map map = new HashMap();
//遍历统计
for(int startIndex=0;startIndex<str.length();){
char c = temp.charAt(startIndex);// 获取第一个相同字符
String t = String.valueOf(c);// 把第一个字符转换成字符串
// 获取字符最后出现的位置
int lastIndex = temp.lastIndexOf(t);
System.out.println(t+" 出现的次数为: "+(lastIndex+1-startIndex));
map.put(t, lastIndex+1-startIndex);
startIndex=lastIndex+1;//下次开始的位置
}
System.out.println(map.toString());
根据开始日期和结束日期返回时间段内的日期集合
public static void main(String[] args) throws ParseException {
String start = "2014-04-01";
String end = "2014-04-02";
String sdf = "yyyy-MM-dd";
List<String> listDate = getPerDaysByStartAndEndDate(start, end, sdf);
Collections.reverse(listDate);//getPerDaysByStartAndEndDate方法获取的日期是倒序,需要用Collections.reverse()反转
for(int i=0;i<listDate.size();i++){
System.out.println(sdf.format(listDate.get(i)));
}
}
/**
* 获取某日期区间的所有日期 日期倒序
* @param startDate 开始日期
* @param endDate 结束日期
* @param dateFormat 日期格式
* @return 区间内所有日期
*/
public static List<String> getPerDaysByStartAndEndDate
(String startDate, String endDate, String dateFormat) throws ParseException {
DateFormat format = new SimpleDateFormat(dateFormat);
Date sDate = format.parse(startDate);
Date eDate = format.parse(endDate);
long start = sDate.getTime();
long end = eDate.getTime();
if (start > end) {
return null;
}
Calendar calendar = Calendar.getInstance();
calendar.setTime(eDate);
List<String> res = new ArrayList<String>();
while (end >= start) {
res.add(format.format(calendar.getTime()));
calendar.add(Calendar.DAY_OF_MONTH, -1);
end = calendar.getTimeInMillis();
}
return res;
}
基姆拉尔森公式判断指定日期是星期几
public static int CalculateWeekDay(int year, int month, int day) {
if (month == 1 || month == 2) {
month+= 12;
year --;
}
int iWeek = (day+2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400)%7;
int weekcode = 0;
switch (iWeek) {
case 0: case 1: case 2:case 3:case 4://01234表示周内工作日,0表示工作日
weekcode = 0;
break;
case 5:case 6://56表示周末,2表示公休日
weekcode = 2;
break;
}
return iWeek;//如果只需要返回周内还是周末可以return weekcode
}
根据year,month返回该月份天数
function:根据year,month返回该月份天数
四年一闰,百年不闰,四百年再闰
public static int Days(int year,int month){
int days=28;
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){
days = 31;
} else if(month==4||month==6||month==9||month==11){
days = 30;
} else{
if(year%4==0) {
if(year%100==0) {
if(year%400==0) {
days = 29;
}
} else{
days = 29;
}
}
}
return days;
}
读取excel写入数据库
准备xlsx文件,序号,姓名,工号,部门,性别
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>
读取excel数据
Workbook workbook = WorkbookFactory.create(new File("C:\\Environment\\user.xlsx"));
//获取一张表
Sheet sheet = workbook.getSheetAt(1);
for (int i=1; i<=sheet.getLastRowNum(); i++) {
//跳过第一行,取得其他行数据
Row row=sheet.getRow(i);//取得第i行数据
for (int j=0; j<row.getLastCellNum(); j++) {
Cell cell=row.getCell(j);//取得第j列数据
cell.setCellType(CellType.STRING);
String value = cell.getStringCellValue();
System.out.print(value);
System.out.print("===");
}
System.out.println();
}
// 11===张三===0002===信息部===男===
// 12===李四===0003===管理部===男===
// 13===王五===0004===财务部===男===
// 14===李磊===0005===行政部===男===
// 15===王大拿===0006===人事部===女===
// 16===谢大脚===0007===技术部===女===
读取并写入数据库
public class Test01 {
public static void main(String[] args) throws IOException, InvalidFormatException {
Workbook workbook = WorkbookFactory.create(new File("D:\\company_info.xls"));
System.out.println("sheets" + workbook.getNumberOfSheets());
//获取一张表
Sheet sheet = workbook.getSheetAt(0);
for (int i=1;i<=sheet.getLastRowNum();i++) {
//跳过第一行
Row row=sheet.getRow(i);//取得第i行数据
CUserDto userDto=new CUserDto();
String []str=new String[row.getLastCellNum()];
for (int j=0;j<row.getLastCellNum();j++) {
Cell cell=row.getCell(j);//取得第j列数据
cell.setCellType(CellType.STRING);
str[j]=cell.getStringCellValue().trim();
System.out.print(str[j]+" ");
}
//System.out.println();
//封装对象信息
userDto.setRoleId(2);
userDto.setUsername(str[0]);
userDto.setPassword(str[1]);
userDto.setCompany_name(str[2]);
userDto.setCompany_code(str[3]);
userDto.setRegion_code(Integer.parseInt(str[4]));
userDto.setFirst_cp_code(Integer.parseInt(str[5]));
userDto.setSecond_cp_code(Integer.parseInt(str[6]));
userDto.setFirst_industry_code(Integer.parseInt(str[7]));
userDto.setContact_name(str[8]);
userDto.setContact_phone(str[9]);
userDto.setContact_address(str[10]);
list.add(userDto); //加入到集合中
}
}
}