Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 仅写入(文件中)LinkedList中的最后一个数据_Java_Linked List_Filewriter - Fatal编程技术网

Java 仅写入(文件中)LinkedList中的最后一个数据

Java 仅写入(文件中)LinkedList中的最后一个数据,java,linked-list,filewriter,Java,Linked List,Filewriter,我正在尝试编写一个名为movie.txt的文件,但不幸的是,它只在LinkedList中存储了最后一个文件。我应该用什么来让它逐行存储它们,因为在这之后我需要它作为输入文件 import javax.swing.*; import java.io.*; public class movie508 { public static void main(String[] args) throws IOException { LinkedList listMovie =

我正在尝试编写一个名为movie.txt的文件,但不幸的是,它只在LinkedList中存储了最后一个文件。我应该用什么来让它逐行存储它们,因为在这之后我需要它作为输入文件

import javax.swing.*;
import java.io.*;

public class movie508
{
    public static void main(String[] args) throws IOException
    {
        LinkedList listMovie = new LinkedList();
        int size = Integer.parseInt(JOptionPane.showInputDialog("Enter number of movie: "));
        Movie m;

        for(int i = 0; i < size; i++)
        {
            String a = JOptionPane.showInputDialog("Enter name : ");
            int b = Integer.parseInt(JOptionPane.showInputDialog("Enter year : "));
            String c = JOptionPane.showInputDialog("Enter LPF rating : ");
            int d = Integer.parseInt(JOptionPane.showInputDialog("Enter time"));
            String e = JOptionPane.showInputDialog("Enter genre : ");
            double f = Double.parseDouble(JOptionPane.showInputDialog("Enter member rating : "));

            m = new Movie(a, b, c, d, e, f);
            listMovie.insertAtFront(m);
        }

        Object data = listMovie.getFirst();
        PrintWriter out = null;

        while(data != null)
        {
            m = (Movie)data;

            try {

                out = new PrintWriter(new FileWriter("movie.txt"));
                out.write(m.toString());
            } 
            finally 
            {
                if (out != null) {
                    out.close();
                }
            }
            data = listMovie.getNext();
        }

    }}
import javax.swing.*;
导入java.io.*;
公共级电影508
{
公共静态void main(字符串[]args)引发IOException
{
LinkedList listMovie=新建LinkedList();
int size=Integer.parseInt(JOptionPane.showInputDialog(“输入电影数量:”);
电影m;
对于(int i=0;i
while
循环的每次迭代中,您都会重新打开文件,从而覆盖它。在循环之前打开一次,然后在循环结束时关闭:

PrintWriter out = null;
try {
    out = new PrintWriter(new FileWriter("movie.txt"));
    while(data != null) {
        m = (Movie)data;
        out.println(m.toString());
        data = listMovie.getNext();
    }
} 
finally {
    if (out != null) {
        out.close();
    }
}

我试图在每个数据之后添加新行,但错误是新行具有对java.io.printwriter的私有访问权限。如何添加新行?只需使用,而不是在我的答案中写入编辑。
In java FileWriter api is as below:

public FileWriter(String fileName,
          boolean append)
           throws IOException
Constructs a FileWriter object given a file name with a boolean indicating whether or not to append the data written.
Parameters:
fileName - String The system-dependent filename.
append - boolean if true, then data will be written to the end of the file rather than the beginning. 
Throws: 
IOException - if the named file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason.

So if you want to append just make the append parameter true as below:

out = new PrintWriter(new FileWriter("movie.txt",true));

This will append the text to existing file instead of over writing.