码迷,mamicode.com
首页 > 编程语言 > 详细

java读写 功能

时间:2017-07-17 15:15:27      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:style   char   cat   pack   exce   finally   span   使用   except   

/*
字符流:
Reader Writer。

需求:
在硬盘上创建一个文件并写入一些数据。
*/

import java.io.*;

/*
//创建FileWriter对象,调用window资源,在指定位置创建好了数据存放的目的地。
        //如果指定目录下,已存在要创建的文件,那么会被覆盖。

        FileWriter fw = new FileWriter("demo.txt");

        //通过流对象write方法将字符串写入到流中。
        fw.write("abcde");

        //关闭资源,清空它先。
        fw.close();



需求:对原有文件进行数据的续写。


1,建立对象。
2,使用读写方法。
3,关闭资源。

*/


class  FileWriterDemo
{
    public static void main(String[] args) //throws IOException
    {
        FileWriter fw = null;
        try
        {
            fw = new FileWriter("k:\\demo.txt",true);

            fw.write("qqqqq\r\nmmmm");

        }
        catch (IOException e)
        {
            System.out.println(e.toString());
        }
        finally
        {
            if(fw!=null)
                try
                {
                    fw.close();
                }
                catch (IOException e)
                {
                    System.out.println("close:"+e.toString());
                }
               
        }

    }
}
 
 
package model;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReadDemo {
	public static void main(String[] args){
		try {
			FileReader fr = new FileReader("E:\\demo.txt");
			char[] arr = new char[1024];
			int num = 0;
			 try {
				while((num = fr.read(arr))!=-1)
				    {
				        System.out.println(new String(arr,0,num));
				    }
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

  

java读写 功能

标签:style   char   cat   pack   exce   finally   span   使用   except   

原文地址:http://www.cnblogs.com/xsdf/p/7194106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!