So I'm working on a database-type program for the Java course that i'm doing and i seem to have run into a bit of a roadblock. This class compiles and runs alright but where i have a problem with it is when i try to amend the text file using the same class. All it does is overwrite whatever is on the txt file. I've tried to include a method that reads the text file first and then includes the contents in the output but i cant get it to work because the second i use Formatter.format(Scanner(system.in)) (if that makes sense) it just replaces whatever was in the file with the new input. (I hope I'm coherent.. If not, just let me know :P)

This is the code:

import java.io.File;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.Formatter;
import java.util.FormatterClosedException;
import java.util.NoSuchElementException;
import java.util.Scanner;


public class CreateTextFile
{

   private Formatter output; 

   public void openFile()
   {
      try
      {
         output = new Formatter( "clients.txt" ); 
      } 
      catch ( SecurityException securityException )
      {
         System.err.println(
            "You do not have write access to this file." );
         System.exit( 1 ); 
      } 
      catch ( FileNotFoundException fileNotFoundException )
      {
         System.err.println( "Error opening or creating file." );
         System.exit( 1 ); 
      } 
   } 

   
   public void addRecords()
   {
      
      AccountRecord record = new AccountRecord();
      Scanner input = new Scanner( System.in );
      
      System.out.printf( "%s\n%s\n%s\n%s\n\n",
         "To terminate input, type the end-of-file indicator ",
         "when you are prompted to enter input.",
         "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
         "On Windows type <ctrl> z then press Enter" );

      System.out.printf( "%s\n%s", 
         "Enter product code, Description, width, height, depth and weight.",
         "? " );
         
      while ( input.hasNext() ) 
      {
         try 
         {
            
            record.setAccount( input.next() ); 
            record.setFirstName( input.next() ); 
            record.setLastName( input.nextDouble() ); 
            record.setBalance( input.nextDouble() ); 
            record.setDepth(input.nextDouble());
            record.setWeight(input.nextDouble());
            
            if ( ((record.getAccount()).length() > 0) &&  ((record.getAccount()).length() <=4 ))
            {
               
               output.format( "%s %s %.2f %.2f %.2f %.2f\n", record.getAccount(), 
                  record.getFirstName(), record.getLastName(),
                  record.getBalance(), record.getDepth(), record.getWeight() );
            } 
            else
            {
               System.out.println(
                  "Product Code is between 0 and 4 characters" );
            } 
         }
         catch ( FormatterClosedException formatterClosedException )
         {
            System.err.println( "Error writing to file." );
            return;
         } 
         catch ( NoSuchElementException elementException )
         {
            System.err.println( "Invalid input. Please try again." );
            input.nextLine(); 
         } 

         System.out.printf( "%s\n%s", 
         "Enter product code, Description, width, height, depth and weight.",
         "? " );
      } 
   } 

   
   public void closeFile()
   {
      if ( output != null )
         output.close();
   } 
}

Is there some way to indicate to Formatter to skip like x amount of lines. Cos then i could count the lines with LineNumberReader from java.io.

Thanks for lookin,
Khodz

Recommended Answers

Are you trying to append to the end of the existing file? If so, see this link:
http://www.artima.com/forums/flat.jsp?forum=1&thread=118486
Not sure it that's what you're looking for...

Jump to Post

All 2 Replies

Yea that's exactly what I'm trying to do. Thanks for the speedy response.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.