Giter Club home page Giter Club logo

jfilehelpers's Introduction

What is JFileHelpers?

Chances are every programmer will end up having to manipulate structured text files at any point in his career. Those files are called flat files and, until now, you had to go through that same boring script to work with them. You had to open the file, create a reader, then start reading and parsing line by line. No more.

JFileHelpers is a library that automates the tedious task of parsing and creating structured text files. It handles fixed width or delimited files with Java annotations sweetness.

JFileHelpers started as a port of the awesome Marcos Meli's FileHelpers but it's starting to take its own path and we are in need of passinate developers who would like to give us a hand.

Go ahead and try it out!

The latest binary release can always be found on the Releases area.

So what's code like?

Let's take, for instance, a fixed length structured text file, that handles customer data (you can see the ever-growing list of examples as well).

Here's how our bean should be:

@FixedLengthRecord()
public class Customer {
    @FieldFixedLength(4)
    public Integer custId;

    @FieldAlign(alignMode=AlignMode.Right)
    @FieldFixedLength(20)
    public String name;

    @FieldFixedLength(3)
    public Integer rating;

    @FieldTrim(trimMode=TrimMode.Right)
    @FieldFixedLength(10)
    @FieldConverter(converter = ConverterKind.Date,
        format = "dd-MM-yyyy")
    public Date addedDate;

    @FieldFixedLength(3)
    @FieldOptional
    public String stockSimbol;
}

This could would handle a text file structured like this:

....|....1....|....2....|....3....|....4
1   Antonio Pereira     10012-12-1978ABC
2   Felipe Coury          201-01-2007
3   Anderson Polga       4212-11-2007DEF

And reading that file is as easy as:

  FileHelperEngine<Customer> engine =
      new FileHelperEngine<Customer>(Customer.class);
  List<Customer> customers =
      new ArrayList<Customer>();

  customers = engine.readResource(
      "/samples/customers-fixed.txt");

This way, you can manipulate any properties of the beans contained on the ArrayList collection and eventually, even recreate a file with same format, also as easy as:

FileHelperEngine<Customer> engine =
    new FileHelperEngine<Customer>(Customer.class);
List<Customer> customers = new ArrayList<Customer>();

customers = engine.readResource(
    "/samples/customers-fixed.txt");

// retrieves customer 3 - Anderson Polga
Customer c = customers.get(2);
// changes a couple of properties
c.rating = 82;
c.stockSimbol = "APR";

// and removes first customer - Antonio Pereira
customers.remove(0);

// writes the output file
engine.writeFile("customers-fixed-out.txt", customers);

As you may have already anticipated, the output file will look like this:

....|....1....|....2....|....3....|....4
   2        Felipe Coury  201-01-2007
   3      Anderson Polga 8212-11-2007APR

Examples

Enum conversion

This library has the out-of-the-box ability to export and import values to/from Java enum classes.

Take the following enum as an example:

public enum Enum2 { One, Two, Three }

If we have an input file containing the following lines:

One
one
Two
Three
Three

And a very simple bean like this:

@DelimitedRecord(",")
public class EnumType2 {
    public Enum2 enumValue;
}

It would be natural to write code for loading the file:

public static void main(String[] args) throws IOException {
    FileHelperEngine engine =
        new FileHelperEngine<EnumType2>(EnumType2.class);

    List<EnumType2> res =
        engine.readResource(
            "/test/Good/EnumConverter2.txt");

    System.out.println("Size: " + res.size());

    System.out.println(
        Enum2.One.equals(res.get(0).enumValue));
    System.out.println(
        Enum2.Two.equals(res.get(2).enumValue));
    System.out.println(
        Enum2.Three.equals(res.get(3).enumValue));
    System.out.println(
        Enum2.Three.equals(res.get(4).enumValue));
}

... and that would print out:

Size: 5
true
true
true
true

Master-detail

See how easy it is to handle master-detail formatted files:

ALFKI|Alfreds Futterkiste|Maria Anders|Sales Representative|Obere Str. 57|Berlin|Germany
10248|VINET|5|04071996|01081996|16071996|3|32.38
10249|TOMSP|6|05071996|16081996|10071996|1|11.61
10250|HANAR|4|08071996|05081996|12071996|2|65.83
10251|VICTE|3|08071996|05081996|15071996|1|41.34
ANATR|Ana Trujillo Emparedados y helados|Ana Trujillo|Owner|Avda. de la Constitución 2222|México D.F.|Mexico
10252|SUPRD|4|09071996|06081996|11071996|2|51.3
10253|HANAR|3|10071996|24071996|16071996|2|58.17
10254|CHOPS|5|11071996|08081996|23071996|2|22.98
ANTON|Antonio Moreno Taquería|Antonio Moreno|Owner|Mataderos  2312|México D.F.|Mexico
10257|HILAA|4|16071996|13081996|22071996|3|81.91
10258|ERNSH|1|17071996|14081996|23071996|1|140.51
DUMON|Du monde entier|Janine Labrune|Owner|67, rue des Cinquante Otages|Nantes|France

To indicate what records are to be considered as master as what are to be considered as detail, we use the following code:

engine = new MasterDetailEngine<CustomersVerticalBar,
    OrdersVerticalBar>(CustomersVerticalBar.class, OrdersVerticalBar.class,

        new MasterDetailSelector() {

            @Override
            public RecordAction getRecordAction(String recordString) {
                // if the first char on the record is a letter
                // we'll consider it a master record and
                // if not, we'll consider it a detail record
                if (Character.isLetter(recordString.charAt(0)))
                    return RecordAction.Master;
                else
                    return RecordAction.Detail;
            }

        });

