繁体   English   中英

PrintWriter未写入文件。[Java]

[英]PrintWriter not writing to file.[Java]

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

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();
    }

}

}

有问题的代码:

    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();
        }
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM