怎么打印列表到文件?

时间:2013-12-10 08:13:21

标签: java list

我的问题是如何编写新类,它将从主类List获取信息并将ir打印到新文件中?

所以这是我的ProgramTest类,主要是:

public class ProgramTest {
    public static void main(String [] args)throws Exception {

        //Autoriai
        Author authorInf00 = new Author ("Mykolas", "Razma");
        Author authorInf01 = new Author ("Lukas", "Brazukas");
        Author authorInf02 = new Author ("Kristijonas", "Stoma");



        //Knygos
        Book bookInf00 = new Book ("Mykolas", "Razma", "Paukstis", 11111111, authorInf00);
        Book bookInf01 = new Book ("Kristijonas", "Stoma", "gandras", 2222222, authorInf01);
        Book bookInf02 = new Book ("Lukas", "Brazukas", "Varna", 3333333, authorInf02);


        //Knygu listas
        List <String> bookList = new ArrayList <String>();
        bookList.add(bookInf00.getName() + "    " + bookInf00.getIsbn() + " " + bookInf00.getFirstName() + "        " + bookInf00.getLastName());
        bookList.add(bookInf01.getName() + "        " + bookInf01.getIsbn() + "     " + bookInf01.getFirstName() + "    " + bookInf01.getLastName());
        bookList.add(bookInf02.getName() + "        " + bookInf02.getIsbn() + "     " + bookInf02.getFirstName() + "        " + bookInf02.getLastName());



        //Isveda i konsole Knygu lista
        System.out.println("Knyga       ISBN        Vardas      Pavarde");
        for(int i=0; i < bookList.size();i++)
        {           
            System.out.println(bookList.get(i));
        }


        //Sukuria instrukcija
        Txt xce = new Txt();

      //Paleidzia dirbti xml writeri
        xce.runExample();

    }

}

第二堂课是图书馆:

import java.util.List;

public class Library {
    private List <String> bookList;
    private DataWriter dataWriter;


    public List <String> getBookList(){
        return bookList;        
    }

    public void exportBookList (List <String> bookList){
        this.bookList = bookList;
    }

    public void setDataWriter(DataWriter dataWriter) {
        this.dataWriter = dataWriter;
    }

    public DataWriter getDataWriter() {
        return dataWriter;
    }       

}

DataWriter应将信息写入文件

import java.util.List;

public interface DataWriter {

    public void dataWriterBook(List<String> bookList);
}

这就是问题:不知道如何正确地写它将我的列表打印到file.txt

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

    public class Txt {

        public void runExample(){
            System.out.println("Started .. ");
            createDOMTree();
            printToFile();
            List <String> bookList;
            System.out.println("Generated file successfully.");
        }


        public void dataWriterBook(List <String> bookList){

        }

        public void printToFile(){

            try {           


                List<String> bookList = new ArrayList<String>();
                // obviously you would want to use a list with stuff in it
                BufferedWriter out = new BufferedWriter(
                  new FileWriter("C:/Users/jjegorovas/xml_failas.xml"));
                for (String item : bookList){
                    out.write(item);
                    out.newLine();
                }
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

您可以创建一个Book of Book,然后覆盖toString()方法。 例如:

class Book
{
    private string name;
    private string author;
    private int id;

    public Book(string name, string author, int id)
    {
        this.name = name;
        this.author = author;
        this.id = id;
    }

    public String toString()
    {
        return id + " - " + name + " - " + author;   //Maybe use StringBuilder
    }

}

class Main()
{
   public static void main(String [] args)
   {
      Book book1 = new Book("Book1","James",1);
      Book book2 = new Book("Book2","John",2);
      Book book3 = new Book("Book2","Smith",3);

      List<Book> bookList = new ArrayList<Book>();
      bookList.add(book1);
      bookList.add(book2);
      bookList.add(book3);

      saveBooksToFile(bookList);
   }

   public static void saveBooksToFile(List<Book> bookList)
   {
        //better use try-with-resource here
        try {           
            BufferedWriter out = new BufferedWriter(
              new FileWriter("C:/Users/jjegorovas/xml_failas.xml"));
            for (Book book : bookList){
                out.write(book.toString());
                out.newLine();
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
   }
}

答案 1 :(得分:0)

在ProgramTest.java中,将bookList传递给Txt构造函数,如下所示:

    //Sukuria instrukcija
    Txt xce = new Txt( bookList); //pass bookList

  //Paleidzia dirbti xml writeri
    xce.runExample();

修改Txt.java,如下所示(注释中的解释):

public class Txt {
    List<String> bookList; //add a field to store the book list

    public Txt(List<String> bookList) { //add a constructor method
        this.bookList = bookList;
    }

    public void printToFile() { //modify this method

        try {

            BufferedWriter out = new BufferedWriter(new FileWriter(
                    "C:/Users/jjegorovas/xml_failas.txt")); //change extension from XML to TXT
            for (String item : bookList) {
                out.write(item);
                out.newLine();
            }
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}