List<MasterDetails<CustomersVerticalBar, OrdersVerticalBar>> res =
    (List<MasterDetails<CustomersVerticalBar, OrdersVerticalBar>>)
        Common.readTest(engine, "Good/MasterDetail1.txt");

And here's how the beans are:

@DelimitedRecord("|")
public class CustomersVerticalBar
    implements ComparableRecord<CustomersVerticalBar> {

    public String customerID;
    public String companyName;
    public String contactName;
    public String contactTitle;
    public String address;
    public String city;
    public String country;

    @Override
    public boolean equalsRecord(CustomersVerticalBar record) {
        if (this.customerID == null) {
            return false;
        }
        return this.customerID.equals(record);
    }

}

@DelimitedRecord("|")
public class OrdersVerticalBar {

    public int orderID;
    public String customerID;
    public int employeeID;
    public Date orderDate;
    public Date requiredDate;
    @FieldNullValue("2005-1-1")
    public Date shippedDate;
    public int shipVia;
    public float freight;

}

jfilehelpers's People

Contributors

fcoury avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jfilehelpers's Issues

Paginação

Olá, você teria um exemplo de paginação usando jfilehelper?

Estou utilizando seu framework e estou lendo um arquivo de mais de 1GB, porém recebo o error abaixo.

The java.lang.OutOfMemoryError: GC overhead limit exceeded

acho que a melhor forma seria usando paginação. Fiz uma paginação aqui mas ta meio gambi.

Leitura de arquivo com multiplos tipos de registros no mesmo arquivo

Preciso ler o arquivo no formato abaixo, estou pensando numa adaptação do jfilehelper para suportar esse tipo de cadastro, alguma ideia?

CMP9TIT0
CMP0TIT000000
CMP1TIT10000000000000000000000001283FANDRÉIA CRISTINA ZANARDI S1501198200000000 CANTORA/AUTORA F00 00000000000
CMP1TIT200000000000000000000000012830124918760500000000000000 0000000011.298.98300 000 00000 000000000000000000 [email protected] 1
CMP1TIT40000000000000000000000001283I PP00000000000ANDREIA ZANARDI S
CMP9TIT0
CMP0OBM000000
CMP1OBM10000000000000000000000000237MANDA TEUS ANJOS SNN000000001607201200000000 000000BR0000000112NNN0000000000000 PTN000 PINRELIGIOSO
CMP1OBM200000000000000000000000002370000000000000000000000001421AGNALDO JOSÉ DOS SANTOS F094489038550000000000000000000000000TD1 A A 037500000000000000000AS PE. AGNALDO JOSE
CMP1OBM200000000000000000000000002370000000000000000000000001428PAULO SERGIO DE SOUZA F137516798710000000000000000000000000TD1 A A 037500000000000000000AS PE. PAULO SERGIO DE SOUZA
CMP1OBM200000000000000000000000002370000000000878000000000000002INSTITUTO ALBERIONE J000000000005378142300015200000000000TD1 E E 025000000000000000000AS PAULINAS-COMEP
CMP9OBM0
CMP0FON000000
CMP1FON1000000000000000000000264000000000000000000000000023700000000000000000000000000000000BRCMP1200057300120123107201216072012N000213000000000000SBR0000000112001000000 INDEFINIDON RELIGIOSO BR000001N
CMP1FON20000000000000000000002640000000000878000000000000002INSTITUTO ALBERIONE J000000000005378142300015200000000000PFPFN4170000000S PAULINAS-COMEP 000000000000000000000000000000000000S
CMP1FON20000000000000000000002640000000000000000000000001421AGNALDO JOSÉ DOS SANTOS F094489038550000000000000000000000000I I N4170000000S PE. AGNALDO JOSE 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000002658APARECIDO DA SILVA NUNES DOS SANTOS F287988478050000000000000000000000000MAMAN0207500000S NENÊ 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000002660VAGNER RENATO GREGO F365198758210000000000000000000000000MAMAN0207500000S VAGNER GREGO 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000001322ANA PAULA RAMALHO F290179778410000000000000000000000000MACVN0207500000S IR.ANA PAULA RAMALHO 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000001283ANDRÉIA CRISTINA ZANARDI F012491876050000000000000000000000000MACVN0207500000S ANDREIA ZANARDI 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000000158DALVA TENORIO DE SOUZA F126151218980000000000000000000000000MACVN0207500000S DALVA TENORIO 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000001131MARCELO DA SILVA DIÓRIO F105606338660000000000000000000000000MACVN0207500000S MARCELO MATTOS 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000002657RENAN ALBERTO DOS SANTOS CARVALHO F346279888000000000000000000000000000MACVN0207500000S RENAN CARVALHO 000000000000000000003012189930121899S
CMP1FON20000000000000000000002640000000000000000000000000365KARLA LÍCIA FIORAVANTE F121144708140000000000000000000000000MAMAN0207500000S KARLA FIORAVANTE 000000000000000000003012189930121899S
CMP1FON300000000000000000000026400000000000000000000000026581393TECLADO ELETRONICO
CMP1FON300000000000000000000026400000000000000000000000026600834VIOLAO BASE
CMP1FON300000000000000000000026400000000000000000000000003651273DIREÇÃO VOCAL
CMP9FON0
CMP0FON000000

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.