读取时文件名中的随机字符 - java
random character in file name while reading - java
我正在尝试从特定位置读取文本文件。但文件名末尾的字符未确定。
前任。可以是
Custom Summary Report -- UAT -- 2015-08-26d.txt
Custom Summary Report -- UAT -- 2015-08-26f.txt
Custom Summary Report -- UAT -- 2015-08-26.txt
Custom Summary Report -- UAT -- 2015-08-26c.txt
因此,最后一个字符可以是任何字母或没有字母。
我正在使用扫描仪读取该文本文件。在文件路径中我不知道要传递什么作为字符串。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String filepath = "C:/Custom Summary Report -- UAT -- "+dateFormat.format(date)+X+".txt";
// creating File instance to reference text file in Java
File text = new File(filepath);
// Creating Scanner instance to read File in Java
Scanner scnr = new Scanner(text);
在String文件路径中,字符X是可以是任何字符的字符。 (即任何字母表或什么都没有。)
因此,我在字符串文件路径中提到的 X 可以是我在特定位置不知道的任何内容。所以我想读那个文件。因为我不知道字符扫描器会抛出错误。
我想要一个代码,它可以在那个特定的地方接受任何字符并且可以读取文件。
试试这个:g 将是您要查找的文件,但只有当您查找的文件只有一个时它才有效:
File f = null;
File g = null;
File[] paths;
try{
// create new file
f = new File("c:/");
// returns pathnames for files and directory
paths = f.listFiles();
// for each pathname in pathname array
for(File file:paths) {
if(file.getAbsolutePath().startsWith("C:/Custom Summary Report -- UAT -- ") {
g=file;
System.out.println(g);
break;
}
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
最后我修改了解决方案并得出了这个答案。
File f = null;
File filepath = null;
File[] paths;
try{
// create new file
f = new File("C:/");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
// returns pathnames for files and directory
paths = f.listFiles();
// for each pathname in pathname array
for(File file:paths) {
if(file.getAbsolutePath().contains("Custom Summary Report -- UAT -- "+dateFormat.format(date))) {
filepath=file;
System.out.println(filepath);
break;
}
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
// Creating Scanner instance to read File in Java
Scanner scnr = new Scanner(filepath);
我正在尝试从特定位置读取文本文件。但文件名末尾的字符未确定。 前任。可以是
Custom Summary Report -- UAT -- 2015-08-26d.txt
Custom Summary Report -- UAT -- 2015-08-26f.txt
Custom Summary Report -- UAT -- 2015-08-26.txt
Custom Summary Report -- UAT -- 2015-08-26c.txt
因此,最后一个字符可以是任何字母或没有字母。
我正在使用扫描仪读取该文本文件。在文件路径中我不知道要传递什么作为字符串。
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
String filepath = "C:/Custom Summary Report -- UAT -- "+dateFormat.format(date)+X+".txt";
// creating File instance to reference text file in Java
File text = new File(filepath);
// Creating Scanner instance to read File in Java
Scanner scnr = new Scanner(text);
在String文件路径中,字符X是可以是任何字符的字符。 (即任何字母表或什么都没有。)
因此,我在字符串文件路径中提到的 X 可以是我在特定位置不知道的任何内容。所以我想读那个文件。因为我不知道字符扫描器会抛出错误。
我想要一个代码,它可以在那个特定的地方接受任何字符并且可以读取文件。
试试这个:g 将是您要查找的文件,但只有当您查找的文件只有一个时它才有效:
File f = null;
File g = null;
File[] paths;
try{
// create new file
f = new File("c:/");
// returns pathnames for files and directory
paths = f.listFiles();
// for each pathname in pathname array
for(File file:paths) {
if(file.getAbsolutePath().startsWith("C:/Custom Summary Report -- UAT -- ") {
g=file;
System.out.println(g);
break;
}
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
最后我修改了解决方案并得出了这个答案。
File f = null;
File filepath = null;
File[] paths;
try{
// create new file
f = new File("C:/");
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
// returns pathnames for files and directory
paths = f.listFiles();
// for each pathname in pathname array
for(File file:paths) {
if(file.getAbsolutePath().contains("Custom Summary Report -- UAT -- "+dateFormat.format(date))) {
filepath=file;
System.out.println(filepath);
break;
}
}
}catch(Exception e){
// if any error occurs
e.printStackTrace();
}
// Creating Scanner instance to read File in Java
Scanner scnr = new Scanner(filepath);