建议楼主使用FileOutputStream来写入文件就能解决这个问题 import java.io.*; public class WriteTest {
/** Creates a new instance of Write1 */ public static void main(String arg[]) { try { System.out.print("Input:"); int count,n=512; byte buffer[]=new byte[n]; count=System.in.read(buffer); //从键盘读取一行字符,存储到缓冲区 FileOutputStream wf=new FileOutputStream("test.txt"); wf.write(buffer,0,count); //写入输出流 wf.close(); System.out.println("Save to test.txt"); } catch(IOException ioe) { System.out.println("File Write Error!"); } }
}write public void write(byte[] b, int off, int len) throws IOException将指定字节数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 覆盖: 类 OutputStream 中的 write 参数: b - 数据。 off - 数据中的起始偏移量。 len - 要写入的字节数。 抛出: IOException - 如果发生 I/O 错误。
在给出 File 对象的情况下构造一个 FileWriter 对象。再封装一层.. 用这个 FileWriter 后面的 boolean 设置成true就好了..就可以追加输出了.
import java.io.*;
public class WriteTest {
/** Creates a new instance of Write1 */
public static void main(String arg[])
{
try
{
System.out.print("Input:");
int count,n=512;
byte buffer[]=new byte[n];
count=System.in.read(buffer); //从键盘读取一行字符,存储到缓冲区
FileOutputStream wf=new FileOutputStream("test.txt");
wf.write(buffer,0,count); //写入输出流
wf.close();
System.out.println("Save to test.txt");
}
catch(IOException ioe)
{
System.out.println("File Write Error!");
}
}
}write
public void write(byte[] b,
int off,
int len)
throws IOException将指定字节数组中从偏移量 off 开始的 len 个字节写入此文件输出流。 覆盖:
类 OutputStream 中的 write
参数:
b - 数据。
off - 数据中的起始偏移量。
len - 要写入的字节数。
抛出:
IOException - 如果发生 I/O 错误。
追加对象就往这个集合结构类对象中,
用objectoutputstream输出这个结合结构类对象。
这个就是正解.FileWriter
public FileWriter(String fileName,
boolean append)
throws IOException在给出文件名的情况下构造 FileWriter 对象,它具有指示是否挂起写入数据的 boolean 值。 参数:
fileName - 一个字符串,表示与系统有关的文件名。
append - 一个 boolean 值,如果为 true,则将数据写入文件末尾处,而不是写入文件开始处。
抛出:
IOException - 如果指定文件存在,但它是一个目录,而不是一个常规文件;或者该文件不存在,但无法创建它;抑或因为其他某些原因而无法打开它