将数组写入文件

发布于 2024-10-14 04:20:42 字数 776 浏览 0 评论 0原文

好的,我已经上课了(对格式感到抱歉,不太熟悉该网站)

import java.io.*;  
public class writingArray  
{  
    public static void writeToFile(String fileName, String[] input) {  
        PrintWriter printWriter;  
        try {    
            printWriter = new PrintWriter(new FileOutputStream(fileName, true));
            int length = input.length;    
            int i;  
            for(i = 0; ++i < length; i++) {  
                printWriter.println(input[i]);  
            }   
            printWriter.close();    
        }  
        catch(IOException e) {  
            System.out.println(e.getMessage());  
        }  
    }  
}

问题是我想将一个数组写入一个文件,每个条目都在单独的行上,并且我想擦除该文件从每次写入开始。有什么建议吗?因为我所能做的就是写入数组的最后一个条目,同时擦除所有先前的条目,但我可以在最后一个条目下方多次写入。

谢谢^^

Okay so I've got my class ( sorry about the formatting, not too familiar with the site )

import java.io.*;  
public class writingArray  
{  
    public static void writeToFile(String fileName, String[] input) {  
        PrintWriter printWriter;  
        try {    
            printWriter = new PrintWriter(new FileOutputStream(fileName, true));
            int length = input.length;    
            int i;  
            for(i = 0; ++i < length; i++) {  
                printWriter.println(input[i]);  
            }   
            printWriter.close();    
        }  
        catch(IOException e) {  
            System.out.println(e.getMessage());  
        }  
    }  
}

The problem is I want to write an array to a file with each entry being on a separate line and I want to wipe the file at the start from each write. Any advice on what to do? Because all I've managed to do is write the last entry of the array while wiping all the previous entries and yet I can write the last entry several times below itself.

Thanks ^^

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

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

发布评论

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

评论(4

铜锣湾横着走 2024-10-21 04:20:42
for(i = 0; ++i < length; i++) {

我认为你不想将 i 增加两次。摆脱 ++i。

for(i = 0; ++i < length; i++) {

I don't think you want to increase i twice. Get rid of the ++i.

执笔绘流年 2024-10-21 04:20:42

我强烈建议使用库 http://commons .apache.org/io/api-release/org/apache/commons/io/FileUtils.html 已经解决了其中许多问题。如果将数组转换为列表,您可以使用:

public static void writeLines(File file,
                              Collection<?> lines)
                       throws IOException

写入每个的 toString() 值
集合中的项目到指定的
逐行归档。默认虚拟机
编码和默认行结尾
将被使用。

该库的作者想到了大多数人没有想到的许多问题,例如行结束、编码、错误捕获等。它对于一般用途也可能尽可能高效。

I strongly recommend using the library http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html which has already tackled many of these problems. If you transform your array to a List you can use:

public static void writeLines(File file,
                              Collection<?> lines)
                       throws IOException

Writes the toString() value of each
item in a collection to the specified
File line by line. The default VM
encoding and the default line ending
will be used.

The authors of the library have thought of many problems that most people don't such as line-endings, encodings, error trapping, etc. It's also likely to be as efficient as possible for general use.

溺ぐ爱和你が 2024-10-21 04:20:42

for(i = 0; ++i < length; i++) { 看起来不错,应该是 for(i = 0; i < length; i++) { > ?

或者,甚至更好: for (String line : input) { }

另外,new FileOutputStream(fileName, true) 不会清除文件,而是追加到文件中。

for(i = 0; ++i < length; i++) { does look right, should this be for(i = 0; i < length; i++) { ?

Or, even better: for (String line : input) { }

Also, new FileOutputStream(fileName, true) will not clear the file, rather append to it.

寂寞美少年 2024-10-21 04:20:42
import java.io.*;

public class writingArray {
    public static void writeToFile(String fileName, String[] input) {
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(new FileOutputStream(fileName));
            for (int i = 0; i < input.length; i++) {
                printWriter.println(input[i]);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
        }
    }

}
import java.io.*;

public class writingArray {
    public static void writeToFile(String fileName, String[] input) {
        PrintWriter printWriter = null;
        try {
            printWriter = new PrintWriter(new FileOutputStream(fileName));
            for (int i = 0; i < input.length; i++) {
                printWriter.println(input[i]);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } finally {
            if (printWriter != null) {
                printWriter.close();
            }
        }
    }

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