第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

第一次運(yùn)行后沒(méi)有給EOF添加“東西”......怎么樣?

第一次運(yùn)行后沒(méi)有給EOF添加“東西”......怎么樣?

牛魔王的故事 2019-04-23 18:15:48
第二次將“something”這個(gè)詞添加到文件中并打印“atext has added”但它將其視為空文件....什么..public class ReadFile {     public ReadFile() {     }     public boolean readFile() {         boolean success = false;         String file_name = "source/text.txt";         try {             FileReader file = new FileReader(file_name);             BufferedReader read = new BufferedReader(file);              BufferedWriter bf = new BufferedWriter(new FileWriter (file_name));             if (read.readLine() != null) {                 bf.append("something");                 System.out.println("a text has been added!!");             } else {                 bf.append(file_name);                 System.out.println("File updated");             }             read.close();             bf.close();             success = true;         } catch(IOException e) {             e.getMessage();         }         return success;     }     public static void main(String[] args) {         // TODO Auto-generated method stub         ReadFile rf = new ReadFile();         rf.readFile();     }}當(dāng)文件不為空時(shí),應(yīng)該在EOF中添加“something”
查看完整描述

3 回答

?
鴻蒙傳說(shuō)

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

您顯然是在寫(xiě)入您嘗試閱讀的同一文件。編寫(xiě)沒(méi)有附加選項(xiàng)的文件會(huì)將文件的內(nèi)容截?cái)酁榱汩L(zhǎng)度。嘗試寫(xiě)入其他文件。


查看完整回答
反對(duì) 回復(fù) 2019-05-15
?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

您需要以附加模式打開(kāi)文件,否則當(dāng)FileWriter打開(kāi)文件時(shí)它將被清空:

BufferedWriter bf = new 
                BufferedWriter(new FileWriter (file_name, true));


查看完整回答
反對(duì) 回復(fù) 2019-05-15
?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

這里需要進(jìn)行一些更改

  • 您需要以追加模式打開(kāi)文件進(jìn)行寫(xiě)入

  • 在將數(shù)據(jù)附加到文件以便讀取之后,需要調(diào)用flush()

  • 你需要一個(gè)循環(huán),至少如果你想在同一個(gè)運(yùn)行中發(fā)生一切

請(qǐng)注意,我在這里使用了一個(gè)無(wú)限循環(huán),這是危險(xiǎn)的,因此有改進(jìn)的余地。捕獲異常時(shí)我還添加了println


try {
    FileReader file = new FileReader(file_name);
    BufferedReader read = new BufferedReader(file); 
    BufferedWriter bf = new BufferedWriter(new FileWriter (file_name, true));

    while(true) {
        if(read.readLine() != null) {
            bf.append("something");
            System.out.println("a text has been added!!");
            break;
        } else {
            bf.append(file_name);
            bf.flush();            
            System.out.println("File updated");
        }
     }
     read.close();
     bf.close();
     success = true;} catch(IOException e) {
    System.out.println(e.getMessage());}


查看完整回答
反對(duì) 回復(fù) 2019-05-15
  • 3 回答
  • 0 關(guān)注
  • 421 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)