Files and Streams in java -- How to display successive records

Hi,
I developed an application in which I enter name and id in a gui. Each time I enter, the records are added in the sequential file.
Then I have a button to open that file. It also opens.
Then I have a button to read the records(which has name and id) in sequential order. Each time I click next button records should be displayed in their respective text boxes. But, only the 1st record is displyed. Then I get IOException. Why am I not able to move to records after 1st record. How could I disply records till EOF is reached.
Plz see my code for open function and read next function
private void openFile()
input=new ObjectInputStream(new FileInputStream("c:/file.dat"));
openf.setEnabled(false);
readf.setEnabled(true);
private void readNext()
rcrd=(Record)input.readObject();
String val[]= {String.valueOf(rcrd.getId()),rcrd.getName(),rcrd.getAddr()};
gui.setFieldValues(val);
Why am I not able to move to records after 1st record. How could I disply records till EOF is reached.

package files;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import files.FileGui;
import files.Record;
import java.util.*;
import java.lang.*;
public class CreateFile extends JFrame
     private ObjectOutputStream output;
     private FileGui gui;
     private JButton enter, readf, openf;
     private Record rcrd;
     private ObjectInputStream input;
     boolean eof;
     LinkedList link = new LinkedList();
     ListIterator li=link.listIterator();
     public CreateFile()
          super( "Creating sequential file" );
          getContentPane().setLayout( new BorderLayout() );
          gui = new FileGui();
          enter = gui.getTask1();
          enter.setText("Enter");
          enter.setEnabled(true);
          enter.addActionListener(
                    new ActionListener()
                         public void actionPerformed( ActionEvent e )
                              addRecord1();
          addWindowListener(
                    new WindowAdapter()
                         public void windowClosing( WindowEvent e )
                              if(output != null)
                                   closeFile();
                              else
                                   System.exit(0);
          openf = gui.getTask2();
          openf.setText("Open");
          openf.addActionListener(
                    new ActionListener()
                         public void actionPerformed(ActionEvent e)
                              openFile();
readf = gui.getTask3();
          readf.setText("Read Next");
          readf.addActionListener(
                    new ActionListener()
                         public void actionPerformed(ActionEvent e)
                              readFile();
          getContentPane().add(gui, BorderLayout.CENTER);
          setSize(300,200);
          show();
     private void readFile()
                    try
                         rcrd=(Record)input.readObject();
                         String val[]={String.valueOf(rcrd.getId()),rcrd.getName(),rcrd.getAddr()};
                         gui.setFieldValues(val);
                    catch(EOFException eof)
                         System.out.print("EOFException");
                    catch(ClassNotFoundException cnfe)
                         System.out.print("ClassNotFoundException");
                    catch(IOException ioe)
                         System.out.print("IOException");
     private void openFile()
               JFileChooser fileChooser = new JFileChooser();
               fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
               int result = fileChooser.showOpenDialog(this);
               if(result==JFileChooser.CANCEL_OPTION)
                    return;
               File fileName = fileChooser.getSelectedFile();
               if(fileName==null || fileName.getName().equals(""))
                    JOptionPane.showMessageDialog(this, "Invalid file name", "Invalid file name", JOptionPane.ERROR_MESSAGE);
               else
                    try
                         //File fileName=new File("c:/file.txt");
                         input=new ObjectInputStream(new FileInputStream("c:/file.dat"));
                         openf.setEnabled(false);
                         readf.setEnabled(true);
               catch (Exception e)
                    System.out.println("Error in opening file");
     private void closeFile()
          try
               output.close();
               System.exit(0);
          catch(IOException ex)
               JOptionPane.showMessageDialog(this, "Error Closing File", "Error", JOptionPane.ERROR_MESSAGE);
               System.exit(1);
     private void addRecord1()
          String[] fieldValues = gui.getFieldValues();
          if(! fieldValues[0].equals(""))
               try
                    int id=Integer.parseInt(fieldValues[0]);
                    if(id>0)
                         Record rcrd = new Record(id, fieldValues[1], fieldValues[2]);
                         System.out.println(" " +rcrd.getString());
                         link.add(" " +rcrd.getString());
                         //System.out.println("Hello3");
                         FileOutputStream fos = new FileOutputStream("c:/file.dat",true);
                         output = new ObjectOutputStream(fos);
                         output.writeObject(rcrd);
                         output.flush();
                         //gui.clearFields();
                         readf.setEnabled(true);
               catch (NumberFormatException nfe)
                    JOptionPane.showMessageDialog(this, "Incorrect id", "Invalid number formate", JOptionPane.ERROR_MESSAGE);
               catch (IOException io)
                    closeFile();
     public static void main(String args[])
          new CreateFile();
}

Similar Messages

  • How to display success message in function module

    Hi Experts,
                      I am developing new function module regarding URL finder. As per my requirement ,
                                                 If USER ID is not provided -display SUCCESS MESSAGE and Provide sy-uname.
    how to display success message.if possible can You write the code.
    Thanks
    raju

    Hello,
    We can use an exporting parameter like single character field like 'S' for success and 'E' for error instead of a message.
    Another option is you can use the Tables parameter and populate Return table with error or success message. Return table of type 'BAPIRET2'.
    Hope this might help you!
    Regards,
    MM Jaffer.

  • File and Streams resources

    Can anyone tell me of any links where I can find extensive material about Files & Streams in Java?
    Many Thanks

    Hi there
    try
    http://java.sun.com/docs/books/tutorial/essential/io/index.html
    There is a section on Files and Streams
    Alternatively there is a very useful book by Elliot Rusty Haroldcalled "Java I/O" O'Reilly & Associates; ISBN: 1565924851
    This is very readable and helpful

  • Hello, in former Versions it was available to mark text in a pdf file and change it. How do I have

    Hello, in further Versions it was available to mark text in a pdf file and change it. How do I have to act, to do the same in Acrobat XI? Also I need to now, how can I import a graphic file into a pdf to add my handwrote signature or a picture of mine? Please help me soon. It is urgent.

    Moving the discussion to Acrobat Forums

  • I purchased a song that is a corrupted file and won't download - how do i get that song or get a refund as i already have been charged

    i purchased a song thats a corrupted file and won't download - how do i get this song or get a refund as i have already benn charged ?

    http://support.apple.com/kb/HT1933
    Regards.

  • I delected the "Recently Added" playlist file and don't know how to get it back, does anyone know how?

    I delected the "Recently Added" plylist file and don't know how to get it back, any suggestions as to how to get it back?

    See Restore Original Smart Playlists.
    tt2

  • Files and Streams java -- Sequential access file-- Plz help me

    I am able to add records in sequential file and then also I am able to display each record sequentially.
    But after closing the file if I open the file for adding records, the newly added records are not being displayed while clicking the "next" button to read them.
    How to bring the pointer to end of records in sequential file , so that when I stat adding records after opening an existing file, they should be added successively after the last record in that file.
    enter = gui.getTask1();
              enter.setText("Enter");
              enter.setEnabled(true);
              try
              FileOutputStream fos = new FileOutputStream("c:/file.txt",true);
              output = new ObjectOutputStream(fos);
              enter.addActionListener(
                        new ActionListener()
                             public void actionPerformed( ActionEvent e )
                                  addRecord1();
              catch(Exception e)
                   System.out.println("Error in ObjectOutputStream before pressing Enter button");
    private void addRecord1()
              String[] fieldValues = gui.getFieldValues();
              if(! fieldValues[0].equals(""))
                   try
                        int id=Integer.parseInt(fieldValues[0]);
                        if(id>0)
                             Record rcrd = new Record(id, fieldValues[1], fieldValues[2]);
                             System.out.println(" " +rcrd.getString()+"\n");
                             output.writeObject(rcrd);
                             output.flush();
                             gui.clearFields();
                             display.append(" "+rcrd.getId()+" "+rcrd.getName()+" "+rcrd.getAddr()+"\n");
                             readf.setEnabled(true);
                   catch (NumberFormatException nfe)
                        JOptionPane.showMessageDialog(this, "Incorrect id", "Invalid number formate", JOptionPane.ERROR_MESSAGE);
                   catch (IOException io)
                        closeFile();
         }Message was edited by:
    MissJavaa

    You can't append multiple ObjectOutputStreams to the same file unless you know where one ends and the other starts and you use a new ObjectInputStream at each join. And even that mightn't work because of buffering. Keep the file open or start a new file.
    And I wouldn't call the file 'file.txt', it isn't a text file.

  • Read text file and split up.  How???

    Can anyone tell me how to split up a string into words.
    The string is: "me and my cat both like milk and cookies"
    I need read through the file and diplay the biggest word(s) and the smallest word(s).
    I would appriciate help on this as i am really stumped
    Thanks
    ..::M::..
    p.s. I also need to be able to do this in C++ but if i know it in java i think the C++ will be simple

    String[] words = "your sentencehere".split("\\s+");
    and now you have array of strings, each containigwords from text.
    this is helpful, is there a version in C++ tooThe short answer for C++ is "no". Unless of course you install one of the regexp libraries available. But there is no native language way to do it like in Java.
    However if they words always have spaces and always will have spaces you can write your C++ code like this:
    vector <string> words;
    ifstream inFile( "file.txt" );
    string temp;
    inFile >> temp;
    words.push_back( temp );
    PS I have not tested this code in anyway but it should be simple enough

  • Files and Streams Question

    Hi! Java is my first and only language; and I am very new to Java. This is my first somewhat knowledgeable attempt to get a question answered on a forum of any kind. I put a question in this forum about two weeks ago, but I lost it. So, I am very grateful for any comments that can improve my ability to get my questions answered. Thank you. Mike
    Here is my problem:
    Read File: I want to read pT.txt or plainText.txt file (that is encoded in Unicode) into a String variable (inputLine).
    (I want to do some processing of the inputLine String and assign the processing results into outputLine.)
    Write File: I want to write outputLine into aPT.txt or anotherPlainText.txt file (that is encoded in ASCII).
    Here is my question:
    What is the simplest or easiest code for the Read File and Write File instructions?
    Thanks. Mike

    Why am I not entering the while loop?
    import java.io.*;
    //not yet: import java.util.Scanner;
    public class FnS
         public static void main(String[] args) throws Exception
              File inFile = new File("pT.txt");
              BufferedReader input = new BufferedReader (new FileReader (inFile));          
              //not yet: PrintWriter outFile = new PrintWriter (new BufferedWriter (new FileWriter ("outFile.txt")));
              String peek1, peek2, peek3, peek4, peek5, peek6;
              boolean pk1 = false, pk2 = false, pk3 = false, pk4 = false, pk5 = false;
              int count = 0;
              while(input.ready())     // begin search
                   peek1  = input.readLine();
                   if (peek1.startsWith("One"))
                        pk1 = true;
                        peek2  = input.readLine();
                        if(pk1 && peek2.startsWith("Two"))
                             pk2 =true;
                             while(pk1 && pk2)
                                  peek3  = input.readLine();
                                  if(pk1 && pk2 && peek3.startsWith("Three"))
                                       pk3 = true;
                                       peek4 = input.readLine();
                                       if(pk1 && pk2 && pk3 && peek4.startsWith("Four"))
                                            pk4 = true;
                                            while(pk1 && pk2 && pk3 && pk4)
                                                 peek5 = input.readLine();
                                                 if(pk1 && pk2 && pk3 && pk4 && peek5.startsWith("Five"))
                                                      pk5 = true;
                                                      peek6 = input.readLine();
                                                      if(pk1 && pk2 && pk3 && pk4 && pk5 && peek6.startsWith("Six"))
                                                            *  (I want to do some processing of the inputLine String and assign the processing results into outputLine.)
                                                            *     Write File:  I want to write outputLine into aPT.txt or anotherPlainText.txt file (that is encoded in ASCII).
                                                            *     Here is my question:
                                                            *  What is the simplest or easiest code for the Read File and Write File instructions?
                                                            *     Thanks.  Mike
                                                           System.out.println("\n" + peek6 + "\n" + peek5 + "\n" + peek4 + "\n" + peek3);
                                                           count++;
                                                           pk1 = false;
                                                           pk2 = false;
                                                           pk3 = false;
                                                           pk4 = false;
                                                           pk5 = false;
                                                           break;
                                                      }//if pk1 && pk2 && pk3 && pk4 && pk5 && peek6
                                                      else
                                                           pk1 = false;
                                                           pk2 = false;
                                                           pk3 = false;
                                                           pk4 = false;
                                                           pk5 = false;
                                                           break;
                                                      }//else begin  new search
                                                 }//if pk1 && pk2 && pk3 && pk4 && peek5
                                            }//while pk1 && pk2 && pk3 && pk4
                                       }// if pk1 && pk2 && pk3 && peek4
                                       else
                                            pk1 = false;
                                            pk2 = false;
                                            pk3 = false;
                                            pk4 = false;
                                            pk5 = false;
                                            break;
                                       }//else begin new search
                                  }//if pk1 && pk2 && peek3
                             }//while pk1 && pk2                              
                        }//if pk1 && peek2
                        else
                             pk1 = false;
                             pk2 = false;
                             pk3 = false;
                             pk4 = false;
                             pk5 = false;
                        }//else begin new search
                   }//if peek1
              }//while hasNext
              System.out.println("\nemail count = " + count);
    }

  • When I "save page as" I also get a folder with gif images, jscript script files and other similar items. how can I stop this.

    When I "save page as" via the file button at the top edge of the page, I also get a folder containing gif images, jscript script files and other similar items. I am not is allowed to delete it unless I also delete the page I need. How can I stop this from happening. is it the way I've configured firefox perhaps.
    == since I installed firefox

    Make sure that you have selected "Web Page, complete" to save the page.

  • I lost an .msi file and cannot do updates, how do i fix the file?

    I lost an .msi file and cannot do updates to my Itunes system, Apple has not been abe to help, where do i find the .msi file or how do i repair the system?  All prior updates to the system have worked, but the latest 10.5.2 will not update due to this lost file.

    Try the following steps:
    1. Go to Microsoft website to fix install and Unistall problems. Click "Run now" from Fix it to remove all iTunes & related installer files:
    http://support.microsoft.com/mats/Program_Install_and_Uninstall
    Be aware that Windows Installer CleanUp Utility will not remove the actual program from your computer. However, it will remove the installation files so that you can start the installation, upgrade, or uninstall over.
    2. You should remove all instances of iTunes and the rest of the components listed below:
    it may be necessary to remove all traces of iTunes, QuickTime, and related software components from your computer before reinstalling iTunes.
    Use the Control Panel to uninstall iTunes and related software components in the following order:
    iTunes
    QuickTime
    Apple Software Update
    Apple Mobile Device Support
    Bonjour
    Apple Application Support (iTunes 9 or later)
    Follow the instructions from Apple article listed here: http://support.apple.com/kb/HT1923 to remove all components
    3. Reboot your computer. Next, download iTunes from here:http://www.apple.com/itunes/download/ and install from scratch

  • How to Display multiple records in Table in VC without using BAPI.

    Hi All,
    I am working on Visual composer (NW2004s SP10). I am trying to display Poitems from BAPI_PO_GETDETAIL. I am creating my front end using VC. I have created one form and one Table where I want to display POItems. I am writing my logic of retrieving data from BAPI in CAF.I am connecting them in Guided procedures.When I run my process in Guided Procedures I am getting single row displayed in table. Can Anyone help me how to display multiple rows in table.
    Regards,
    Sheetal

    Hi Sheetal,
    if the BAPI returns a table, then you get multiple rows. From which system is the BAPI, so that I can check the BAPI to give you further information.
    Best Regards,
    marcel

  • How to display multiple records in smart forms in new page for each record

    Hi,
              How to display the data from a internal table in a smart form.
    I want each record to be displayed in seperate page.
    please tell me with example.
    thank u,
    Sarath

    Do this ,
       in the main window - open a loop on your internal table ,
    within the loop open the text and give the output fields,
    after this text  use the Command node and in this set the next page as page1,
    so when the loop gets executed its first record will be on the first page and the second record will be on the next page and so on ..
    Reward to usefull answers.

  • How to display success message when data is changed in the custom tab in MM

    Hi,
    I have added a new custom data tab in the MM01/MM02/MM03 transactions. Whenever I do changes to fields in the custom tab in MM02 transaction, and no changes in the standard tabs, I will get a message stating "No Changes Made".
    But if I do changes in the standard tabs, it works as usual with display of message "Changes to particular material has been done.
    Please let me know, if anyone of you know, how to display the success message if the changes to the custom tab is done.
    Thanks in advance,
    sudhanva

    Hi Sudhanva,
    The exit EXIT_SAPLMGMU_0001 is a function exit that you can use for custom validation but not to add custom tab/screen.
    But the message issued by SAP is not related to this Function Exit.
    If you have used a Screen Exit, then there must be some Function Exits also in the same Enhancement using which you can assign the value of custom fields to/from the standard structure. Thus when the value of any custom field is changed the system can understand that the some changes have been changed and will  not issue the message.
    In case you have used a BADI, there can be other methods in the BADi using whcih you can assign the value of custom fields to/from the standard structure. This might also prevent the message from being displayed.
    I could try giving you further details if you can provide the name of the Enhancement/BADi that you used to add the additional tab.
    Hope this helps.
    Regards,
    Abhisek.

  • How to display interaction record data in action mail directly ??

    Hi Gurus,
    We use CRM 7.0
    I have service order for the follow up transaction of interaction record.
    I define many actions for changing status of service order. I do it in actions with a method implemented from EXEC_METHODCALL_PPF. I provide e-mails with this badi. But whn I look to mails, I see that data is attached to mail as a pdf document. I don't want this attachment. I want to see what I am sending directly inside the mail, front page of mail, when user open mail he/she should see data directly, not with opning an attachment. I don't know why an attachment is created...what cause which paramter do it... I want a simple mail and seen directly. How can I do this? Please help me..

    Hi.
    Please check your SCOT parameters.
    Regards

Maybe you are looking for

  • My Photo's are disappearing before my eyes

    Hi I have just over 4000 photo's in my iphoto and while scrolling down I can see the photo's then they are going blank as I keep scrolling the same is happening and then some stay! Scroll a little further and I see them then they go blank again and s

  • Trojan horse Dropper.Generic2.CKPW

    AVG found Trojan horse Dropper.Generic2.CKPW when I downloaded the update to QuickTime. AVG can't delete or vault it.

  • Following error while deleting offline db tables

    Hello while I am deleting a offline DB table build out of a database view I am getting following error: java.lang.NullPointerException      at oracle.jdeveloper.offlinedb.browser.BaseSchemaObjectNode.getSchemaObject(BaseSchemaObjectNode.java:50)     

  • Resource Adapter ans Logging

    Does anyone know, how to let the server set a LogWriter for a resource adapter? What is the meaning of the <property> elements in sun-ra.xml? Thanks, Guido

  • How do I obtain a refund???

    I need to know how I can obtain a refund or a reduce price for a Microsoft Word program? Pages is unacceptable and detrimental to my future. First, I nearly lost a job because I converted my Resume from Pages to Word and the employer received the doc