2015-07-13 26 views
3

我已經創建了一個簡單的程序,它將輸入一個標題和一個註釋,然後您可以選擇使用BufferedWriter將註釋導出到txt文件,但是因爲每個註釋都是存儲在一個ArrayList當他們存儲他們迭代通過一個增強循環它不斷重複每個音符,因爲我遍歷所有的對象。ArrayList和BufferedReader注意程序

注類

import java.util.*; 
public class Notes 
{ 
    private String notes; 
    private String titleOfNotes; 
    Scanner input = new Scanner(System.in); 

    public Notes() 
    { 
     titleOfNote(input); 
     takeNotes(input); 
    } 

    public void takeNotes(Scanner x) 
    { 
     System.out.println("Please Enter Your Note"); 
     notes = x.nextLine(); 
    } 

    public void titleOfNote(Scanner y) 
    { 
     System.out.println("Please Enter Title"); 
     titleOfNotes = y.nextLine(); 
    } 
    public String toString() 
    { 
     return "Title: " + titleOfNotes + "\t" + notes; 
    } 

} 

App類//不mostof工作

import java.util.*; 
import java.io.*; 
public class App 
{ 
    private int exit = 0; 
    private int createANote; 
    private int displayTheNotes; 
    private int inputFromUser; 
    public boolean haveFileBeenWritten = true; 

    File file = new File("Notes.txt"); 

    Scanner input = new Scanner(System.in); 

    ArrayList<Notes> arrayOfNotes = new ArrayList<Notes>(); 

    public void makeNoteObject() 
    { 
     arrayOfNotes.add(new Notes()); 
    } 

    public void displayAllTheNote(ArrayList<Notes> n) 
    { 
      for(Notes singleObjectOfNote : n) 
      { 
       System.out.println(singleObjectOfNote); 
      } 
    } 

    public void programUI(){ 


     while(exit != 1) 
     { 
      System.out.println("1. Create A Note"); 
      System.out.println("2. Display The Notes"); 
      System.out.println("3. Exit"); 
      System.out.println("4. Export to text file"); 
      System.out.println("Enter Your Operation"); 
      inputFromUser = input.nextInt(); 

      if(inputFromUser == 1) 
      { 
       makeNoteObject(); 
      } 
      else if(inputFromUser == 2) 
      { 
       displayAllTheNote(arrayOfNotes); 
      } 
      else if(inputFromUser == 3) 
      { 
       System.out.println("Exited"); 
       exit = 1; 
      } 
      else if(inputFromUser == 4) 
      { 
       makeATxtFileFromNotes(arrayOfNotes); 
       System.out.println("Textfile created filename: " + file.toString()); 
      } 
      else 
      { 
       System.out.println("You Select A Invalid Command"); 
      } 
     } 
    } 

    public void makeATxtFileFromNotes(ArrayList<Notes> x) 
    { 

     try (BufferedWriter bw = new BufferedWriter(new FileWriter(file,haveFileBeenWritten))) 
     { 
//Problem here! 
      for(Notes singleObjectOfNotes : x) 
      { 
       bw.write(singleObjectOfNotes.toString()); 
       bw.newLine(); 
      } 


     }catch(IOException e) 
     { 
      System.out.println("Cant Write File: " + file.toString()); 
      haveFileBeenWritten = false; 
     } 
    } 

    public App() 
    { 
     programUI(); 
    } 
    public static void main(String[]args) 
    { 
     App objectOfApp = new App(); 

    } 
} 

我是新來的Java,所以我的代碼我不是最好的!

回答

1

如果你的問題是,你只需要看到當前列表中的Notes不包括之前的,這是因爲這行:

try (BufferedWriter bw = new BufferedWriter(new FileWriter(file,haveFileBeenWritten))) 

默認情況下,haveFileBeenWrittentrue因此基於該FileWriter API它將追加對現有文件Notes.txt所以如果你不想那樣,請將其更改爲false

參數:

文件 - 一個文件對象寫入

追加 - 如果爲true,則將字節 寫入到文件的末尾,而不是一開始

編輯:要訪問List<>元素,請使用get()

例子:

int size = myList.size(); 
for (int i = 0 ; i < size ; i++) { 
    //... 
    Notes note = myList.get(i); 
    //... 
} 
+0

微服私訪我希望能看到現有的筆記和不想覆蓋它們不過是在數組列表中的任何方法,可以讓你在一個正常的數組訪問一定陣列狀。 – dijam

+0

請參閱編輯@dijam。 – Incognito

+1

這解決了嗎?我好奇。 – Incognito