Menu

Question about the FixedLengthWriter

Help
Eric Leray
2008-02-14
2013-04-22
  • Eric Leray

    Eric Leray - 2008-02-14

    I am wondering if the FixedLengthWriter has the following feature:

    - instead of writing a field in a file from left to right filling the remaining field size with the fillChar as in :
    JOHN                               DOE                                1234 ...

    is it possible to fill the same record fileds from right to left as in :
                                   JOHN                                DOE 1234...

    this is quite an important feature in order to translater properly numbers found in a file, especially if the expected fillChar is '0'.

    In the following extract the numeric field can be problematic as it gives different values : 12340 or 01234
    JOHN0000000000000000000000000000000DOE0000000000000000000000000000000012340...
    0000000000000000000000000000000JOHN00000000000000000000000000000000DOE01234...

    NB: using '0' as the fillChar is a mandatory requirement in my case.

    any help or ideas on this will be much appreciated.

     
    • Paul Zepernick

      Paul Zepernick - 2008-02-14

      Hi Eric,

      I see the dilemma...  Currently there is no way to tell the writer to zero fill the string.  Here is a utility method that you could call on the string yourself before giving it to the writer.  I will look at getting this functionality added for a future release:

          public static String zeroFillString(String field, int numOfSpaces) {
              int spacesNeeded=0;
              //deletes any spaces in the string
               //calculates amount of 0's need to complete the field
              spacesNeeded=numOfSpaces - field.length();
                      final StringBuffer sb = new StringBuffer();
                      sb.append(field);
              for (int i=0; i < spacesNeeded; i++){
                              sb.insert(0, "0");
              }
              return sb.toString();

          }

      So your code might look something like this:
      writer.addRecordEntry("NUMERICFIELD", zeroFillString("100", 10));

      I realize it stinks to have to basically define the field length in the code when it is already in the XML, but I don't have any other ideas at the moment.

      Thanks,

      Paul

       
    • Guy de Pourtales

      Hi Eric and Paul

      At a first look, it should be simpler to use java.text.NumberFormat to format the numeric field with the correct length and default front filling. Another solution is to create to new classes :

      a. FrontFillFixedLengthWriter extends FixedLengthWriter and to overwrite its method "protected char[] formattedValue(Object value, final ColumnMetaData element)" to implement front filling instead of back filling
      b. FrontFillFixedWriterFactory extends FixedWriterFactory overriding just "public Writer createWriter(final java.io.Writer output) throws IOException" to return  a FrontFillFixedLengthWriter insance.

      A third solution is to implement a FormattedFixedLengthWriter accepting a java.text.Format subclass implementing different custom formatting options

      Regards,

      Guy

       
    • Paul Zepernick

      Paul Zepernick - 2008-02-14

      Hi Guy,

      Thanks very much for your suggestions and input...

      Thanks,

      Paul

       

Log in to post a comment.