JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java IDE » Eclipse  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Eclipse txt檔案讀寫檔案,個數發生問題(已解決)
twice7713





發文: 2
積分: 0
於 2017-04-05 08:25 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
將txt檔內的資料(共四萬筆)讀進二維陣列當中,並利用手動輸入需要取出幾個(亂數),之後利用二維辨識是否有取用過,有取用過則使用while random到沒使用過為止.

跑下去之後照理念來說輸入2000筆,因該後面紅字測試比數也應是2000筆,但其結果卻為15XX,請問是何原因

以下為我的程式碼

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileWriter;
import java.util.Random;
import java.util.Scanner;
public class FOLD_T {

  public static void main(String[] args)throws IOException {
    // TODO Auto-generated method stub
    FileReader fr = new FileReader("C:\\Users\\twice\\Desktop\\重要資料備份\\馬老師計畫\\AllTables\\MA_CWC\\Value_6.txt");
    BufferedReader br = new BufferedReader(fr);
    String line;
    int a=0;
    while ((line = br.readLine())!=null) {
      a++;
    }
    System.out.println("**"+a);
    String[][] BOX = new String[a][2];
    fr = new FileReader("C:\\Users\\twice\\Desktop\\重要資料備份\\馬老師計畫\\AllTables\\MA_CWC\\Value_6.txt");
    br = new BufferedReader(fr);
    int number=0;
    line="";
    while ((line = br.readLine())!=null) {
      BOX[number][0]=line;
      BOX[number][1]="0";
      number++;
    }
    FileWriter Index_X = new FileWriter("C:\\Users\\twice\\Desktop\\重要資料備份\\馬老師計畫\\AllTables\\MA_CWC\\Index_X.txt");
    Random rnd= new Random();
    int Y;
    
    Scanner UserIn= new Scanner(System.in);
    int S=UserIn.nextInt();
    for(int i=0;i<S;i++){
      Y=rnd.nextInt( a );
      while(BOX[Y][1]!="0"){
        Y=rnd.nextInt( a );
      }
      Index_X.write(BOX[Y][0]+"\r\n");
      BOX[Y][1]="1";
    }

    fr = new FileReader("C:\\Users\\twice\\Desktop\\重要資料備份\\馬老師計畫\\AllTables\\MA_CWC\\Index_X.txt");
    br = new BufferedReader(fr);
    int test=0;
  
    while ((line = br.readLine())!=null) {
      test++;
    }
    
    System.out.println(test);

    
  }

}


twice7713 edited on 2017-04-07 02:09
reply to postreply to post
作者 Re:Eclipse txt檔案讀寫檔案,個數發生問題 [Re:twice7713]
weber





發文: 195
積分: 0
於 2017-04-06 00:49 user profilesend a private message to usersend email to weberreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
原因是您的 FileWriter 最後並沒有進行關閉的動作, 所以最後的部份資料沒有被寫入,
您只要在寫入的動作全部結束之後, 加一行指令即可:
1
Index_X.close();


weber edited on 2017-04-06 01:26
reply to postreply to post
作者 Re:Eclipse txt檔案讀寫檔案,個數發生問題 [Re:weber]
twice7713





發文: 2
積分: 0
於 2017-04-07 02:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝您,幫上了大忙,我是程式的新手.
沒想到少加一行結束,會影響輸出.


reply to postreply to post
作者 Re:Eclipse txt檔案讀寫檔案,個數發生問題 [Re:twice7713]
weber





發文: 195
積分: 0
於 2017-04-07 02:22 user profilesend a private message to usersend email to weberreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不客氣。

reply to postreply to post
» JWorld@TW »  Java IDE » Eclipse

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8