Java ArrayList和BufferedReader笔记程序

Java ArrayList和BufferedReader笔记程序,java,file,arraylist,filewriter,bufferedwriter,Java,File,Arraylist,Filewriter,Bufferedwriter,我创建了一个简单的程序,其中包含一个标题和一个注释,然后您可以选择使用BufferedWriter将注释导出到txt文件。但是,由于每个注释都是一个对象,存储在ArrayList中,因此在存储它们时,我通过一个for enhanced循环进行迭代,它会不断复制每个注释遍历所有对象 注释类 import java.util.*; public class Notes { private String notes; private String titleOfNotes;

我创建了一个简单的程序,其中包含一个标题和一个注释,然后您可以选择使用
BufferedWriter
将注释导出到txt文件。但是,由于每个注释都是一个对象,存储在
ArrayList
中,因此在存储它们时,我通过一个for enhanced循环进行迭代,它会不断复制每个注释遍历所有对象

注释类

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; 
    }

}
应用程序类//完成大部分工作

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();

    }
}
import java.util.*;
导入java.io.*;
公共类应用程序
{
私有int出口=0;
私人国际创意酒店;
私人int显示注释;
私有int inputFromUser;
公共布尔值haveFileBeenWrite=true;
File File=新文件(“Notes.txt”);
扫描仪输入=新扫描仪(System.in);
ArrayList arrayOfNotes=新的ArrayList();
public void makeNoteObject()
{
添加(新注释());
}
公共无效显示所有注释(ArrayList n)
{
用于(注:n)
{
System.out.println(singleObjectOfNote);
}
}
公共程序UI(){
while(退出!=1)
{   
System.out.println(“1.创建注释”);
System.out.println(“2.显示注释”);
System.out.println(“3.Exit”);
System.out.println(“4.导出到文本文件”);
System.out.println(“输入您的操作”);
inputFromUser=input.nextInt();
如果(inputFromUser==1)
{
makeNoteObject();
}
else if(inputFromUser==2)
{
显示所有注释(阵列注释);
}
else if(inputFromUser==3)
{   
System.out.println(“退出”);
退出=1;
}
else if(inputFromUser==4)
{
从Notes(arrayOfNotes)生成X文件;
System.out.println(“Textfile创建的文件名:“+file.toString());
}
其他的
{
System.out.println(“您选择的命令无效”);
}
}
}
public void makeATxtFileFromNotes(ArrayList x)
{
try(BufferedWriter bw=new BufferedWriter(new FileWriter(file,havefilebeenwrited)))
{
//这里有问题!
用于(注释:x)
{
write(singleObjectOfNotes.toString());
换行符();
}
}捕获(IOE异常)
{
System.out.println(“无法写入文件:+File.toString());
haveFileBeenWrite=false;
}
}
公共应用程序()
{
programUI();
}
公共静态void main(字符串[]args)
{
App objectOfApp=new App();
}
}

我是Java新手,所以我的代码不是最好的

如果您的问题是只需要查看当前列表的
注释
而不包括上一行,则是因为这一行:

try (BufferedWriter bw = new BufferedWriter(new FileWriter(file,haveFileBeenWritten)))
默认情况下,
havefilebeenwrited
true
,因此基于此,它将在现有文件
Notes.txt
上追加,因此如果您不想这样做,请将其更改为
false

参数:

文件-要写入的文件对象

追加-如果为true,则字节将为 写入文件的末尾而不是开头

编辑:要访问
列表
元素,请使用

例如:

int size = myList.size();
for (int i = 0 ; i < size ; i++) {
    //...
    Notes note = myList.get(i);
    //...
}
int size=myList.size();
对于(int i=0;i
Incognito我希望能够看到现有的注释,不想覆盖它们,但是它们在数组列表中的任何方法都允许您像在普通数组中一样访问特定数组。