参考代码和注释如下
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) {
String fp = "InputFile1.dat";//文件名
File f = new File(fp);//文件对象
if(!f.exists()){//如果文件不存在
try {
f.createNewFile();//创建文件
System.out.println("文件不存在。创建文件");
} catch (IOException e) {
e.printStackTrace();
}
}
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));//参数true表示在文件末尾添加数据
StringBuffer info = new StringBuffer();//用于拼接字符串
for (char i = 'a'; i < 'z'; i++) { //注意变量时char 类型
info.append(i+"\t");//用制表符分开
}
info.append('z');//最后一个字符z 就不用制表符了
bw.write(info.toString());
bw.close();
System.out.println("数据传送完成");
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试
床前明月光,疑是地上霜。
举头望明月,低头思故乡a b c d e f g h i j k l m n o p q r s t u v w x y z
本回答被网友采纳