• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
superStar1213
博客园    首页    新随笔    联系   管理    订阅  订阅
FileWriter向文件中写入内容

1.创建fileWriter对象

2.写入文件信息

3.关闭流

必须关闭流,否则将不能写入文件中

/**
 * 
 * @author Administrator
 *    文件写入对象的使用
 *
 */
public class fileWriterTest {
    
    public static void main(String[] args) {
         File file1 = new File("d:\\test.txt");
            FileWriter fw;
            try {
                fw = new FileWriter(file1);
                 fw.write(97);
                    fw.write("this is 我们");
                    fw.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
           
        
        try {
        //1.创建文件
        File desFile = new File("aa\\bb");
        
        File sourceFile = new File(desFile,"aa.txt");
        
        //2.创建文件写入对象
        FileWriter writer = new FileWriter(sourceFile);
        
        //3.写入整数
        writer.write(1);
    
        writer.close();
        
         FileReader fr = new FileReader(sourceFile);
        System.out.println( fr.read());
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

 

posted on 2017-12-11 16:45  superStar1213  阅读(491)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3