java讀寫 功能

來源:互聯網
上載者:User

標籤: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 blocke.printStackTrace();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

  

java讀寫 功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.