风故故,也依依

Stand still in the wind.

导航

用FileOutputStream将内容写入到文本

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("文本已经成功写入文件!");
   }
}

posted on 2007-05-28 00:27  jadmin  阅读(644)  评论(0编辑  收藏  举报