读取txt 修改文档里面 某个日期到前几天的日期

来源:互联网 发布:c语言接口与实现 pdf 编辑:程序博客网 时间:2024/05/22 09:40
package com.ac.whu;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.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Random;class ChangeDPITemplateTime {public static void main(String[] args) throws IOException {String str1, str2;int count = 0;String destidate = "20140604";InputStreamReader isr = new InputStreamReader(new FileInputStream(new File("读入的文档路径")), "GBK");// 考虑到编码格式BufferedReader br = new BufferedReader(isr);OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(new File("写入的文档路径" )), "GBK");BufferedWriter bw = new BufferedWriter(osw);while ((str1 = br.readLine()) != null) {//System.out.println(Math.round(呵呵));//System.out.println(Math.floor(呵呵));//System.out.println(randNumAB(28, 31));//System.out.println(randNumdaybeforerandday("20140601"));str1 = str1.replace("20140521", destidate);str2 = str1.replace("20140520", randNumdaybeforerandday(destidate)) + "\n";bw.write(str2);if (count % 10000 == 0)System.out.println(count);count++;}System.out.println("总行数为:" + count);// bw.flush();br.close();bw.close();}
<span style="white-space:pre"></span>显示a,b之间的数值public static String randNumAB(int a, int b){int result = 0;String rt = "";result = (int) Math.round(a + Math.random() * (b - a));if(result < 10){rt = "0" + result;}rt = Integer.toString(result);return rt;}//随机显示某个日期前两天,包括本身的日期,格式为20140601之类public static String randNumdaybeforerandday(String str){String result = "";//SimpleDateFormat sdf =   new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");SimpleDateFormat sdf =   new SimpleDateFormat("yyyyMMdd");Date date = null;Random rand = new Random();try {date = sdf.parse(str);Calendar now =Calendar.getInstance();     now.setTime(date);     now.set(Calendar.DATE,now.get(Calendar.DATE)- rand.nextInt(3));   date = now.getTime();   result = sdf.format(date);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}}

0 0
原创粉丝点击