2019年8月25日 星期日

[Java程式]_Ch13.1_文字檔的讀取與寫入



歡迎訂閱頻道:杜仲教學YouTube
https://www.youtube.com/channel/UCElwt6s8Ea_V-gnu9sZZT5g?sub_confirmation=1
杜仲教學部落格:
https://derekcctu.blogspot.com/

1 則留言:

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

    public class A13 {

    public static void main(String[] args) throws IOException {
    String path=new String("C:\\CCTU\\Gjun8\\A20190826.txt");
    FileReader fr;
    try {
    fr=new FileReader(path);
    } catch (FileNotFoundException e) {
    fr=new FileReader("C:\\CCTU\\Gjun8\\A20190823.txt");
    System.out.println("1.[例外發生],改讀23.txt,e="+e);
    }
    char data[]=new char[512];
    int num=fr.read(data);
    for(int i=0;i<data.length;i++){
    System.out.print(data[i]);
    }
    System.out.println();
    String s=new String(data,0,num);
    System.out.println("s字串長度:"+s.length()+"\n"+s);
    FileWriter fw=new FileWriter("C:\\CCTU\\Gjun8\\B20190823.txt",true);
    fw.write(data);
    fw.write(s);
    fr.close();
    fw.close();
    System.out.println("讀取完畢,共讀取"+num+"字元,內容:"+data);
    }
    }

    回覆刪除

Ether.fi Cash Card 一張所有人必備的加密貨幣信用卡   [Ether.fi Cash Card] : https://www.ether.fi/app/cash/sign-up?ref_code=d27f8523 ------國際交易所------ [Bitg...