aspose file tools*
The moose likes I/O and Streams and the fly likes stream output variables to specific positions in output file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "stream output variables to specific positions in output file" Watch "stream output variables to specific positions in output file" New topic
Author

stream output variables to specific positions in output file

Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Hello,

In a proprietary db editor I put in the following:



which allowed me to line up a variable to a position in the file. I cannot find any such similar Java example. I have ended up "padding" the variables with spaces to mimic the above and what was around 170KB has blossomed into a 1MB file! [So has to be wrong]

I read in from a comma separated file using CSVParser as there are commas within the quotes to a multi-dim array.

Have my outer & inner for loop setup and want to do the Java equivalent. I haven't found any examples like the above code snippet.

Please advise.

Thanks
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Welcome to the JavaRanch.
Java supports formatted output. Have a look at java.util.Formatter for the details.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Thank you for the pointer.

I have



1. I have to put in 2 spaces to "line up" with the particular column rather than saying start values[i][3] at column 31.
2. The file (with 25% done) is now three times the size of the original.

I have made some progress but still not really what I am after....

Thanks
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

It would be a bit more efficient, and arguably more readable, to write:However, that doesn't do anything about the output being different. To help you to get the correct format string, I think we'd need to see some (short) examples of actual data, and code that compiles.
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Sample data: There is an "ORD" header Line and then an "ITM" line.

FDP,ORD,182426,"0370","School Corp 1","0409","School 1","9575 N Anywhere Street","","Hope","IN","47246","Joe","Schmow","(812) 123-1234",01/14/2008,11/10/2008,"Delivery",20,11/6/2008,|
FDP,ITM,182426,7,"B027","Cheddar, Redu Fat Shred Y",3,|
FDP,ITM,182426,8,"B430","Macaroni, Elbow",2,|



The other system so you can get some idea of the column layout (for ORD) is:



Thank you,
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

That should produce the same output, although it's much less efficient than it could be. However, it looks like there's a problem with your loop on j. With this inner loop, you're formatting each input line several times, identically; 20 times for ORD lines, and nine times for ITM lines. You don't need to loop over i or j, since you only parse one line at a time (i), and you format all the fields at once (j).

Take a step back, and look at your overall design. If you create a new instance of CSVParser from the BufferedReader, and use its getLine() method, it should make more sense because it returns String[]. You can also get better performance by crating a new Formatter instance from the BufferedWriter and using its format() method. Otherwise, CSVParser.parse() creates a new CSVParser for each line, and String.format() creates a new Formatter for each field.

Let me know whether this makes any sense at all.
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Thanks Carey for the input - you are right with respect to the multiple formats etc. I could see that with my console output.

I was more concerned with the answer to placing data at a particular point.

What I now have is:



The String [] value = parse.getLine" is wrong as the while reads a line and then this code reads the next line... I can't seem to get away from having to assign a String [] to the parse.getLine(). Please advise.

Thanks,
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

You can nest an assignment in the while statement. There's an example in the second code sample here (don't dwell on the content of that link too much, it doesn't apply to your case).
[ November 11, 2008: Message edited by: Joe Ess ]
Peter Brown
Greenhorn

Joined: Sep 26, 2008
Posts: 22
Thank you for your help.

Here is the relevant code that now matches the output (and file size) to the required format I was after. [for future reference].

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: stream output variables to specific positions in output file