public void appendToFile(String str, String filename) throws Exception
{
// Open up an outputstream writer to the file, and append str to it.
FileOutputStream stream;//provides file access
OutputStreamWriter writer;//writes to the file
try
{
stream = new FileOutputStream(filename, true);
writer = new OutputStreamWriter(stream);
writer.write(str);
writer.close();
stream.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//appendToFile
public void writeFile(String str, String filename) throws Exception
{
// Open a writer to the file, then write the string.
BufferedWriter bwriter;//writer to the file
String fullfilepath;//path for the output file
try
{
bwriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename)));
bwriter.write(str);
bwriter.flush();
bwriter.close();
}//try
catch(Exception e)
{
throw e;
}//catch
}//writeFile
把string里的东西生成一个文件:
FileOutputStream out = null;
out = new FileOutputStream("C:\a.txt");
DataOutputStream textout = null;
textout = new DataOutputStream(out);
String val = "要写入的字符串";
textout.writeBytes(val);
textout.close();
out.close();