[英]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
. 而是使用Path
和Files
。 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模式是熟悉的模式,如*.java
或Name-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.