vendredi 29 mai 2015

Create a text file table with java with pattern

here is the result I need to get:

+ ------------- + ----------- +
|    field1     |   field2    |
+ ------------- + ----------- +
|    f1val1     |   f2val1    |
+ ------------- + ----------- +
|    f1val2     |   f2val2    |
+ ------------- + ----------- +

and so on. As you can see the values must be centered. So I need to get a pattern like (colwidth - string.length ) / 2 for blanks each the right and the left side.

The size of each filed is set by 2 variables. Each one saying how many - signs there are to be set and therefore stating the padding of the table.

The values are in 2 arrays arr1(vala,valb,valc,vald) and arr2(val1,val2,val3,val4)

The first value is the header value.

It all needs to be written to a text file.

To be honest I have researched a lot and have not idea on how to get this going. I got the basics sort of set but the pattern is still missing completely.

File dir = new File("C:\\testdir");
dir.mkdirs();
File outfile = new File(dir, "output.txt");
FileWriter writer = new FileWriter(outfile);
int col1 = 10;
int col2 = 15;
String[] arr1 = {"vala","valb","valc","vald"};
String[] arr2 = {"val1","val2","val3","val4"};
for (int i = 0; i < arr1.length; i++) {
'this needs to go into the file'
System.out.print(intArray[i]); if(i != intArray.length - 1) 
System.out.print(", ");
};

Any idea on how to get this done? Help would be greatly appreciated.

The main problem is how to get the table with the pattern.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire