Chinaunix首页 | 论坛 | 博客
  • 博客访问: 168998
  • 博文数量: 13
  • 博客积分: 2530
  • 博客等级: 少校
  • 技术积分: 485
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-15 18:31
文章分类

全部博文(13)

文章存档

2008年(13)

我的朋友

分类: Java

2008-05-20 21:14:38

 

package forecast2008;

import java.io.File;
import java.io.FileOutputStream;

public class MessageFile {
    private String fileName ;
    
    MessageFile(String fileName){
        this.fileName = fileName ;
    }
    
    protected synchronized void write(String content)throws Exception {
        File file = new File("d:/"+fileName) ;
        System.out.println(file);
        if(file.exists())
            throw new Exception("要创建的文件已经存在,请检查目录或文件名") ;
        //创建报文

        if(!file.createNewFile())
            throw new Exception("创建报文失败,可能文件已经存在,请检查路径") ;
        FileOutputStream out = new FileOutputStream(file,true) ;
        StringBuffer sb = new StringBuffer() ;
        sb.append(content) ;
        out.write(sb.toString().getBytes("US-ASCII")) ;
        out.flush() ;
        out.close() ;
    }
    public static void main(String[] args){
        String content="adfjoiwenf;lvjiow;ejf;dsjfoiejwf你好" ;
        MessageFile mf = new MessageFile("a.txt") ;
        try {
            mf.write(content) ;
        } catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
    }
}

阅读(2037) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~