将文本文件读取为字符串总是会导致空字符串?
Reading a text file to a string ALWAYS results in empty string?
郑重声明,我知道将文本文件读取为字符串并不总是会导致空字符串,但在我的情况下,我无法让它执行任何其他操作。
我目前正在尝试编写一个程序,从 .txt 文件中读取文本,根据特定参数对其进行操作,然后将文本保存回文档中。无论我尝试了多少种不同的方法,我似乎都无法从 .txt 文件中获取文本。该字符串只是 returns 作为一个空字符串。
例如,我传入参数“-c 3 file1.txt”并解析文件的参数(文件总是最后传入)。我得到文件:
File inputFile = new File(args[args.length - 1]);
当我调试代码时,它似乎将该文件识别为 file1.txt,如果我传入另一个不存在的文件的名称,则会抛出错误。所以它正确地识别了这个文件。从这里开始,我尝试了我可以在网上找到的每种类型的文件文本解析,从旧的 Java 版本技术到 Java 8 种技术。 None 有效。我尝试过的一些是:
String fileText = "";
try {
Scanner input = new Scanner(inputFile);
while (input.hasNextLine()) {
fileText = input.nextLine();
System.out.println(fileText);
}
input.close();
} catch (FileNotFoundException e) {
usage();
}
或
String fileText = null;
try {
fileText = new String(Files.readAllBytes(Paths.get(filename)), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
我也试过其他的。缓冲阅读器、扫描仪等。我试过重新编译项目,我试过第 3 方库。仍然只是得到一个空字符串。我想这一定是某种配置问题,但我很难过。
对于任何想知道的人,文件似乎在正确的位置,当我引用错误的位置时会引发异常。而且文件中确实有文本。我已经四重检查了。
即使您的第一个代码片段可能会读取文件,但它实际上并没有将文件的内容存储在您的 fileText
变量中,而是 仅 文件的最后一个线。
有
fileText = input.nextLine();
您将 fileText
设置为 当前行 的内容,从而覆盖了 fileText
的先前值。您需要存储文件中的 all 行。例如。尝试
static String read( String path ) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
sb.append(line).append('\n');
}
}
return sb.toString();
}
我的建议是创建一种将文件读入字符串的方法,只要发现意外状态,该方法就会抛出带有描述性消息的异常。这是这个想法的可能实现:
public static String readFile(Path path) {
String fileText;
try {
if(Files.size(path) == 0) {
throw new RuntimeException("File has zero bytes");
}
fileText = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
if(fileText.trim().isEmpty()) {
throw new RuntimeException("File contains only whitespace");
}
return fileText;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
此方法检查 3 个异常:
- 找不到文件
- 文件为空
- 文件只包含空格
郑重声明,我知道将文本文件读取为字符串并不总是会导致空字符串,但在我的情况下,我无法让它执行任何其他操作。
我目前正在尝试编写一个程序,从 .txt 文件中读取文本,根据特定参数对其进行操作,然后将文本保存回文档中。无论我尝试了多少种不同的方法,我似乎都无法从 .txt 文件中获取文本。该字符串只是 returns 作为一个空字符串。
例如,我传入参数“-c 3 file1.txt”并解析文件的参数(文件总是最后传入)。我得到文件:
File inputFile = new File(args[args.length - 1]);
当我调试代码时,它似乎将该文件识别为 file1.txt,如果我传入另一个不存在的文件的名称,则会抛出错误。所以它正确地识别了这个文件。从这里开始,我尝试了我可以在网上找到的每种类型的文件文本解析,从旧的 Java 版本技术到 Java 8 种技术。 None 有效。我尝试过的一些是:
String fileText = "";
try {
Scanner input = new Scanner(inputFile);
while (input.hasNextLine()) {
fileText = input.nextLine();
System.out.println(fileText);
}
input.close();
} catch (FileNotFoundException e) {
usage();
}
或
String fileText = null;
try {
fileText = new String(Files.readAllBytes(Paths.get(filename)), StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
我也试过其他的。缓冲阅读器、扫描仪等。我试过重新编译项目,我试过第 3 方库。仍然只是得到一个空字符串。我想这一定是某种配置问题,但我很难过。
对于任何想知道的人,文件似乎在正确的位置,当我引用错误的位置时会引发异常。而且文件中确实有文本。我已经四重检查了。
即使您的第一个代码片段可能会读取文件,但它实际上并没有将文件的内容存储在您的 fileText
变量中,而是 仅 文件的最后一个线。
有
fileText = input.nextLine();
您将 fileText
设置为 当前行 的内容,从而覆盖了 fileText
的先前值。您需要存储文件中的 all 行。例如。尝试
static String read( String path ) throws IOException {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
for (String line = br.readLine(); line != null; line = br.readLine()) {
sb.append(line).append('\n');
}
}
return sb.toString();
}
我的建议是创建一种将文件读入字符串的方法,只要发现意外状态,该方法就会抛出带有描述性消息的异常。这是这个想法的可能实现:
public static String readFile(Path path) {
String fileText;
try {
if(Files.size(path) == 0) {
throw new RuntimeException("File has zero bytes");
}
fileText = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
if(fileText.trim().isEmpty()) {
throw new RuntimeException("File contains only whitespace");
}
return fileText;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
此方法检查 3 个异常:
- 找不到文件
- 文件为空
- 文件只包含空格