简体   繁体   English

Java - 我怎么说我不关心这部分字符串包含什么?

[英]Java - How do I say I don't care what this part of the string contains?

I've got a bit of code, where I get the timestamp, however I want to be able to say that I don't care what seconds it is, so... this is the code I have: 我有一些代码,我得到了时间戳,但是我希望能够说我不关心它的秒数,所以...这是我的代码:

    String timeStampString = new SimpleDateFormat("yyyyMMddHHmm").format(Calendar.getInstance().getTime());
    File fileName = new File(universal + "/Downloads/" + timeStampString);
    String newName = "allPi.csv";
    if (fileName.exists()) {
        fileName.renameTo(new File(universal + "/Downloads/allPi.csv"));
    }
    else
    {
        Assert.fail();
    }

The downloaded file will have a full timestamp down to seconds, how do I say that I don't care what numbers are in the seconds part, like timeStampString + "**"; 下载的文件将有一个完整的时间戳,只有秒,我怎么说我不关心秒部分的数字,如timeStampString +“**”; or something?? 或者其他的东西??

I've tried a few random things like: 我尝试了一些随机的东西,比如:

   File fileName = new File(universal + "/Downloads/" + timeStampString + "[.+]");

Can anyone help? 有人可以帮忙吗?

First, don't use the File class. 首先,不要使用File类。 It is an outdated, badly written class which should not be used in new code, as better alternatives have existed since Java 1.4 or so. 它是一个过时的,编写得很糟糕的类,不应该在新代码中使用,因为自Java 1.4以来存在更好的替代方案。

Instead, use Path and Files . 而是使用PathFiles They have many methods that allow you to do common tasks, and in addition have better error reporting. 它们有许多方法可以让您执行常见任务,此外还有更好的错误报告。

In this particular case, you can use a DirectoryStream , which comes from the same family of classes ( java.nio.file ). 在这种特殊情况下,您可以使用DirectoryStream ,它来自同一个类系列( java.nio.file )。 A DirectoryStream is an iterable object of files in a directory. DirectoryStream是目录中文件的可迭代对象。 And the Files class allows you to get a DirectoryStream of all the files in the directory that match a glob pattern. Files类允许您获取目录中与glob模式匹配的所有文件的DirectoryStream Glob patterns are the familiar patterns like *.java or Name-xyz-?.txt . Glob模式是熟悉的模式,如*.javaName-xyz-?.txt

In your case you assume that you'll have only one entry. 在您的情况下,您假设您只有一个条目。 So you just need the first entry in the associated iterator. 所以你只需要相关迭代器中的第一个条目。

Path dir = Paths.get( universal, "Downloads" );
Path renameTo = Paths.get( universal, "Downloads", "allPi.txt" );
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, timeStampString + "*" )) {
    Iterator<Path> iterator = stream.iterator();
    if ( iterator.hasNext() ) {
        Path path = iterator.next();
        Files.move( path, renameTo, StandardCopyOption.REPLACE_EXISTING );
    }
}

Of course, have try-catch for any exceptions. 当然,有任何例外的try-catch。

You could use the java.nio.file API instead: 您可以使用java.nio.file API:

Path directory = Paths.get(universal, "Downloads");
Path path = Files.list(directory).filter((Path p) -> {
                String fileName = p.getFileName().toString();
                return fileName.startsWith(timeStampString);
            })
            .findAny()
            .orElse(null);

if (path == null) {
     Assert.fail();
} else {
    Files.move(path, directory.resolve("allPi.csv"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查两个ArrayList是否不同,我不在乎改变了什么 - How can I check if two ArrayList differ, I don't care what's changed 如何截断Java中字符串的一部分? - How do I truncate a part of the string in Java? 如果该可选部分包含字符串结尾 ($),如何使正则表达式部分可选并在同一点完成正则表达式? - How do I make a regex part optional and finish regex at the same point if that optional part contains a string end ($)? CDI注入歧义依赖“任意选择,我不在乎” - CDI Inject Ambiguous Dependency “pick any, i don't care” 如何检查字符串是否包含Java中所有重复的字母? - How do I check if a string contains all repeated letters in Java? 如何在java中为拆分字符串分配多个对象? - How do I assign multiple objects a part of a split string in java? 我不想在java中使用“instanceof”,我该怎么办? - I don't want to use “instanceof” in java , what do I do? 如何将xml字符串转换为对象。 我不知道什么对象提前。 只是几个可能性 - how do I convert an xml string to an object. I don't know what object ahead of time. Just several possibities 这个方法是否链接在 java 中? 我不明白链接的某些部分 - Is this method chaining in java? I don't understand some part of chaining 如果没有上下文该怎么办? - What to do if I don't have Context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM