猿问

如何使用 java 搜索文件中存在的日期格式 20180603?

我有一个文件,其中的单词由“|”分隔。这里我需要搜索日期“20180603”。但是,我无法硬编码要搜索的值。日期格式固定为 YYYYDDMM,日期可以是任何格式。我需要将此处显示的日期转换为今天的日期(系统日期)。


我正在粘贴外部文件的外观(只是我在相关值周围添加了星号以强调):


00000548|WILLIAM|HUBER|WH5718||N|**20180306**|SVP-TECHNICAL FIELD SERVICES|06|329000.00 |0.00 |0.00 |205440.00 |0.00 |0.00 |0.00 |0.00 |0.00 |55000.00 |0.00 |0.00 |0.00 |1600.00 |0.00 |0.00 |0.00 |0.00 |225502.08 |0.00 |0.00 |0.00 |27629.91 |36717.17 |0.00 |33.000 |0.000 |F

00000828|NORBERTA|NOGUERA|NN1413||N|**20180306**|VP-SPECIAL PROJECTS|05|213000.00 |0.00 |88464.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |0.00 |86502.87 |0.00 |0.00 |0.00 |16811.41 |15023.40 |0.00 |33.000 |0.000 |F

00001308|ERROL|PHIPPS|EP4499||N|00000548|WILLIAM|HUBER|WH5718||N|20180306|SVP-TECHNICAL FIELD SERVICES|06|329000.00 |0.00 |0.00 |205440.00 |0.00 |0.00 |0.00 |0.00 |0.00 |55000.00 |0.00 |0.00 |0.00 |1600.00 |0.00 |0.00 |0.00 |0.00 |225502.08 |0.00 |0.00 |0.00 |27629.91 |36717.17 |0.00 |33.000 |0.000 |F

我尝试了很多问题,但没有运气。


下面是我试过的代码;


public class ReadFile {


    public static void main(String[] args) throws IOException {

        File f1= new File("C:/Users/kumar.sushobhan/Desktop/ESPYTR_Big_file_EXEC.dat");

        //File f1= new File("C:/Users/kumar.sushobhan/Desktop/h.txt");

        String words[]= null;

        FileReader fr= new FileReader(f1);

        BufferedReader br= new BufferedReader(fr);


        String s;

        int c = 0;


        String regex= "\\d{4}\\d{2}\\d{2}";

        while((s= br.readLine())!=null)

        {

            words= s.split("|");

            for(String word: words)

            {

                //System.out.println(word);

                if(word.equals(regex))

                {

                    c++;

                }

            }

        }

        System.out.println(c);

        fr.close();

    }


}

我希望读取快照中存在的日期并将其更改为当前系统日期。


翻过高山走不出你
浏览 146回答 2
2回答

白衣非少年

这是一个基本算法,它将在管道分隔文件中查找,将“看起来像”日期的值替换为当前日期,然后将所有内容写回新文件。它使用YYYYDDMM您在问题中描述的格式,但它可能应该是YYYYMMDD,我已经注意到您需要在哪里进行更改。这通过日期验证和错误处理减少了一些角落,以尽量保持相对较短,但我过度评论以尝试解释所有内容:import java.io.BufferedReader;import java.io.BufferedWriter;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.time.LocalDate;import java.time.format.DateTimeFormatter;import java.util.regex.Matcher;import java.util.regex.Pattern;public class DateReplacer{&nbsp; &nbsp; private static final Pattern DATE_MATCHER =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Pattern.compile("(?:(?:19|20)[0-9]{2})([0-9]{2})([0-9]{2})");&nbsp; &nbsp; public static void main(String... args)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws Exception&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // These are the paths to our input and output files&nbsp; &nbsp; &nbsp; &nbsp; Path input = Paths.get("input.dat");&nbsp; &nbsp; &nbsp; &nbsp; Path output = Paths.get("output.dat");&nbsp; &nbsp; &nbsp; &nbsp; // We need to get today's date in YYYYDDMM format, so we create a&nbsp; &nbsp; &nbsp; &nbsp; // DateFormatter for that. If it turns out that your date format is&nbsp; &nbsp; &nbsp; &nbsp; // actually YYYYMMDD, you can just use DateFormatter.BASIC_ISO_DATE&nbsp; &nbsp; &nbsp; &nbsp; // instead.&nbsp; &nbsp; &nbsp; &nbsp; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyddMM");&nbsp; &nbsp; &nbsp; &nbsp; String todaysDate = LocalDate.now().format(formatter);&nbsp; &nbsp; &nbsp; &nbsp; // Use try-with-resources to create a reader & writer&nbsp; &nbsp; &nbsp; &nbsp; try (BufferedReader reader = Files.newBufferedReader(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BufferedWriter writer = Files.newBufferedWriter(output)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String line;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Read lines until there are no more lines&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((line = reader.readLine()) != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Split them on the | character, notice that it needs to be&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // escaped because it is a regex metacharacter&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] columns = line.split("\\|");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Iterate over every column...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < columns.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ... and if the value looks like a date ...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (isDateLike(columns[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ... overwrite with today's date.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columns[i] = todaysDate;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Re-join the columns with the | character and write it out&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.write(String.join("|", columns));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; writer.newLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static boolean isDateLike(String str)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Avoid the regular expression if we can&nbsp; &nbsp; &nbsp; &nbsp; if (str.length() != 8) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; Matcher matcher = DATE_MATCHER.matcher(str);&nbsp; &nbsp; &nbsp; &nbsp; if (matcher.matches()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If it turns out that your date format is actually YYYYMMDD&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // you will need to swap these two lines.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int day = Integer.parseInt(matcher.group(1), 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int month = Integer.parseInt(matcher.group(2), 10);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // We don't need to validate year because we already know&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // it is between 1900 and 2099 inclusive&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return day >= 1 && day <= 31 && month >= 1 && month <= 12;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; }}此示例使用一条try-with-resources语句来确保正确关闭输入和输出文件。

繁花如伊

您可以使用如下正则表达式。&nbsp;&nbsp;String&nbsp;regex&nbsp;=&nbsp;"(19|20)[0-9][0-9](0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|30|31)";它并不完美,但可以匹配大多数日期。例如,它将消除月份超过 12 的日期。此外,它适用于 2099 年之前的日期。它不处理像 6 月有 30 天这样的日期规则。它将匹配天数在 1-31 之间的任何日期。您不能用于equals日期。你将不得不使用Pattern.matches(regex, string)
随时随地看视频慕课网APP

相关分类

Java
我要回答