• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

老王的蜕变

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

java io 从文件的读取和输入

从文件中读取内容

package j2se.io.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class ReadFromFile
{
    /**
     *BufferedReader 由Reader类扩展而来,提供通用的缓冲方式文本读取,而且提供了很实用的readLine,读取一个文本行,
     *从字符输入流中读取文本,缓冲各个字符,从而提供字符、数组和行的高效读取。
     */
    public static void main(String args[]) throws Exception
    {
        File f = new File("d:" + File.separator + "xml2.log"); // 指定操作文件
        FileInputStream input= new FileInputStream(f);
        BufferedReader br = new BufferedReader(new InputStreamReader(input));
        String data = null;
        StringBuffer str = new StringBuffer();
        while((data = br.readLine())!=null){
             str.append(data).append('\n'); // 取数据
        }
        System.out.println(str);

          input.close();
          br.close();

    }
}

 

 

将程序内容写入文件中

package j2se.io.util;

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

public class WriteToFile
{
    public static void main(String arg[]) throws Exception
    {
        PrintStream ps = null; // 声明打印流对象
        // 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
        ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"), true));
        ps.print("\r\r** ");
        ps.print("\r\r**dasdasdad     啊倒萨大v阿达v双savs倒萨按时  ");
        ps.println("1 + 1 = " + 2);
        ps.close();
    }
}

 

posted on 2013-06-06 15:14  老王的蜕变  阅读(287)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3