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

时间:2014-10-27 08:41:23

标签: java io

我试图从以下代码编写输出文件。文件已成功创建但仍为空。我试过找到如何解决这个问题,我读到我必须添加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   

2 个答案:

答案 0 :(得分:1)

您的计划中存在逻辑错误:

  • 您可以在输入文件的每一行打开和关闭文件一次,而不是在第一行之前打开并在最后一行之后关闭。

注意:Line变量的名称不应以大写字母开头。

您的计划无需使用flush()close在关闭流/写入器之前刷新所有缓冲区。

答案 1 :(得分:1)

在您的代码中,您正在创建一个新的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;
}