1
//用FileOutputStream将内容写入文本文件
import java.io.*;
public class FileOutputDemo
{
public static void main(String[] args)
{
FileOutputStream out;
PrintStream p;
try
{
out = new FileOutputStream("myfile.txt");
p = new PrintStream(out);
p.println("Hello,World!");
p.close();
}
catch(Exception e)
{
System.err.println("Error writing to file!");
}
}
}
2
import java.io.*;
class FSODemo
{
public static void main(String args[]) throws Exception
{
String source="Beijing is the capital of China\n"
+"and it is a very beautiful city.\n"
+"Welcome to Beijing!";
byte buf[] = source.getBytes();
FileOutputStream f=new FileOutputStream("file.txt");
f.write(buf);
f.close();
System.out.println("文本已经成功写入文件!");
}
}