在Java中将数组写入文件

发布于 2024-11-04 02:03:16 字数 775 浏览 1 评论 0原文

您好,我已经查看了我能找到的所有其他与此类似的问题,但无法解决我的上一个问题,尽管其他问题的答案让我到目前为止。

我正在尝试将数组写入文件,到目前为止我可以做到这一点,但作者只是将所有内容写入同一行。由于某种原因,在记事本中查看时它不会接受我的新行命令(\n)。

尝试解释的示例:

数组内容: test、test2、test3、test4

写入文件

文件内容: test test2 test3 test4

而我希望文件为: 测试 测试2 测试3 test4

下面是我要写入文件的代码段。

    public void save(String fileName) throws FileNotFoundException {

    try {

        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        for ( int i = 0; i < nbrMovies; i++)
        {      
        writer.write(movies[i].getName() + " \n");
        }
        writer.close();
    } catch(IOException ex) {
        ex.printStackTrace();
    }
}

该文件不接受“\n”命令,而是仅在最后添加的电影旁边放置一个小框。

任何和所有的帮助将不胜感激。

Hi i've looked at all the other questions similar to this that I could find and haven't been able to fix my last problem, though the answers to other questions got me this far.

I'm trying to write an array to a file, and so far I can do that, but the writer just writes everything on the same line. For some reason it won't accept my new line command (\n) when viewed in notepad.

An example to try and explain:

Array contents: test, test2, test3, test4

Write to file

File contents: test test2 test3 test4

Whereas i want the file to be:
test
test2
test3
test4

Below is the segment of my code to write to the file.

    public void save(String fileName) throws FileNotFoundException {

    try {

        BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
        for ( int i = 0; i < nbrMovies; i++)
        {      
        writer.write(movies[i].getName() + " \n");
        }
        writer.close();
    } catch(IOException ex) {
        ex.printStackTrace();
    }
}

The file doesn't accept the "\n" command and instead just puts a little box next to the last movie added.

Any and all help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

影子是时光的心 2024-11-11 02:03:16

首先,您不应该在每次写入后 close() 流;将 close() 移到循环之外,最好移到 finally 块中以确保调用它。其次,与其写“\n”,更好的做法是

writer.write(movies[i].getName());
writer.newLine();
writer.flush(); //optional

First of all, you shouldn't close() the stream after every write; move the close() outside your loop, preferably into a finally block to ensure it is called. Second, rather than writing "\n", a better approach is

writer.write(movies[i].getName());
writer.newLine();
writer.flush(); //optional
清音悠歌 2024-11-11 02:03:16

writer.close(); 从 for 循环中取出。你的代码应该是这样的:-

 public void save(String fileName) throws FileNotFoundException {
    BufferedWriter writer = null;
    try {

        writer = new BufferedWriter(new FileWriter(fileName));
        for ( int i = 0; i < nbrMovies; i++)
        {      
          writer.write(movies[i].getName());
          writer.newLine();
      writer.flush();
        }

    } catch(IOException ex) {
        ex.printStackTrace();
    } finally{
        if(writer!=null){
            writer.close();
        }  
    }
}

Take writer.close(); out of the for loop. Your code should be like this:-

 public void save(String fileName) throws FileNotFoundException {
    BufferedWriter writer = null;
    try {

        writer = new BufferedWriter(new FileWriter(fileName));
        for ( int i = 0; i < nbrMovies; i++)
        {      
          writer.write(movies[i].getName());
          writer.newLine();
      writer.flush();
        }

    } catch(IOException ex) {
        ex.printStackTrace();
    } finally{
        if(writer!=null){
            writer.close();
        }  
    }
}
梦幻的心爱 2024-11-11 02:03:16

有这个 writer.newLine(); 并将调用移出循环以关闭。

There is this writer.newLine(); and move the call to close out of the loop.

剩余の解释 2024-11-11 02:03:16
import java.io.*;

public class WriteChar 
{

    public static void main(String[] args) 
    {

        try
    {
       File f=new File("WriteChar.txt");
      char c[]={'a','b','c','d','e','f','g'};


       FileWriter out=new FileWriter(f);
       out.write(c);
       System.out.println("Done ..........");
       out.close();
    }

        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

}
import java.io.*;

public class WriteChar 
{

    public static void main(String[] args) 
    {

        try
    {
       File f=new File("WriteChar.txt");
      char c[]={'a','b','c','d','e','f','g'};


       FileWriter out=new FileWriter(f);
       out.write(c);
       System.out.println("Done ..........");
       out.close();
    }

        catch(IOException ioe)
        {
            ioe.printStackTrace();
        }
    }

}
橪书 2024-11-11 02:03:16

用 PrintWriter 包装 FileWriter 或 BufferedWriter,您将可以访问 println(...) 方法。

Wrap the FileWriter or BufferedWriter with a PrintWriter, and you will have access to the println(...) method.

温柔戏命师 2024-11-11 02:03:16

将 writer.close() 移到循环之外。另外,请确保在关闭 writer 之前将其刷新。

Move the writer.close() outside the loop. Also, make sure that you flush the writer before closing it.

梦明 2024-11-11 02:03:16

为什么不在循环外调用 close 呢?

Writer writer = null;
try {
    writer = new BufferedWriter(new FileWriter(fileName));
    for (Movie movie : movies) {      
        writer.write(movie.getName() + " \n");
    }
} catch(IOException ex) {
    ex.printStackTrace();
} finally {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException ignored) {
        }
    }
}

Why don't you call close outside the loop?

Writer writer = null;
try {
    writer = new BufferedWriter(new FileWriter(fileName));
    for (Movie movie : movies) {      
        writer.write(movie.getName() + " \n");
    }
} catch(IOException ex) {
    ex.printStackTrace();
} finally {
    if (writer != null) {
        try {
            writer.close();
        } catch (IOException ignored) {
        }
    }
}
那伤。 2024-11-11 02:03:16

我同意将 writer.close() 移到循环之外。但是有一种更简单的方法可以完成您想要的操作 - 无需调用换行函数,只需在语句末尾附加回车符而不是 return

更改语句:writer.write(movies[i].getName() + " \n");
到: writer.write(movies[i].getName() + " \r\n");

I agree with moving the writer.close() outside the loop. But there is a simpler way of doing what you want - rather than calling the newline function, just append carriage return instead of return at the end of the statement
i.e.

change the statement: writer.write(movies[i].getName() + " \n");
to: writer.write(movies[i].getName() + " \r\n");

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文