Java中利用I/O流向文件中写入数据的方式的总结
//以单字节写文件
public static void writeFileByBytes(File file) throws Exception{
OutputStream ous = new FileOutputStream(file,true); //true:以追加的方式写入文件
Scanner sc = new Scanner(System.in);
System.out.println("请输入要写入的内容:");
byte[] bs = sc.nextLine().getBytes();
ous.write(bs);
ous.write('\n');
ous.flush();
ous.close();
System.out.println("写文件 "+file.getAbsolutePath()+" 写入成功");
}
//以字符写文件
public static void writeByChar(File file) throws Exception{
FileWriter fw = new FileWriter(file,true); //true:以追加的防暑写入文件
BufferedWriter bw = new BufferedWriter(fw);
Scanner sc = new Scanner(System.in);
String nextLine = sc.nextLine();
bw.write(nextLine);
bw.newLine();
bw.flush();
bw.close();
fw.close();
System.out.println("写文件 "+file.getAbsolutePath()+" 写入成功");
}
public s