,向 其中添加新数据,使用字节流将随机生成的50 个整数写入文件中,每个数用“-”隔开。
然后再把它们读出来。
package com.zy.daozhi;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Demo2 {
public static void main(String[] args) {
try {
File f=new File("D:/yi.txt");
FileWriter fw =new FileWriter(f);;
Random rom = new Random();
if(f.exists()){
System.out.println("文件已存在。。。");
}else{
f.createNewFile();
}
Random random = new Random();
for(int i = 0; i < 50;i++) {
System.out.println(Math.abs(random.nextInt())%50);
fw.write(Math.abs(random.nextInt())%50+"-");
}
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}