【问题标题】:PrintWriter not writing to file.[Java]PrintWriter 不写入文件。[Java]
【发布时间】:2016-02-25 01:27:49
【问题描述】:

我对这个作业代码有疑问。每当我运行它时,控制台不会显示任何内容,我可以编写任何我想要的内容,但脚本不会自行终止,就好像它处于无限循环中一样。更重要的是,输出文件也不写。我目前正在学习输入和输出流,所以我真的不知道我还能做些什么来解决这个问题。任何帮助将不胜感激。

public class PublicationListingProcess1 extends Publication implements Serializable{
static Publication PublicationArray[];
static String a, n, line;
static int y, c = 0, p, count = 0;
static long z;
static double s;

enum PublicationTypes{PUBLICATIONCODE, PUBLICATIONNAME, PUBLICATIONYEAR, PUBLICATIONAUTHORNAME, PUBLICATIONCOST, PUBLICATIONNBPAGES}

public static void main(String[] args)  {
    // TODO Auto-generated method stub
    try{
        File f = new File("PublicationData_Input.txt");
        Scanner input = new Scanner(f);
        while(input.hasNextLine())
        {
            c++;
        }
        PublicationArray = new Publication[c];

    System.out.println("Welcome to this organizing software.");
    System.out.print("Please enter the destiny file for output: ");
    Scanner kb = new Scanner(System.in);

    File outFile = new File (kb.next()+".txt");
    FileWriter output = new FileWriter (outFile);
    PrintWriter writer = new PrintWriter (output);

    while(input.hasNextLine())
    {
        String [] split = input.nextLine().split(" ");

        for(int i = 0; i < split.length; i++)
        {
            z = Long.parseLong(split[0]);
            n = split[1];
            y = Integer.parseInt(split[2]);
            a = split[3];
            s = Double.parseDouble(split[4]);
            p = Integer.parseInt(split[5]);
            PublicationArray[count] = new Publication(z, n, a, y, s, p);
            writer.println(PublicationArray[count]);
            count++;
        }

    }
output.close();
input.close();
    }

    catch(RuntimeException e)
    {
        e.getMessage();
    } 
    catch (FileNotFoundException ex) {
        // TODO Auto-generated catch block
        ex.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

}

【问题讨论】:

  • 你永远不会关闭PrintWriter
  • 你正在阅读一个空白文本文件..你的程序没有什么要解析的
  • 尝试传入一个包含 1 或 2 行示例的文本文件,看看您会遇到什么错误(如果有)
  • The file on its own it's a file provided by the teacher, which is read in the console: 900876512 Core_Java 2007 Mike_Simon 129.99 568 765867999 Java_Applications_for_Programmers 2010 David_Wilson_and_Jack_Westman 173.25 672 465979798 From_Java_to_C++ 2008 Linda_Jackson 118.73 439 760098908 Microsoft_VC++ 2006 Garry_Wesley 165.20 416 529086890 Software_Engineering 2005 Alain_Macmillan 219.99 651 765867999 Visual_Basic 2004 Mary_Rosen 108.33 388 但根本没有转移
  • 我还添加了以下几行: input.close();输出.flush();输出.close();但它仍然没有将任何内容写入文件。感谢您顺便回答。

标签: java arrays object file-io outputstream


【解决方案1】:

有问题的代码:

    while(input.hasNextLine())
    {
        c++;
    }

Scanner.hasNextLine() 确实将指针向前移动。因此,如果您的文件包含某些内容,那么您就有了一个无限循环。

要解决这个无限循环,只需删除这个 while 块。至少在发布的代码中,它与明显的含义无关。删除 while 块,并将 PublicationArray 从数组更改为 ArrayList

import java.util.*;

public class PublicationListingProcess1 extends Publication implements Serializable{
    static ArrayList<Publication> pubs;
    static String a, n, line;
    static int y, p, count = 0;
    static long z;
    static double s;

    enum PublicationTypes{
        PUBLICATIONCODE, 
        PUBLICATIONNAME, 
        PUBLICATIONYEAR, 
        PUBLICATIONAUTHORNAME, 
        PUBLICATIONCOST, 
        PUBLICATIONNBPAGES;
    }

    public static void main(String[] args)  {
    // TODO Auto-generated method stub
        try{
               File f = new File("PublicationData_Input.txt");
            Scanner input = new Scanner(f);

               pubs = new ArrayList<>();

            System.out.println("Welcome to this organizing software.");
            System.out.print("Please enter the destiny file for output: ");
            Scanner kb = new Scanner(System.in);

            File outFile = new File(kb.next()+".txt");
            FileWriter output = new FileWriter(outFile);
            PrintWriter writer = new PrintWriter(output);

            while (input.hasNextLine()) {
                String[] split = input.nextLine().split(" ");

                for (int i = 0; i < split.length; i++) {
                    z = Long.parseLong(split[0]);
                    n = split[1];
                    y = Integer.parseInt(split[2]);
                    a = split[3];
                    s = Double.parseDouble(split[4]);
                    p = Integer.parseInt(split[5]);
                    pubs.add(new Publication(z, n, a, y, s, p));
                    writer.println(pubs.get(count);
                    count++;
                }

            }
            output.close();
            input.close();
        } catch(RuntimeException e) {
            e.getMessage();
        } catch (FileNotFoundException ex) {
            // TODO Auto-generated catch block
            ex.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

上面的示例代码保留了您的主要控制流程,但可能有更好的方法。

【讨论】:

  • 感谢您的回答,但我使用该 while 循环来计算文档中有多少行,因为我必须将每一行作为信息输入到 PublicationArray 对象中。
  • 我确实发现了这个问题。发生的事情是我假设指针会自行重置。现在我打开了两次流,因为我知道重新打开流是不可能的。现在它确实起作用了,我现在想弄清楚的是如何将对象写入文件。感谢你的回答。没有考虑数组列表,感谢反馈!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-22
  • 1970-01-01
相关资源
最近更新 更多