gpt4 book ai didi

java - 空输出文件(flush() 和 close() 已添加)

转载 作者:行者123 更新时间:2023-12-02 05:20:03 25 4
gpt4 key购买 nike

我正在尝试从以下代码编写输出文件。文件已成功创建,但仍为空。我尝试寻找如何解决这个问题,我读到我必须添加flush()或close()。我尝试将flush()和close()放入代码中,但它仍然不起作用,我似乎无法弄清楚为什么。

下面是代码:

import java.util.Scanner;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Citation2{

String title;
String author;
String year;
String conference;
String index;
String cite;
String abstracts;
String Line;
private final Path fFilePath;
private final static Charset ENCODING = StandardCharsets.UTF_8;

public static void main (String[] args) throws SQLException,
ClassNotFoundException, IOException{

Citation2 parser = new Citation2("D:/test.txt");
parser.processLineByLine();

}

public Citation2(String aFileName){
fFilePath = Paths.get(aFileName);
}

public final void processLineByLine() throws IOException, ClassNotFoundException, SQLException {
try (Scanner scanner = new Scanner(fFilePath, ENCODING.name())){
String nextLine = null;
if(scanner.hasNextLine()){
nextLine = scanner.nextLine();
}
while (nextLine!=null){
nextLine = processLine(nextLine, scanner);
}
}
}

protected String processLine(String aLine, Scanner scanner) throws FileNotFoundException, SQLException {

String nextLine = null;
PrintWriter out = new PrintWriter("D:/output.txt");


try{
if (aLine.startsWith("#*")) {
title = aLine.substring(2);
Line = title;
}
else if (aLine.startsWith("#@")){
author = aLine.substring(2);
Line = Line + "\t" + author;
}
else if (aLine.startsWith("#t")){
year = aLine.substring(2);
Line = Line + "\t" + year;

}
else if (aLine.startsWith("#c")){
conference = aLine.substring(2);
Line = Line + "\t" + conference;
}
else if (aLine.startsWith("#index")){
index = aLine.substring(6);
Line = Line + "\t" + index;
}
else if (aLine.startsWith("#%")){
cite = aLine.substring(2);
while(scanner.hasNextLine() && (nextLine = scanner.nextLine()).startsWith("#%"))
{
cite += "," + nextLine.substring(2);
}
Line = Line + "\t" + cite;

out.write(Line);
}
if(nextLine== null && scanner.hasNextLine()){
nextLine = scanner.nextLine();
}
}
catch(Exception e){
}
finally{
out.flush();
out.close();
}
return nextLine;
}

我也尝试过这个,但效果不佳。

  else if (aLine.startsWith("#%")){
cite = aLine.substring(2);
while(scanner.hasNextLine() && (nextLine = scanner.nextLine()).startsWith("#%"))
{
cite += "," + nextLine.substring(2);
}
Line = Line + "\t" + cite;

out.write(Line);
out.flush();

}
if(nextLine== null && scanner.hasNextLine()){
nextLine = scanner.nextLine();
}
}
catch(Exception e){
}
finally{
out.close();
}
return nextLine;
}

有人说我可以使用 out.println() 因为它会自动刷新,但它也不起作用。

输入文件如下:

#*A strategy for acquiring customer requirement patterns using laddering technique and ART2 neural network.
#@Chun-Hsien Chen,Li Pheng Khoo,Wei Yan
#t2002
#cAdvanced Engineering Informatics
#index743424
#%
#!

#*Usable autonomic computing systems: The system administrators' perspective.
#@Rob Barrett,Paul P. Maglio,Eser Kandogan,John H. Bailey
#t2005
#cAdvanced Engineering Informatics
#index743458
#%121622
#%635878
#%806957
#%892618
#!

#*Ant colony optimization techniques for the vehicle routing problem.
#@John E. Bell,Patrick R. McMullen
#t2004
#cAdvanced Engineering Informatics
#index743464
#%
#!

当我尝试 System.out.println() 时,它给出了我想要的输出,如下所示:

A strategy for acquiring customer requirement patterns using laddering technique and ART2 neural network.   Chun-Hsien Chen,Li Pheng Khoo,Wei Yan   2002    Advanced Engineering Informatics    743424   
Usable autonomic computing systems: The system administrators' perspective. Rob Barrett,Paul P. Maglio,Eser Kandogan,John H. Bailey 2005 Advanced Engineering Informatics 743458 121622,635878,806957,892618
Ant colony optimization techniques for the vehicle routing problem. John E. Bell,Patrick R. McMullen 2004 Advanced Engineering Informatics 743464

最佳答案

在您的代码中,您将创建一个新的 PrintWriter 并写入每一行并将其关闭。这将覆盖以前的写入。因此,我将 PrintWriter 创建移至只有一个。

public final void processLineByLine() throws IOException, ClassNotFoundException, SQLException {
try (Scanner scanner = new Scanner(fFilePath, ENCODING.name())){
String nextLine = null;
if(scanner.hasNextLine()){
nextLine = scanner.nextLine();
}
PrintWriter out = new PrintWriter("D:/output.txt");
while (nextLine!=null){
nextLine = processLine(nextLine, scanner, out);
}
out.close();
}
}

protected String processLine(String aLine, Scanner scanner, PrintWriter out) throws FileNotFoundException, SQLException {

String nextLine = null;

if (aLine.startsWith("#*")) {
title = aLine.substring(2);
Line = title;
}
else if (aLine.startsWith("#@")){
author = aLine.substring(2);
Line = Line + "\t" + author;
}
else if (aLine.startsWith("#t")){
year = aLine.substring(2);
Line = Line + "\t" + year;

}
else if (aLine.startsWith("#c")){
conference = aLine.substring(2);
Line = Line + "\t" + conference;
}
else if (aLine.startsWith("#index")){
index = aLine.substring(6);
Line = Line + "\t" + index;
}
else if (aLine.startsWith("#%")){
cite = aLine.substring(2);
while(scanner.hasNextLine() && (nextLine = scanner.nextLine()).startsWith("#%"))
{
cite += "," + nextLine.substring(2);
}
Line = Line + "\t" + cite;

out.append(Line);
}
if(nextLine== null && scanner.hasNextLine()){
nextLine = scanner.nextLine();
}
return nextLine;
}

关于java - 空输出文件(flush() 和 close() 已添加),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26583743/

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