java 有条件地从文件中读取行的程序
java program to conditionally read lines from file
我是 Java 中的编码新手。我将这段代码放在一起以读取以下文本文件中 "Start" 和 "End" 标记之间的所有行。
开始
嗨
你好
如何
是
你
在做什么?
结束
我的程序如下....
package test;
import java.io.*;
public class ReadSecurities {
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileInputStream in = new FileInputStream("U:\Read101.txt");
FileOutputStream out = new FileOutputStream("U:\write101.txt");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
for (int i=1; i<=countLines("U:\Read101.txt"); i++) {
String line=br.readLine();
while (line.contains("Start")) {
for (int j=i; j<=countLines("U:\Read101.txt"); j++) {
String line2=br.readLine();
System.out.println(line2);
if(line2.contains("End")) break;
else {
bw.write(line2);
bw.newLine();
}
bw.close();
} break;
}
}
br.close();
}
catch (Exception e) { }
finally { }
}
}
程序只读取前两行 "hi hello" 就好像 if 条件不存在一样。我感觉这个错误很基本,但请指正。
String line;
do{ line = br.readLine(); }
while( null != line && !line.equals("Start"));
if ( line.equals("Start") ) { // in case of EOF before "Start" we have to skip the rest!
do{
line = br.readLine();
if ( line.equals("End") ) break;
// TODO write to other file
}while(null != line )
}
应该就这么简单。为了简洁起见,我省略了资源的创建/销毁和适当的异常处理。
但请至少记录异常!
编辑:
如果在开始之前遇到EOF,就得跳过copy这一步了!
您在代码中犯了一个严重错误:您没有正确处理异常。两件事:
永远抓不到Exception
。要么只捕获一种类型的异常,要么指定要捕获的异常列表。在您的情况下,一个简单的 IOException
就足够了。
切勿将 catch 块留空。要么抛出一个新的异常,return 一个值,要么 - 在你的情况下 - 使用 e.printStackTrace()
.
打印异常
当您执行这两件事时,您会注意到您的代码抛出 IOException
,因为您过早地关闭了 bw
-Stream。将 bw.close()
向下移动到 br.close()
所在的位置。
现在,当您完成该操作后,您的代码就差不多可以工作了。唯一的问题是 - 你现在得到一个 NullPointerException
。这是因为在阅读所有条目后您没有更改 line
。解决这个问题的简单方法是从
while(line.equals("Start")) { ...
到
if(line.equals("Start")) { ...
此外,您的代码中还有其他一些不太整洁的地方,但我现在先不说了 - 经验是随着时间而来的。
对于Java 8:
List<String> stopWords = Arrays.asList("Start", "End");
try (BufferedReader reader = new BufferedReader(input))) {
List<String> lines = reader.lines()
.map(String::trim)
.filter(s -> !StringUtils.isEmpty(s) && !stopWords.contains(s))
.collect(Collectors.toList());
}
我是 Java 中的编码新手。我将这段代码放在一起以读取以下文本文件中 "Start" 和 "End" 标记之间的所有行。
开始
嗨
你好
如何
是
你
在做什么?
结束
我的程序如下....
package test;
import java.io.*;
public class ReadSecurities {
public static int countLines(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
boolean empty = true;
while ((readChars = is.read(c)) != -1) {
empty = false;
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n') {
++count;
}
}
}
return (count == 0 && !empty) ? 1 : count;
} finally {
is.close();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileInputStream in = new FileInputStream("U:\Read101.txt");
FileOutputStream out = new FileOutputStream("U:\write101.txt");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(out));
BufferedReader br = new BufferedReader(new InputStreamReader(in));
for (int i=1; i<=countLines("U:\Read101.txt"); i++) {
String line=br.readLine();
while (line.contains("Start")) {
for (int j=i; j<=countLines("U:\Read101.txt"); j++) {
String line2=br.readLine();
System.out.println(line2);
if(line2.contains("End")) break;
else {
bw.write(line2);
bw.newLine();
}
bw.close();
} break;
}
}
br.close();
}
catch (Exception e) { }
finally { }
}
}
程序只读取前两行 "hi hello" 就好像 if 条件不存在一样。我感觉这个错误很基本,但请指正。
String line;
do{ line = br.readLine(); }
while( null != line && !line.equals("Start"));
if ( line.equals("Start") ) { // in case of EOF before "Start" we have to skip the rest!
do{
line = br.readLine();
if ( line.equals("End") ) break;
// TODO write to other file
}while(null != line )
}
应该就这么简单。为了简洁起见,我省略了资源的创建/销毁和适当的异常处理。
但请至少记录异常!
编辑:
如果在开始之前遇到EOF,就得跳过copy这一步了!
您在代码中犯了一个严重错误:您没有正确处理异常。两件事:
永远抓不到
Exception
。要么只捕获一种类型的异常,要么指定要捕获的异常列表。在您的情况下,一个简单的IOException
就足够了。切勿将 catch 块留空。要么抛出一个新的异常,return 一个值,要么 - 在你的情况下 - 使用
e.printStackTrace()
. 打印异常
当您执行这两件事时,您会注意到您的代码抛出 IOException
,因为您过早地关闭了 bw
-Stream。将 bw.close()
向下移动到 br.close()
所在的位置。
现在,当您完成该操作后,您的代码就差不多可以工作了。唯一的问题是 - 你现在得到一个 NullPointerException
。这是因为在阅读所有条目后您没有更改 line
。解决这个问题的简单方法是从
while(line.equals("Start")) { ...
到
if(line.equals("Start")) { ...
此外,您的代码中还有其他一些不太整洁的地方,但我现在先不说了 - 经验是随着时间而来的。
对于Java 8:
List<String> stopWords = Arrays.asList("Start", "End");
try (BufferedReader reader = new BufferedReader(input))) {
List<String> lines = reader.lines()
.map(String::trim)
.filter(s -> !StringUtils.isEmpty(s) && !stopWords.contains(s))
.collect(Collectors.toList());
}