file won't change size when size is increased and recompiled


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: file won't change size when size is increased and recompiled

  1. #1
    newToJava Guest

    file won't change size when size is increased and recompiled


    I've got a record class and a createRandomFile class along with the read and
    write classes. My problem is when I recompile my record class and change
    my record size the record size does not change.
    CODE:
    // NOTE: This method contains a hard coded value for the
    // size of a record of information.
    public static int size() { return 72; }
    }

    if I change the .dat size() amount to say 200 so I can add more fields down
    the road if I need them, the original 72 bytes remain for my 100 records.
    the file size always stays 7200 bytes when I check the size of my .dat file.
    I've changed the class names, deleted the .class files and .dat file and
    recompiled. the .dat file always reads 7200 bytes.I can't seem to figure
    it out or is this a bug in the java.

    I'm using the jdk1.3 on W98 from the dos prompt

    Both files area included below. The problem could be to my system only.
    The "public static int size() { return 72; }" statement is at the bottom
    of
    the RecordJ1 class File.
    Any help would be greatly appreciated.

    /**********************code for RecordJ1.java***************************/
    // Record class for the RandomAccessFile programs.


    import java.io.*;
    import javax.swing.*;


    public class RecordJ1 {
    private int account;
    private String lastName;
    private String firstName;
    private double balance;

    // Read a record from the specified RandomAccessFile
    public void read( RandomAccessFile file ) throws IOException
    {
    account = file.readInt();

    char first[] = new char[ 15 ];

    for ( int i = 0; i < first.length; i++ )
    first[ i ] = file.readChar();

    firstName = new String( first ).replace( '\0', ' ');

    char last[] = new char[ 15 ];

    for ( int i = 0; i < last.length; i++ )
    last[ i ] = file.readChar();

    lastName = new String( last ).replace( '\0', ' ');

    balance = file.readDouble();
    }

    // Write a record to the specified RandomAccessFile
    public void write( RandomAccessFile file ) throws IOException
    {
    StringBuffer buf;

    file.writeInt( account );

    if ( firstName != null )
    buf = new StringBuffer( firstName );
    else
    buf = new StringBuffer( 15 );

    buf.setLength( 15 );

    file.writeChars( buf.toString() );

    if ( lastName != null )
    buf = new StringBuffer( lastName );
    else
    buf = new StringBuffer( 15 );

    buf.setLength( 15 );

    file.writeChars( buf.toString() );

    file.writeDouble( balance );
    }


    //method for calculating shipping cost
    public double shippingCost(){ //***method to calculate shipping cost

    int width, height, depth;
    int cubicVolume;
    String w_input, h_input, d_input;


    w_input=JOptionPane.showInputDialog( //frames to read in volume
    "Enter Box Width in inches: " );

    h_input=JOptionPane.showInputDialog(
    "Enter Box Height in inches: " );

    d_input=JOptionPane.showInputDialog(
    "Enter Box Depth in inches: " );

    width = Integer.parseInt( w_input ); //converts string to int for calculations
    height = Integer.parseInt( h_input );
    depth = Integer.parseInt( d_input );

    cubicVolume = (((width * height) * depth) / 12); //calculate volume



    double weight = 0;
    //weight = 0;
    String boxWeight_input;


    boxWeight_input = JOptionPane.showInputDialog( //read in weight
    "Enter the weight of parcel in pounds: " );

    weight = Double.valueOf(boxWeight_input).doubleValue();



    double shipping_Cost = 0;
    //shipping_Cost = 0.0;

    double region_Cost;
    String regionCost_input;
    int regionCost;

    regionCost_input = JOptionPane.showInputDialog( //read in region code
    "Enter shipping region: " +
    "\nPacific = 0" +
    "\nMountain = 1" +
    "\nCentral = 2" +
    "\nEastern = 3");
    regionCost = Integer.parseInt(regionCost_input);



    switch(regionCost) { //calculate region cost

    case 0: region_Cost= 14.0;
    break;

    case 1: region_Cost= 10.0;
    break;

    case 2: region_Cost= 7.0;
    break;

    case 3: region_Cost= 5.0;
    break;

    default:region_Cost=2.0;
    break;

    }//End switch



    shipping_Cost = ((cubicVolume * weight ) * .001) + region_Cost; //calculate
    cost =
    //(weight * volume * .001) + region_cost

    return ( shipping_Cost ); //***convert double to string

    }//End method


    public void setAccount( int a ) { account = a; }

    public int getAccount() { return account; }

    public void setFirstName( String f ) { firstName = f; }

    public String getFirstName() { return firstName; }

    public void setLastName( String l ) { lastName = l; }

    public String getLastName() { return lastName; }

    public void setBalance( double b ) { balance = b; }

    public double getBalance() { return balance; }

    // NOTE: This method contains a hard coded value for the
    // size of a record of information.
    public static int size() { return 72; }
    }
    /**************************end code RecordJ1.java*************************/



    /************************code for CreateRandomFileJ1.java***************/


    // This program creates a random access file sequentially
    // by writing 100 empty records to disk.

    import java.io.*;

    public class CreateRandomFileJ1 {
    private RecordJ1 blank;
    private RandomAccessFile file;

    public CreateRandomFileJ1()
    {
    blank = new RecordJ1();

    try {
    file = new RandomAccessFile( "randomFileJ1.dat", "rw" );

    for ( int i = 0; i < 100; i++ )
    blank.write( file );
    }
    catch( IOException e ) {
    System.err.println( "File not opened properly\n" +
    e.toString() );
    System.exit( 1 );
    }

    }

    public static void main( String args[] )
    {
    CreateRandomFileJ1 accounts = new CreateRandomFileJ1();
    }
    }
    /****************************end code CreateRandomFileJ1.java**********/












  2. #2
    Paul Clapham Guest

    Re: file won't change size when size is increased and recompiled

    Recompiling the class just creates a new version of the class. That's all.
    If you have existing data files with record length 72, why on earth would
    the Java compiler go and do anything to that? (That was what you were
    asking, wasn't it?) If you want to change that file, you have to change it,
    perhaps by writing another class that reads in the 72-byte records and
    writes a new file with 200-byte records.

    PC2

    newToJava <mprog@mediaone.net> wrote in message
    news:3aa5bb2e$1@news.devx.com...
    >
    > I've got a record class and a createRandomFile class along with the read

    and
    > write classes. My problem is when I recompile my record class and change
    > my record size the record size does not change.
    > CODE:
    > // NOTE: This method contains a hard coded value for the
    > // size of a record of information.
    > public static int size() { return 72; }
    > }
    >
    > if I change the .dat size() amount to say 200 so I can add more fields

    down
    > the road if I need them, the original 72 bytes remain for my 100 records.
    > the file size always stays 7200 bytes when I check the size of my .dat

    file.
    > I've changed the class names, deleted the .class files and .dat file and
    > recompiled. the .dat file always reads 7200 bytes.I can't seem to figure
    > it out or is this a bug in the java.
    >
    > I'm using the jdk1.3 on W98 from the dos prompt
    >
    > Both files area included below. The problem could be to my system only.
    > The "public static int size() { return 72; }" statement is at the bottom
    > of
    > the RecordJ1 class File.
    > Any help would be greatly appreciated.
    >
    > /**********************code for RecordJ1.java***************************/
    > // Record class for the RandomAccessFile programs.
    >
    >
    > import java.io.*;
    > import javax.swing.*;
    >
    >
    > public class RecordJ1 {
    > private int account;
    > private String lastName;
    > private String firstName;
    > private double balance;
    >
    > // Read a record from the specified RandomAccessFile
    > public void read( RandomAccessFile file ) throws IOException
    > {
    > account = file.readInt();
    >
    > char first[] = new char[ 15 ];
    >
    > for ( int i = 0; i < first.length; i++ )
    > first[ i ] = file.readChar();
    >
    > firstName = new String( first ).replace( '\0', ' ');
    >
    > char last[] = new char[ 15 ];
    >
    > for ( int i = 0; i < last.length; i++ )
    > last[ i ] = file.readChar();
    >
    > lastName = new String( last ).replace( '\0', ' ');
    >
    > balance = file.readDouble();
    > }
    >
    > // Write a record to the specified RandomAccessFile
    > public void write( RandomAccessFile file ) throws IOException
    > {
    > StringBuffer buf;
    >
    > file.writeInt( account );
    >
    > if ( firstName != null )
    > buf = new StringBuffer( firstName );
    > else
    > buf = new StringBuffer( 15 );
    >
    > buf.setLength( 15 );
    >
    > file.writeChars( buf.toString() );
    >
    > if ( lastName != null )
    > buf = new StringBuffer( lastName );
    > else
    > buf = new StringBuffer( 15 );
    >
    > buf.setLength( 15 );
    >
    > file.writeChars( buf.toString() );
    >
    > file.writeDouble( balance );
    > }
    >
    >
    > file://method for calculating shipping cost
    > public double shippingCost(){ file://***method to calculate shipping cost
    >
    > int width, height, depth;
    > int cubicVolume;
    > String w_input, h_input, d_input;
    >
    >
    > w_input=JOptionPane.showInputDialog( file://frames to read in volume
    > "Enter Box Width in inches: " );
    >
    > h_input=JOptionPane.showInputDialog(
    > "Enter Box Height in inches: " );
    >
    > d_input=JOptionPane.showInputDialog(
    > "Enter Box Depth in inches: " );
    >
    > width = Integer.parseInt( w_input ); file://converts string to int for

    calculations
    > height = Integer.parseInt( h_input );
    > depth = Integer.parseInt( d_input );
    >
    > cubicVolume = (((width * height) * depth) / 12); file://calculate volume
    >
    >
    >
    > double weight = 0;
    > file://weight = 0;
    > String boxWeight_input;
    >
    >
    > boxWeight_input = JOptionPane.showInputDialog( file://read in weight
    > "Enter the weight of parcel in pounds: " );
    >
    > weight = Double.valueOf(boxWeight_input).doubleValue();
    >
    >
    >
    > double shipping_Cost = 0;
    > file://shipping_Cost = 0.0;
    >
    > double region_Cost;
    > String regionCost_input;
    > int regionCost;
    >
    > regionCost_input = JOptionPane.showInputDialog( file://read in region code
    > "Enter shipping region: " +
    > "\nPacific = 0" +
    > "\nMountain = 1" +
    > "\nCentral = 2" +
    > "\nEastern = 3");
    > regionCost = Integer.parseInt(regionCost_input);
    >
    >
    >
    > switch(regionCost) { file://calculate region cost
    >
    > case 0: region_Cost= 14.0;
    > break;
    >
    > case 1: region_Cost= 10.0;
    > break;
    >
    > case 2: region_Cost= 7.0;
    > break;
    >
    > case 3: region_Cost= 5.0;
    > break;
    >
    > default:region_Cost=2.0;
    > break;
    >
    > }//End switch
    >
    >
    >
    > shipping_Cost = ((cubicVolume * weight ) * .001) + region_Cost;

    file://calculate
    > cost =
    > file://(weight * volume * .001) + region_cost
    >
    > return ( shipping_Cost ); file://***convert double to string
    >
    > }//End method
    >
    >
    > public void setAccount( int a ) { account = a; }
    >
    > public int getAccount() { return account; }
    >
    > public void setFirstName( String f ) { firstName = f; }
    >
    > public String getFirstName() { return firstName; }
    >
    > public void setLastName( String l ) { lastName = l; }
    >
    > public String getLastName() { return lastName; }
    >
    > public void setBalance( double b ) { balance = b; }
    >
    > public double getBalance() { return balance; }
    >
    > // NOTE: This method contains a hard coded value for the
    > // size of a record of information.
    > public static int size() { return 72; }
    > }
    > /**************************end code

    RecordJ1.java*************************/
    >
    >
    >
    > /************************code for CreateRandomFileJ1.java***************/
    >
    >
    > // This program creates a random access file sequentially
    > // by writing 100 empty records to disk.
    >
    > import java.io.*;
    >
    > public class CreateRandomFileJ1 {
    > private RecordJ1 blank;
    > private RandomAccessFile file;
    >
    > public CreateRandomFileJ1()
    > {
    > blank = new RecordJ1();
    >
    > try {
    > file = new RandomAccessFile( "randomFileJ1.dat", "rw" );
    >
    > for ( int i = 0; i < 100; i++ )
    > blank.write( file );
    > }
    > catch( IOException e ) {
    > System.err.println( "File not opened properly\n" +
    > e.toString() );
    > System.exit( 1 );
    > }
    >
    > }
    >
    > public static void main( String args[] )
    > {
    > CreateRandomFileJ1 accounts = new CreateRandomFileJ1();
    > }
    > }
    > /****************************end code CreateRandomFileJ1.java**********/
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >




  3. #3
    newToJava Guest

    Re: file won't change size when size is increased and recompiled


    Paul, I was trying to practice writing and reading random files. I wanted
    to add more fields to the RecordJ1 class and all related files.

    I don't know enough about the language yet to no why it didn't work. I assumed
    that I could copy all files ( to preserve my originals before changing )
    add my new field objects and methods, change the name to all classes and
    dat files, and recompiling was like telling the java compiler I had a brand
    new program. Somehow the compiler still linked to the original program to
    get the 72 byte record class. I just don't know why.

    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >Recompiling the class just creates a new version of the class. That's all.
    >If you have existing data files with record length 72, why on earth would
    >the Java compiler go and do anything to that? (That was what you were
    >asking, wasn't it?) If you want to change that file, you have to change

    it,
    >perhaps by writing another class that reads in the 72-byte records and
    >writes a new file with 200-byte records.
    >
    >PC2
    >
    >newToJava <mprog@mediaone.net> wrote in message
    >news:3aa5bb2e$1@news.devx.com...
    >>
    >> I've got a record class and a createRandomFile class along with the read

    >and
    >> write classes. My problem is when I recompile my record class and change
    >> my record size the record size does not change.
    >> CODE:
    >> // NOTE: This method contains a hard coded value for the
    >> // size of a record of information.
    >> public static int size() { return 72; }
    >> }
    >>
    >> if I change the .dat size() amount to say 200 so I can add more fields

    >down
    >> the road if I need them, the original 72 bytes remain for my 100 records.
    >> the file size always stays 7200 bytes when I check the size of my .dat

    >file.
    >> I've changed the class names, deleted the .class files and .dat file and
    >> recompiled. the .dat file always reads 7200 bytes.I can't seem to figure
    >> it out or is this a bug in the java.
    >>
    >> I'm using the jdk1.3 on W98 from the dos prompt
    >>
    >> Both files area included below. The problem could be to my system only.
    >> The "public static int size() { return 72; }" statement is at the bottom
    >> of
    >> the RecordJ1 class File.
    >> Any help would be greatly appreciated.
    >>
    >> /**********************code for RecordJ1.java***************************/
    >> // Record class for the RandomAccessFile programs.
    >>
    >>
    >> import java.io.*;
    >> import javax.swing.*;
    >>
    >>
    >> public class RecordJ1 {
    >> private int account;
    >> private String lastName;
    >> private String firstName;
    >> private double balance;
    >>
    >> // Read a record from the specified RandomAccessFile
    >> public void read( RandomAccessFile file ) throws IOException
    >> {
    >> account = file.readInt();
    >>
    >> char first[] = new char[ 15 ];
    >>
    >> for ( int i = 0; i < first.length; i++ )
    >> first[ i ] = file.readChar();
    >>
    >> firstName = new String( first ).replace( '\0', ' ');
    >>
    >> char last[] = new char[ 15 ];
    >>
    >> for ( int i = 0; i < last.length; i++ )
    >> last[ i ] = file.readChar();
    >>
    >> lastName = new String( last ).replace( '\0', ' ');
    >>
    >> balance = file.readDouble();
    >> }
    >>
    >> // Write a record to the specified RandomAccessFile
    >> public void write( RandomAccessFile file ) throws IOException
    >> {
    >> StringBuffer buf;
    >>
    >> file.writeInt( account );
    >>
    >> if ( firstName != null )
    >> buf = new StringBuffer( firstName );
    >> else
    >> buf = new StringBuffer( 15 );
    >>
    >> buf.setLength( 15 );
    >>
    >> file.writeChars( buf.toString() );
    >>
    >> if ( lastName != null )
    >> buf = new StringBuffer( lastName );
    >> else
    >> buf = new StringBuffer( 15 );
    >>
    >> buf.setLength( 15 );
    >>
    >> file.writeChars( buf.toString() );
    >>
    >> file.writeDouble( balance );
    >> }
    >>
    >>
    >> file://method for calculating shipping cost
    >> public double shippingCost(){ file://***method to calculate shipping cost
    >>
    >> int width, height, depth;
    >> int cubicVolume;
    >> String w_input, h_input, d_input;
    >>
    >>
    >> w_input=JOptionPane.showInputDialog( file://frames to read in volume
    >> "Enter Box Width in inches: " );
    >>
    >> h_input=JOptionPane.showInputDialog(
    >> "Enter Box Height in inches: " );
    >>
    >> d_input=JOptionPane.showInputDialog(
    >> "Enter Box Depth in inches: " );
    >>
    >> width = Integer.parseInt( w_input ); file://converts string to int for

    >calculations
    >> height = Integer.parseInt( h_input );
    >> depth = Integer.parseInt( d_input );
    >>
    >> cubicVolume = (((width * height) * depth) / 12); file://calculate volume
    >>
    >>
    >>
    >> double weight = 0;
    >> file://weight = 0;
    >> String boxWeight_input;
    >>
    >>
    >> boxWeight_input = JOptionPane.showInputDialog( file://read in weight
    >> "Enter the weight of parcel in pounds: " );
    >>
    >> weight = Double.valueOf(boxWeight_input).doubleValue();
    >>
    >>
    >>
    >> double shipping_Cost = 0;
    >> file://shipping_Cost = 0.0;
    >>
    >> double region_Cost;
    >> String regionCost_input;
    >> int regionCost;
    >>
    >> regionCost_input = JOptionPane.showInputDialog( file://read in region

    code
    >> "Enter shipping region: " +
    >> "\nPacific = 0" +
    >> "\nMountain = 1" +
    >> "\nCentral = 2" +
    >> "\nEastern = 3");
    >> regionCost = Integer.parseInt(regionCost_input);
    >>
    >>
    >>
    >> switch(regionCost) { file://calculate region cost
    >>
    >> case 0: region_Cost= 14.0;
    >> break;
    >>
    >> case 1: region_Cost= 10.0;
    >> break;
    >>
    >> case 2: region_Cost= 7.0;
    >> break;
    >>
    >> case 3: region_Cost= 5.0;
    >> break;
    >>
    >> default:region_Cost=2.0;
    >> break;
    >>
    >> }//End switch
    >>
    >>
    >>
    >> shipping_Cost = ((cubicVolume * weight ) * .001) + region_Cost;

    >file://calculate
    >> cost =
    >> file://(weight * volume * .001) + region_cost
    >>
    >> return ( shipping_Cost ); file://***convert double to string
    >>
    >> }//End method
    >>
    >>
    >> public void setAccount( int a ) { account = a; }
    >>
    >> public int getAccount() { return account; }
    >>
    >> public void setFirstName( String f ) { firstName = f; }
    >>
    >> public String getFirstName() { return firstName; }
    >>
    >> public void setLastName( String l ) { lastName = l; }
    >>
    >> public String getLastName() { return lastName; }
    >>
    >> public void setBalance( double b ) { balance = b; }
    >>
    >> public double getBalance() { return balance; }
    >>
    >> // NOTE: This method contains a hard coded value for the
    >> // size of a record of information.
    >> public static int size() { return 72; }
    >> }
    >> /**************************end code

    >RecordJ1.java*************************/
    >>
    >>
    >>
    >> /************************code for CreateRandomFileJ1.java***************/
    >>
    >>
    >> // This program creates a random access file sequentially
    >> // by writing 100 empty records to disk.
    >>
    >> import java.io.*;
    >>
    >> public class CreateRandomFileJ1 {
    >> private RecordJ1 blank;
    >> private RandomAccessFile file;
    >>
    >> public CreateRandomFileJ1()
    >> {
    >> blank = new RecordJ1();
    >>
    >> try {
    >> file = new RandomAccessFile( "randomFileJ1.dat", "rw" );
    >>
    >> for ( int i = 0; i < 100; i++ )
    >> blank.write( file );
    >> }
    >> catch( IOException e ) {
    >> System.err.println( "File not opened properly\n" +
    >> e.toString() );
    >> System.exit( 1 );
    >> }
    >>
    >> }
    >>
    >> public static void main( String args[] )
    >> {
    >> CreateRandomFileJ1 accounts = new CreateRandomFileJ1();
    >> }
    >> }
    >> /****************************end code CreateRandomFileJ1.java**********/
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>
    >>

    >
    >



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