您可以使用Writer类将数据写入文件。在下面给出的示例中,我们使用BufferedWriter编写数组的内容。
import java.io.BufferedWriter; import java.io.FileWriter; public class WritingStringArrayToFile { public static void main(String args[]) throws Exception { String[] myArray = {"JavaFX", "HBase", "OpenCV", "Java", "Hadoop", "Neo4j"}; BufferedWriter writer = new BufferedWriter(new FileWriter("myFile.txt", false)); for(int i = 0; i < myArray.length; i++) { writer.write(myArray[i].toString()); writer.newLine(); } writer.flush(); System.out.println("Data Entered in to the file successfully"); } }
输出结果
Data Entered in to the file successfully
如果您验证文件,则可以观察到该文件具有数组的内容-
import java.io.BufferedWriter; import java.io.FileWriter; public class WritingIntArrayToFile { public static void main(String args[]) throws Exception { Integer[] myArray = {23, 93, 56, 92, 39}; BufferedWriter writer = new BufferedWriter(new FileWriter("myFile.txt", false)); for(int i = 0; i < myArray.length; i++) { writer.write(myArray[i].toString()); writer.newLine(); } writer.flush(); System.out.println("Data Entered in to the file successfully"); } }
输出结果
Data Entered in to the file successfully
如果您验证文件,则可以观察到该文件具有数组的内容-