从 Java 中的文件读取 windows 文件名
Read windows filename from file in Java
背景
对于我正在编写的程序,我需要能够从文件中读取 Windows 文件名。不幸的是,Windows 使用 \
而不是 /
,这使得这很棘手。我一直在尝试不同的方法,但它似乎从来没有奏效。这是 Java 代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test {
static String localFile;
static String localFilePrefix;
static String user;
public static void main(String[] args){
readConfig("user.txt");
}
public static boolean readConfig(String cfgFilePath){
try{
BufferedReader reader = new BufferedReader(new FileReader(cfgFilePath));
try{
String line;
while((line = reader.readLine()) != null){
if(line.indexOf("User") != -1){
user = line.substring(line.indexOf(" ")+1);
}else if(line.indexOf("LocalFile") != -1){
String tmp = line.substring(line.indexOf(" ")+1);
System.out.println("Test: " + tmp);
setLocalFile(tmp);
}
}
}catch(IOException ee){
System.err.println(ee.getMessage());
}
}catch(FileNotFoundException e){
System.err.println(e.getMessage());
}
return true;
}
public static void setLocalFile(String lFileName){
System.out.println("FileName: " + lFileName);
localFile = lFileName;
if(new File(localFile).isDirectory()){
System.out.println("Here!");
localFilePrefix=localFile+File.separator;
}
}
}
这是配置文件:
User test
LocalFile C:\User
运行 这段代码和那个文件路径并没有打印出它应该打印的 Test: C:\Users
。它也不打印 FileName: C:\Users
或 Here!
。但是,如果我从文件路径中删除 "Users",它可以正常工作并打印它应该打印的所有内容。它甚至将 C:\
识别为目录。
问题
我不希望用户因为我的程序无法处理而被迫以特殊格式写入文件路径。那么我该如何解决这个问题?
你的第一个条件 line.indexOf("User") != -1
是 true
输入 User test
也是 LocalFile C:\User
(对于包含 [=15= 的每个路径都是如此) ]).因此,不评估 else if
条件。
使用.startsWith
instead of .indexOf
while ((line = reader.readLine()) != null) {
if (line.startsWith("User")) {
user = line.substring(line.indexOf(" ") + 1);
} else if (line.startsWith("LocalFile")) {
String tmp = line.substring(line.indexOf(" ") + 1);
System.out.println("Test: " + tmp);
setLocalFile(tmp);
}
}
背景
对于我正在编写的程序,我需要能够从文件中读取 Windows 文件名。不幸的是,Windows 使用 \
而不是 /
,这使得这很棘手。我一直在尝试不同的方法,但它似乎从来没有奏效。这是 Java 代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Test {
static String localFile;
static String localFilePrefix;
static String user;
public static void main(String[] args){
readConfig("user.txt");
}
public static boolean readConfig(String cfgFilePath){
try{
BufferedReader reader = new BufferedReader(new FileReader(cfgFilePath));
try{
String line;
while((line = reader.readLine()) != null){
if(line.indexOf("User") != -1){
user = line.substring(line.indexOf(" ")+1);
}else if(line.indexOf("LocalFile") != -1){
String tmp = line.substring(line.indexOf(" ")+1);
System.out.println("Test: " + tmp);
setLocalFile(tmp);
}
}
}catch(IOException ee){
System.err.println(ee.getMessage());
}
}catch(FileNotFoundException e){
System.err.println(e.getMessage());
}
return true;
}
public static void setLocalFile(String lFileName){
System.out.println("FileName: " + lFileName);
localFile = lFileName;
if(new File(localFile).isDirectory()){
System.out.println("Here!");
localFilePrefix=localFile+File.separator;
}
}
}
这是配置文件:
User test
LocalFile C:\User
运行 这段代码和那个文件路径并没有打印出它应该打印的 Test: C:\Users
。它也不打印 FileName: C:\Users
或 Here!
。但是,如果我从文件路径中删除 "Users",它可以正常工作并打印它应该打印的所有内容。它甚至将 C:\
识别为目录。
问题
我不希望用户因为我的程序无法处理而被迫以特殊格式写入文件路径。那么我该如何解决这个问题?
你的第一个条件 line.indexOf("User") != -1
是 true
输入 User test
也是 LocalFile C:\User
(对于包含 [=15= 的每个路径都是如此) ]).因此,不评估 else if
条件。
使用.startsWith
instead of .indexOf
while ((line = reader.readLine()) != null) {
if (line.startsWith("User")) {
user = line.substring(line.indexOf(" ") + 1);
} else if (line.startsWith("LocalFile")) {
String tmp = line.substring(line.indexOf(" ") + 1);
System.out.println("Test: " + tmp);
setLocalFile(tmp);
}
}