这是我们的文件-
FileWriter writer = new FileWriter("E:/demo.txt");
现在,考虑一个字符串数组-
String arr[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" };
将上面的数组写入文件“ demo.txt”-
int len = arr.length; for (int i = 0; i < len; i++) { writer.write(arr[i] + "-"); }
以下是一个示例。在这里,我们的文件是“ E:/demo.txt”-
import java.io.FileWriter; public class Demo { public static void main(String[] argv) throws Exception { FileWriter writer = new FileWriter("E:/demo.txt"); String arr[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" }; int len = arr.length; for (int i = 0; i < len; i++) { writer.write(arr[i] + "-"); } writer.close(); } }
输出如下,即文件中的文本为-
输出结果
ONE-TWO-THREE-FOUR-FIVE-SIX-SEVEN-EIGHT-NINE-