Read to Array


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 11 of 11

Thread: Read to Array

  1. #1
    Join Date
    Mar 2005
    Posts
    24

    Read to Array

    Hi there,
    What would the code be for reading a textfile called note.txt into an array called notes please?
    Also can you tell me how to read it in...
    I just can't Fathom it.

  2. #2
    Join Date
    Jan 2005
    Posts
    45
    Hello Phil,

    Of what kind of array do you talk about ?... Do you want to store a text file in a String ?
    Lionel Badiou
    CodeFutures -
    Java Code Generation

    http://www.codefutures.com



  3. #3
    Join Date
    Mar 2005
    Posts
    24
    Well I wish to to have strings, I want to take the info from the file and read it into the array then to be able to read that info from the array into textfields and edit it then if you click save that it would save over the original file.
    THe main thing i want to do is get the stuff into the array a general outline of how this would be done would be appreciated!
    Thanks,
    Phil

  4. #4
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Use BufferedReader and ArrayList

    Use the BufferedReader method readLine(); and the ArrayList method add(Object); in a readloop, readLine returns a null string after last line in file.
    Then use a BufferedWriter for writing the ArrayList strings back to file.

    But having said that,.. why don't you use a JEditorPane for editing this file ?
    eschew obfuscation

  5. #5
    Join Date
    Mar 2005
    Posts
    24

    Question

    Well the information is going to be details on books, eg Title, Author, Description etc... So for say the title it will be in the top of the text file so I can put the first line into the array(index[0]) which will be title and the second line will be Author which will be in the array(index[1]) and so on for each item. When the stuff is then in the array I wish To put index[0] into the txtTitle field and the index[1] into the Author Field and so on...
    Then I will edit this stuff in the text fields and click save. If i click on next I would get all the index's to count up 5 or so till they have the next books title or desc... so I can edit them...
    I dunno if this makes any sense to anyone other then me but if it does what would the code kinda be like?
    Cheers

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Oop

    If your file represents information on books in lines like

    author (book1)
    title (book1)
    description (book1)
    author (book2)
    title (book2)
    description (book2)
    .
    etc., you should make yourself a class e.g. Book that reads and stores that
    info:
    Code:
    import java.io.*;
    import java.util.*;
    
    public class Book {
      private String author=null;
      private String title=null;
      private String descr=null;
      public Book()  {}
      public boolean readBookInfo(BufferedReader br)  {
        try {
          this.author = br.readLine().trim();
          this.title = br.readLine().trim();
          this.descr = br.readLine().trim();
          return true;
        }
        catch (IOException ex) {
          return false; // no more data
        }
      }
      public String getAuthor () {
        return author;
      }
      public String getTitle () {
        return title;
      }
      public String getDescription () {
        return descr;
      }
      public void writeBookInfo(BufferedWriter bw)  throws IOException {
        bw.write(author); bw.newLine();
        bw.write(title);  bw.newLine();
        bw.write(descr); bw.newLine();
      }
      public String toString () {
        return "Author: "+author+", Title: "+title+", Description: "+descr;
      }
      /**
       * Just for kicks....
       * @param ob
       * @return
       */
      public boolean equals (Object ob) {
        if (!(ob instanceof Book)) return false;
        Book aBook=(Book)ob;
        return author.equals(aBook.getAuthor()) && title.equals(aBook.getTitle());
      }
      /**
       * First arg=inputFile, second arg=outputfile
       * @param args
       */
      public static void main(String[] args) {
        BufferedReader br = null;
        try {
          br = new BufferedReader(new FileReader(args[0]));
        }
        catch (FileNotFoundException ex1) {
          ex1.printStackTrace();
          System.exit(0);
        }
        ArrayList bookList=new ArrayList();
        // read booklist
        while (true) {
          Book aBook = new Book();
          if (aBook.readBookInfo(br)) {
            bookList.add(aBook);
          } else {
            break;
          }
        }
        try {
          br.close();
        }
        catch (IOException ex2) {
          ex2.printStackTrace();
          System.exit(0);
        }
        // show booklist
        for (int i=0;i<bookList.size();i++) {
          Book aBook=(Book)bookList.get(i);
          System.out.println(aBook);
        }
        // write booklist (to a new file)
        try {
          BufferedWriter bw = new BufferedWriter(new FileWriter(args[1]));
          for (int i = 0; i < bookList.size(); i++) {
            Book aBook = (Book) bookList.get(i);
            aBook.writeBookInfo(bw);
    
          }
          bw.close();
        }
        catch (IOException ex) {
          ex.printStackTrace();
        }
    
      }
    
    }
    Last edited by sjalle; 03-07-2005 at 01:58 PM.
    eschew obfuscation

  7. #7
    Join Date
    Mar 2005
    Posts
    24
    Hey thats great thanks, I'm still a bit lost tho. I can get the general gist but I'm not very sure how to implament it into my code.
    HEre is what I have, do you think it will be hard to get the above code to mix with mine??
    Code:
    import java.io.*;
    
    public class Books implements Serializable
    {
    	private String strTitle;
    	private String strDesc;
    	private String strAuthor;
    	private String strGenre;
    
    	Books(String strTitleIn, String strAuthorIn, String strDescIn, String strGenreIn)
    	{
    		//Assign property values
    		strTitle = strTitleIn;
    		strDesc = strDescIn;
    		strAuthor = strAuthorIn;
    		strGenre = strGenreIn;
    	}
    
    	public String getTitle()
    	{
    		//Retrieve employee name
    		return  strTitle;
    	}
    	public String getAuthor()
    	{
    		//Retrieve employee name
    		return  strAuthor;
    	}
    
    	public String getDesc()
    	{
    		//Retrieve hire date
    		return strDesc;
    	}
    		
    		public String getGenre()
    	{
    		//Retrieve employee name
    		return  strGenre;
    	}
    
    
    }
    
    
    
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class TabDemo extends JFrame implements ActionListener
    {
    	private Container c;	
    	private GridLayout gLayout;
    	private BorderLayout bLayout;
    	private FlowLayout fLayout;
    	
    	
        final static String SALESPANEL = "New Sale";
        final static String BROWSEPANEL = "Browse Books";
        final static String EDITPANEL = "Edit Book Information";
        final static String STATSPANEL = "Order Details";
        final static String TITLEPANEL = "Browse By Title";
    	final static String GENREPANEL = "Browse By Genre";
    	final static String PRICEPANEL = "Browse By Prices";
    	private JButton makeSaleBtn,saveBtn,exitBtn,nextBtn,previousBtn,readBtn,
    	cancelBtn;
        private JTextField txtTitle,txtDesc, txtAuthor, txtGenre,txtTTitle,
        txtTAuthor,txtTGenre,txtTDesc,txtGTitle,txtGAuthor,txtGGenre,txtGDesc;
        private JLabel lblTitle,lblDesc,lblAuthor, lblGenre;
        private JPanel browseTab,editTab,editBtnTab,editAuthorTxtTab,editAllTxtTab,
        editTitleTxtTab,editDescTxtTab,editGenreTxtTab,titlesTab,genresTab,pricesTab;
            FileOutputStream outputBook; //Stream to create file
    		ObjectOutputStream objSaveBook; //Stream to save an object
            FileInputStream inputBook;
    		ObjectInputStream objGetBook;
         
            
    		
            
            public void openStream()
    	{
    		try
    		{
    			//Create file and object output streams
    			outputBook = new FileOutputStream("Books.txt");
    			objSaveBook = new ObjectOutputStream(outputBook);
    			inputBook = new FileInputStream("Books.txt");
    			objGetBook = new ObjectInputStream(inputBook);
    		}
    		catch(Exception error)
    		{
    			System.err.println("Error opening file");
    		}
    	}
    	public void closeStream()
    	{
    		try
    		{
    			objSaveBook.close(); //Close the object output stream
    			outputBook.close();  //Close the file output stream
    		}
    		catch (IOException error)
    		{
    			System.err.println("Error closing file");
    		}
    		
    	}
    	
            public void addComponentToPane(Container pane) 
            {
            JTabbedPane tabbedPane = new JTabbedPane();
    		
            
            JPanel salesTab = new JPanel();
            
            gLayout = new GridLayout(1,6);
            c = getContentPane();
        
            salesTab.setLayout(gLayout);
            makeSaleBtn = new JButton("Make Sale");
    		
    		exitBtn = new JButton("Exit");
    		nextBtn = new JButton("Next");
    		previousBtn = new JButton("Previous");
    		readBtn = new JButton("Read");
    		cancelBtn = new JButton("Cancel");
    		lblTitle = new JLabel("Title:         ");
    		lblAuthor = new JLabel("Author:      ");
    		lblGenre = new JLabel("Genre:        ");
            lblDesc = new JLabel("Description:");
            txtTitle = new JTextField("Title", 20);
            txtAuthor = new JTextField("Author", 20);
            txtGenre = new JTextField("Genre", 20);
            txtDesc = new JTextField("Description", 20);
            txtGTitle = new JTextField("Title", 20);
            txtGAuthor = new JTextField("Author", 20);
            txtGGenre = new JTextField("Genre", 20);
            txtGDesc = new JTextField("Description", 20);
            txtTTitle = new JTextField("Title", 20);
            txtTAuthor = new JTextField("Author", 20);
            txtTGenre = new JTextField("Genre", 20);
            txtTDesc = new JTextField("Description", 20);
            
            salesTab.add(makeSaleBtn);
    		salesTab.add(exitBtn);
    		salesTab.add(nextBtn);
    		salesTab.add(previousBtn);
            salesTab.add(clearBtn);
            salesTab.add(cancelBtn);
    
            
            browseTab = new JPanel();
            JTabbedPane insideTabbedPane = new JTabbedPane();
            browseTab.setSize(350, 300);
            browseTab.add(insideTabbedPane);
            insideTabbedPane.addTab(TITLEPANEL, titlesTab);
            insideTabbedPane.addTab(GENREPANEL, genresTab);
            insideTabbedPane.addTab(PRICEPANEL, pricesTab);
            
            
            
            titlesTab = new JPanel();
            genresTab = new JPanel();
            pricesTab = new JPanel();
            
            editGenreTxtTab = new JPanel();
            bLayout = new BorderLayout();
    		editTab = new JPanel();
      		editBtnTab = new JPanel();
    		editAllTxtTab = new JPanel();
    		editTitleTxtTab = new JPanel();
    		editDescTxtTab = new JPanel();
    		editAuthorTxtTab = new JPanel();       
    	
    		fLayout = new FlowLayout();
    		editTab.setLayout(bLayout);
    		
    		editAllTxtTab.add(editTitleTxtTab,BorderLayout.WEST);
    		editAllTxtTab.add(editAuthorTxtTab,BorderLayout.WEST);
    		editAllTxtTab.add(editGenreTxtTab,BorderLayout.WEST);
    		editAllTxtTab.add(editDescTxtTab,BorderLayout.WEST);
    		editTab.add(editAllTxtTab);
    		editTab.add(editBtnTab,BorderLayout.SOUTH);
    		
    		saveBtn = new JButton("Save");
    		fLayout = new FlowLayout();
    		editTitleTxtTab.setLayout(fLayout);
    		editDescTxtTab.setLayout(fLayout);
    		editTitleTxtTab.add(lblTitle);
    		editTitleTxtTab.add(txtTitle);
    		editAuthorTxtTab.add(lblAuthor);
    		editGenreTxtTab.add(lblGenre);
    		editGenreTxtTab.add(txtGenre);
    		editAuthorTxtTab.add(txtAuthor);		
    		editDescTxtTab.add(lblDesc);
    		editDescTxtTab.add(txtDesc);
    		editBtnTab.add(saveBtn);
    		saveBtn.addActionListener(this);
    		txtTitle.addActionListener(this);
    		txtDesc.addActionListener(this);
    		setSize(400, 300);
    		editBtnTab.add(nextBtn);
            editBtnTab.add(previousBtn);
            editBtnTab.add(clearBtn);
            JPanel statsTab = new JPanel();
            
            statsTab.add(new JButton("Button 12"));
            statsTab.add(new JButton("Button 23"));
            statsTab.add(new JButton("Button 34"));
    
            tabbedPane.addTab(SALESPANEL, salesTab);
            tabbedPane.addTab(BROWSEPANEL, browseTab);
            tabbedPane.addTab(EDITPANEL, editTab);
            tabbedPane.addTab(STATSPANEL, statsTab);
    
            pane.add(tabbedPane, BorderLayout.CENTER);
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
    
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
    
            //Create and set up the window.
            JFrame frame = new JFrame("TabDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            TabDemo demo = new TabDemo();
            demo.addComponentToPane(frame.getContentPane());
    		demo.openStream();
    		
            //Display the window.
            //frame.pack();
            frame.setSize(500,400);
            frame.setVisible(true);
        }
        	public void actionPerformed(ActionEvent event e)
    	{
    		//Save Employee object
    		if (e.getSource() == saveBtn)
    		{
    		
    		try
    		{
    			Books bookCurrent = new Books(txtTitle.getText(),txtAuthor.getText(),txtDesc.getText(),txtGenre.getText());
    			objSaveBook.writeObject(bookCurrent);
    			objSaveBook.flush();
    			txtTitle.setText("");
    			txtDesc.setText("");
    			txtAuthor.setText("");
    			txtGenre.setText("");
    			txtTitle.requestFocus();
    		}
    		catch(Exception error)
    		{
    			System.err.println("Error writing to file");
    		}
    		}
    			if (e.getSource()==readBtn){
    				Books bookCurrent;
    				bookCurrent = (Employee) objGetBook.readObject();
    				txtTitle.setText(bookCurrent.getTitle());
    				txtDesc.setText(bookCurrent.getDesc());
    				txtAuthor.setText(bookCurrent.getAuthor());
    				txtGenre.setText(bookCurrent.getGenre());
    				txtTitle.requestFocus();
    			}
    	}
        public static void main(String[] args) 
        {
        	SwingUtilities.invokeLater(new Runnable() {
        		public void run(){
        		createAndShowGUI();	
        		}
        	});
        	
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
            
        
    });
    }
    Thanks a Million!

  8. #8
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    No.
    I see you now use the serializable interface. Then you sholud just implement the
    readObject & writeObject methods in your Books class as it is required to perform
    serialization, and forget about the readBookInfo & writeBookInfo in my code, as this
    just uses a Bufferered Reader/Writer.

    Then you can the do like:

    Books aBook=(Books)readObject(in);

    and

    writeObject(aBook)
    eschew obfuscation

  9. #9
    Join Date
    Mar 2005
    Posts
    24
    Cool man thanks, I did that and it is reading and writing
    One more thing if you don't mind, would you know how I would be able to navigate forwars and backwards? If i click readBtn it will load the first book and then load the next until eof but I can't go backwards or even restart at the start of file.
    Here's my Code so far:
    Code:
    import java.io.*;
    
    public class Books implements Serializable
    {
    	private String strTitle;
    	private String strDesc;
    	private String strAuthor;
    	private String strGenre;
    
    	Books(String strTitleIn, String strAuthorIn, String strDescIn, String strGenreIn)
    	{
    		//Assign property values
    		strTitle = strTitleIn;
    		strDesc = strDescIn;
    		strAuthor = strAuthorIn;
    		strGenre = strGenreIn;
    	}
    
    	public String getTitle()
    	{
    		//Retrieve employee name
    		return  strTitle;
    	}
    	public String getAuthor()
    	{
    		//Retrieve employee name
    		return  strAuthor;
    	}
    
    	public String getDesc()
    	{
    		//Retrieve hire date
    		return strDesc;
    	}
    		
    		public String getGenre()
    	{
    		//Retrieve employee name
    		return  strGenre;
    	}
    
    
    }
    
    
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    
    public class TabDemo extends JFrame implements ActionListener
    {
    	private Container c;	
    	private GridLayout gLayout;
    	private BorderLayout bLayout;
    	private FlowLayout fLayout;
    	
    	
        final static String SALESPANEL = "New Sale";
        final static String BROWSEPANEL = "Browse Books";
        final static String EDITPANEL = "Edit Book Information";
        final static String STATSPANEL = "Order Details";
        final static String TITLEPANEL = "Browse By Title";
    	final static String GENREPANEL = "Browse By Genre";
    	final static String PRICEPANEL = "Browse By Prices";
    	private String strTitle, strAuthor, strDesc, strGenre;
    	private JButton makeSaleBtn,saveBtn,exitBtn,nextBtn,previousBtn,readBtn,
    	cancelBtn;
        private JTextField txtTitle,txtDesc, txtAuthor, txtGenre,txtTTitle,
        txtTAuthor,txtTGenre,txtTDesc,txtGTitle,txtGAuthor,txtGGenre,txtGDesc;
        private JLabel lblTitle,lblDesc,lblAuthor, lblGenre;
        private JPanel browseTab,editTab,editBtnTab,editAuthorTxtTab,editAllTxtTab,
        editTitleTxtTab,editDescTxtTab,editGenreTxtTab,titlesTab,genresTab,pricesTab;
            FileOutputStream outputBook; //Stream to create file
    		ObjectOutputStream objSaveBook; //Stream to save an object
            FileInputStream inputBook;
    		ObjectInputStream objGetBook;
         
            
    		
            
            public void openStream()
    	{
    		try
    		{
    			//Create file and object output streams
    			outputBook = new FileOutputStream("Books.txt");
    			objSaveBook = new ObjectOutputStream(outputBook);
    			inputBook = new FileInputStream("Books.txt");
    			objGetBook = new ObjectInputStream(inputBook);
    		}
    		catch(Exception error)
    		{
    			System.err.println("Error opening file");
    		}
    	}
    	public void closeStream()
    	{
    		try
    		{
    			objSaveBook.close(); //Close the object output stream
    			outputBook.close();  //Close the file output stream
    		}
    		catch (IOException error)
    		{
    			System.err.println("Error closing file");
    		}
    		
    	}
    	
            public void addComponentToPane(Container pane) 
            {
            JTabbedPane tabbedPane = new JTabbedPane();
    		
            
            JPanel salesTab = new JPanel();
            
            gLayout = new GridLayout(1,6);
            c = getContentPane();
        
            salesTab.setLayout(gLayout);
            makeSaleBtn = new JButton("Make Sale");
    		
    		
    		
    
    		
    		exitBtn = new JButton("Exit");
    		nextBtn = new JButton("Next");
    		previousBtn = new JButton("Previous");
    		readBtn = new JButton("Read");
    		cancelBtn = new JButton("Cancel");
    		lblTitle = new JLabel("Title:         ");
    		lblAuthor = new JLabel("Author:      ");
    		lblGenre = new JLabel("Genre:        ");
            lblDesc = new JLabel("Description:");
            txtTitle = new JTextField("Title", 20);
            txtAuthor = new JTextField("Author", 20);
            txtGenre = new JTextField("Genre", 20);
            txtDesc = new JTextField("Description", 20);
            txtGTitle = new JTextField("Title", 20);
            txtGAuthor = new JTextField("Author", 20);
            txtGGenre = new JTextField("Genre", 20);
            txtGDesc = new JTextField("Description", 20);
            txtTTitle = new JTextField("Title", 20);
            txtTAuthor = new JTextField("Author", 20);
            txtTGenre = new JTextField("Genre", 20);
            txtTDesc = new JTextField("Description", 20);
            
            salesTab.add(makeSaleBtn);
    		salesTab.add(exitBtn);
    		salesTab.add(nextBtn);
    		salesTab.add(previousBtn);
            salesTab.add(readBtn);
            salesTab.add(cancelBtn);
    
            
            browseTab = new JPanel();
            JTabbedPane insideTabbedPane = new JTabbedPane();
            browseTab.setSize(350, 300);
            browseTab.add(insideTabbedPane);
            insideTabbedPane.addTab(TITLEPANEL, titlesTab);
            insideTabbedPane.addTab(GENREPANEL, genresTab);
            insideTabbedPane.addTab(PRICEPANEL, pricesTab);
            
            
            
            titlesTab = new JPanel();
            genresTab = new JPanel();
            pricesTab = new JPanel();
            
            editGenreTxtTab = new JPanel();
            bLayout = new BorderLayout();
    		editTab = new JPanel();
      		editBtnTab = new JPanel();
    		editAllTxtTab = new JPanel();
    		editTitleTxtTab = new JPanel();
    		editDescTxtTab = new JPanel();
    		editAuthorTxtTab = new JPanel();       
    	
    		fLayout = new FlowLayout();
    		editTab.setLayout(bLayout);
    		
    		editAllTxtTab.add(editTitleTxtTab,BorderLayout.WEST);
    		editAllTxtTab.add(editAuthorTxtTab,BorderLayout.WEST);
    		editAllTxtTab.add(editGenreTxtTab,BorderLayout.WEST);
    		editAllTxtTab.add(editDescTxtTab,BorderLayout.WEST);
    		editTab.add(editAllTxtTab);
    		editTab.add(editBtnTab,BorderLayout.SOUTH);
    		
    		saveBtn = new JButton("Save");
    		fLayout = new FlowLayout();
    		editTitleTxtTab.setLayout(fLayout);
    		editDescTxtTab.setLayout(fLayout);
    		editTitleTxtTab.add(lblTitle);
    		editTitleTxtTab.add(txtTitle);
    		editAuthorTxtTab.add(lblAuthor);
    		editGenreTxtTab.add(lblGenre);
    		editGenreTxtTab.add(txtGenre);
    		editAuthorTxtTab.add(txtAuthor);		
    		editDescTxtTab.add(lblDesc);
    		editDescTxtTab.add(txtDesc);
    		editBtnTab.add(saveBtn);
    		saveBtn.addActionListener(this);
    		readBtn.addActionListener(this);
    		txtTitle.addActionListener(this);
    		txtDesc.addActionListener(this);
    		setSize(400, 300);
    		editBtnTab.add(nextBtn);
            editBtnTab.add(previousBtn);
            editBtnTab.add(readBtn);
            JPanel statsTab = new JPanel();
            
            statsTab.add(new JButton("Button 12"));
            statsTab.add(new JButton("Button 23"));
            statsTab.add(new JButton("Button 34"));
    
            tabbedPane.addTab(SALESPANEL, salesTab);
            tabbedPane.addTab(BROWSEPANEL, browseTab);
            tabbedPane.addTab(EDITPANEL, editTab);
            tabbedPane.addTab(STATSPANEL, statsTab);
    
            pane.add(tabbedPane, BorderLayout.CENTER);
        }
    
        /**
         * Create the GUI and show it.  For thread safety,
         * this method should be invoked from the
         * event-dispatching thread.
         */
    
        private static void createAndShowGUI() {
            //Make sure we have nice window decorations.
            JFrame.setDefaultLookAndFeelDecorated(true);
    
            //Create and set up the window.
            JFrame frame = new JFrame("TabDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //Create and set up the content pane.
            TabDemo demo = new TabDemo();
            demo.addComponentToPane(frame.getContentPane());
    		demo.openStream();
    		
            //Display the window.
            //frame.pack();
            frame.setSize(500,400);
            frame.setVisible(true);
        }
        	public void actionPerformed(ActionEvent e)
    	{
    		//Save Employee object
    		if (e.getSource() == saveBtn)
    		{
    		
    		try
    		{
    		strTitle = txtTitle.getText();
    		strDesc = txtDesc.getText();
    		strAuthor = txtAuthor.getText();
    		strGenre = txtGenre.getText();
    		
    			Books bookCurrent = new Books(txtTitle.getText(),txtAuthor.getText(),txtDesc.getText(),txtGenre.getText());
    			objSaveBook.writeObject(bookCurrent);
    			objSaveBook.flush();
    			txtTitle.setText("");
    			txtDesc.setText("");
    			txtAuthor.setText("");
    			txtGenre.setText("");
    			txtTitle.requestFocus();
    		}
    		catch(Exception error)
    		{
    			System.err.println("Error writing to file");
    		}
    		}
    			if (e.getSource() == readBtn){
    		try
    		{
    				Books bookCurrent;
    				bookCurrent =(Books) objGetBook.readObject();
    				txtTitle.setText(bookCurrent.getTitle());
    				txtDesc.setText(bookCurrent.getDesc());
    				txtAuthor.setText(bookCurrent.getAuthor());
    				txtGenre.setText(bookCurrent.getGenre());
    				txtTitle.requestFocus();
    			}
    		catch(Exception error)
    		{
    			System.err.println("Error reading from file");
    		}
    		}
    	}
        public static void main(String[] args) 
        {
        	SwingUtilities.invokeLater(new Runnable() {
        		public void run(){
        		createAndShowGUI();	
        		}
        	});
        	
            //Schedule a job for the event-dispatching thread:
            //creating and showing this application's GUI.
         }
        
    
    }
    Cheers

  10. #10
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Open the file, read and store every Book object in an ArrayList, and close the file.
    Then navigate the ArrayList. After you have stepped around in that list, editing data,
    you can save it like:

    out.writeObject(list);

    as the ArrayList class will use the contained elements' writeObject method for serialization.

    Kinda neat, I think, saving the whole library in one write statement.
    eschew obfuscation

  11. #11
    Join Date
    Mar 2005
    Posts
    24
    Excelent! I have gotten the array to write to the file now all I have to do is get it to Read the file first and then add the rest to it.
    Thanks very much for the great advice!
    Last edited by Phil O Reilly; 03-08-2005 at 04:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center