Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an smart way to write a fixed length flat file?

Is there any framework/library to help writing fixed length flat files in java?

I want to write a collection of beans/entities into a flat file without worrying with convertions, padding, alignment, fillers, etcs

For example, I'd like to parse a bean like:

public class Entity{
    String name = "name"; // length = 10; align left; fill with spaces
    Integer id = 123; // length = 5; align left; fill with spaces
    Integer serial = 321 // length = 5; align to right; fill with '0'
    Date register = new Date();// length = 8; convert to yyyyMMdd
}

... into ...

name      123  0032120110505
mikhas    5000 0122120110504
superuser 1    0000120101231

...

like image 445
Mikhas Avatar asked May 04 '11 14:05

Mikhas


People also ask

How do I create a fixed length text file?

Just go to: Save As -> Formatted Text (Space Delimited) (. prn)*. This will bring almost same functionality as one you have in Excel.

What is a fixed length flat file?

Fixed length files have a constant length for each field and record. There is no need to place an * in a field where there is no data since the software looks for the data in the same position in each record.

What is fixed length file example?

Fixed length files have a constant length for each field and record. For example, if you have data in a text file where the first column always has exactly 8 characters, and the second column has exactly 5, the third has exactly 12 (and so on), this would be categorized as a fixed-width text file.

How do you read a fixed-width file in Java?

getFormat(). setPadding('_'); // creates a fixed-width parser with the given settings FixedWidthParser parser = new FixedWidthParser(settings); // parses all rows in one go. List<String[]> allRows = parser. parseAll(new File("path/to/fixed.


1 Answers

If you are still looking for a framework, check out BeanIO at http://www.beanio.org

like image 194
Kevin Avatar answered Sep 18 '22 05:09

Kevin