gpt4 book ai didi

java - 如何在读取文件时删除空行

转载 作者:行者123 更新时间:2023-11-30 07:39:34 25 4
gpt4 key购买 nike

我想知道如何在读取/写入文件时删除空行?

for(int i=1;i<=count;i++) {
FileInputStream fistream1;
try {
name1="file1"+Integer.toString(i)+".txt";
fistream1 = new FileInputStream(name1); // first source file
fistream2 = new FileInputStream("Result.txt"); //second source file
sistream = new SequenceInputStream(fistream2, fistream1);
fostream= new PrintWriter(new BufferedWriter(new FileWriter( result+".txt", true)));

while( ( temp = sistream.read() ) != -1) {
fostream.write(temp); // to write to file
}

fostream.println("");
fostream.close();
sistream.close();
fistream1.close();
fistream2.close();
}

我找到了this code但由于以下行我无法实现它:

String line;
while((line = br.readLine())!= null) { ... }

他们使用 line 作为 String,但在我的例子中,我将 temp 作为 int:

int temp;
while((temp = sistream.read())!=-1) { ... }

有办法解决这个问题吗?

@Abdelhak 的回答是 Not Acceptable 。

最佳答案

你可以尝试这样的事情:

while( ( temp = sistream.read() ) != -1) {
if (!temp.trim().equals(""))
{
fostream.write(temp); // to write to file
}
...

关于java - 如何在读取文件时删除空行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34874597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com