简体   繁体   English

如何在Java中将数组的内容输出到文本文件中?

[英]How to output the contents of an array into a text file in Java?

I want to know how to output the integers of an array into a text file separated by different lines. 我想知道如何将数组的整数输出到由不同行分隔的文本文件中。 This is the pertinent code below, but every time I run the program, the file is created but no data is saved into the file itself. 这是下面的相关代码,但是每次我运行该程序时,都会创建该文件,但不会将任何数据保存到文件本身中。

public static void printToFile(double[] gravity)throws IOException
{
    PrintWriter outputFile = new PrintWriter (new FileWriter("gravitydata.txt", true));
    for(int a = 0; a < 9; a++)
    {
         outputFile.println(gravity[a]);
    }
}

You have to close() the file (closing the PrintWriter will close the FileWriter which will close the file). 您必须close()文件(关闭PrintWriter将关闭FileWriter ,这将关闭文件)。 You could use a try-with-resources to do it for you 您可以使用try-with-resources为您做到这一点

public static void printToFile(double[] gravity) throws IOException
{
    try (PrintWriter outputFile = new PrintWriter(
            new FileWriter("gravitydata.txt", true))) {
        for(int a = 0; a < gravity.length; a++){
            outputFile.println(gravity[a]);
        }
    }
}

or the older finally block and something like 或较旧的finally块之类的东西

public static void printToFile(double[] gravity) throws IOException
{
    PrintWriter outputFile = new PrintWriter(
            new FileWriter("gravitydata.txt", true))
    try {
        for(int a = 0; a < gravity.length; a++){
            outputFile.println(gravity[a]);
        }
    } finally {
        outputFile.close();
    }
}

in either case you should use the array length property instead of hard-coding 9 . 无论哪种情况,都应该使用array length属性而不是硬编码9

{
    PrintWriter outputFile = new PrintWriter (new FileWriter("gravitydata.txt", true));
    for(int a = 0; a < 9; a++)
    {
        outputFile.println(gravity[a]);
    }
    outputFile.close();

}

The following approach can be good if you wish to utilize the built-in functions from Java 8 (to avoid writing your own file handling and looping). 如果您希望利用Java 8的内置函数(以避免编写自己的文件处理和循环),则以下方法可能很好。

public static void printToFile(double[] gravity) throws IOException {
    // First, convert the double[] to a list of Strings
    final List<String> doublesAsStrings = Arrays.stream(gravity)
            .boxed() // Box it to Doubles
            .map(g -> String.valueOf(g)) // Convert each Double to a String
            .collect(Collectors.toList()); // Create a List<String>

    // Then, write the list to the file
    Files.write(new File("gravitydata.txt").toPath(), doublesAsStrings);
}

There are a few differences compared to the previous answers: 与以前的答案相比,有一些区别:

  • The Files class is used to write the data which is convenient since it handles try/catch etc Files类用于写入数据,这很方便,因为它可以处理try / catch等
  • No external looping is required since the streaming API:s are used to create a sequence of strings that will be printed 不需要外部循环,因为流API:用于创建将要打印的字符串序列
  • It is short and compact ;) 它短而紧凑;)

暂无
暂无

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

相关问题 如何在java中的输出文本文件中的每一行中打印stringbuilder的内容 - How to print the contents of stringbuilder in each line in a output text file in java 如何在Java中将数组的字符串输出到文本文件 - how to output strings of an array to a text file in java (Java)将文本文件中的内容存储到数组中时,打印数组的输出均为null - (Java)When store contents from text file into an array, output of printing array was all null 如何将数组内容写入输出文件? - How To Write Array Contents To Output File? 如何在文本文件中写入数组的内容? - How to write the contents of my array in a text file? (Java)如何读取可以使用各种编码的文本文件并将内容输出到看起来正常的文本文件中? - (Java) How can I read in a text file that could use various encodings and output the contents in a text file that looks normal? 如何在Java中将文件内容放入数组的字符串 - How to put the contents of a file to a String of array in java Java:如何将文本文件的内容重定向到Java文件作为键盘输入 - Java: How to redirect the contents of a text file to java file as keyboard input Java问题,将文本文件的内容放入数组,然后显示和处理数组的内容 - Java Issue with taking contents of a text file into an array and then displaying and manipulating the contents of the array 获取Java中文本文件的内容? - Get contents of text file in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM