Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 将数组内容写入文本文件_Java_Arrays - Fatal编程技术网

Java 将数组内容写入文本文件

Java 将数组内容写入文本文件,java,arrays,Java,Arrays,我目前有一个数组,它保存从GUI发出的一组命令。我可以将命令列表打印到屏幕上,但在将这些命令写入文本文件时遇到问题。我需要一些建议。这是打印到控制台的代码 for (int i = 0; i < movementArray.length; i++) { System.out.println(movementArray[i]); } for(int i=0;i

我目前有一个数组,它保存从GUI发出的一组命令。我可以将命令列表打印到屏幕上,但在将这些命令写入文本文件时遇到问题。我需要一些建议。这是打印到控制台的代码

for (int i = 0; i < movementArray.length; i++)
{
    System.out.println(movementArray[i]);
}
for(int i=0;i
使用
PrintWriter
BufferedWriter


首先使用StringBuilder创建字符串:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < movementArray.length; i++)
{
    sb.append(movementArray[i]);
}
setContents(new File("your path here"), sb.toString());

嗯,请再努力一点。在Google或此处学习java IO topicsSearch:“Write to file java”。尝试在Google上搜索“java Write array to file”,几十个很好的例子,其中一些例子本身就是如此。嗯,所有其他例子都谈到从文件读取并在数组中写入,这与从Arraylist读取并在文本文件中写入非常不同。
public static void setContents(File aFile, String aContents)
            throws FileNotFoundException, IOException {
        if (aFile == null) {
            throw new IllegalArgumentException("File should not be null.");
        }
        if (!aFile.exists()) {
            throw new FileNotFoundException("File does not exist: " + aFile);
        }
        if (!aFile.isFile()) {
            throw new IllegalArgumentException("Should not be a directory: " + aFile);
        }
        if (!aFile.canWrite()) {
            throw new IllegalArgumentException("File cannot be written: " + aFile);
        }

        //declared here only to make visible to finally clause; generic reference
        Writer output = null;
        try {
            //use buffering
            //FileWriter always assumes default encoding is OK!
            output = new BufferedWriter(new FileWriter(aFile));
            output.write(aContents);
        } finally {
            //flush and close both "output" and its underlying FileWriter
            if (output != null) {
                output.close();
            }
        }
    }