Forums Register Login

Split a File According to a String in the File

+Pie Number of slices to send: Send
HI Guys I have a File that contains Several Customer Information Separated by A line. I need to separate the File So that Each Customer Has a Different File. The Problem with the code I am using is that is creates One file for the First Customer and does Not Create another File for The other Customers.
The file is in this format:


I Need Separate Files for each Customer. I need To set it such that every time the Buffer Reader Encounters a line, it writes A different file for that Customer.



Here is the code am using:

+Pie Number of slices to send: Send
You break out of your while loop as soon as you encounter the first divider line.
Instead you should close your current output stream and create a new one at that point.

Also you probably don't need the call to flush(). It sort of defeats the purpose of a BufferedWriter if you force it to write the buffer contents every time round the loop.
+Pie Number of slices to send: Send
... and a continue to ignore the divider line and read the next customer details.
+Pie Number of slices to send: Send
 

John Jai wrote:... and a continue to ignore the divider line and read the next customer details.


I'd go with an else block, but each to his own.
+Pie Number of slices to send: Send
Am getting a write error After Doing that.
This is the coed I am using:



Please Tell me where the Mistake is
+Pie Number of slices to send: Send
 

Joanne Neal wrote:Instead you should close your current output stream and create a new one at that point.


You need to do all this in the if block
Something like this
+Pie Number of slices to send: Send
I got one file with May squares inside: This is the Modified Code:



+Pie Number of slices to send: Send
Don't create new variables for your output streams on lines 41 and 42 - use the ones you declared before your loop (fos and bw).
Open the new output stream in the if block after the close, not in the else block.
+Pie Number of slices to send: Send
Guys I realy Appreciate your help but am still getting a write Error Is there any other method I can use to separate the Customer details and store them in different file something like funtion?
+Pie Number of slices to send: Send
Here Is the code am trying out for Split function But each Line of the Text File is getting written to a different file ending up with thousands of file:

+Pie Number of slices to send: Send
 

Stanley Mungai wrote:Guys I realy Appreciate your help but am still getting a write Error Is there any other method I can use to separate the Customer details and store them in different file something like funtion?


Not really, because your delimiter is a line, so you need to split your file based on line content.

Stepping back a moment: Are you really sure you want to do this?
It smacks to me very much of using the filesystem as a database, and your admins may not like it.

Unless this is a requirement for a 3rd party over which you have no control, I'd definitely advise against it. And even if it is, you might want to ask them if this is really the best way to proceed.

Winston
+Pie Number of slices to send: Send
Winston, I have No choice over this. I have to split that file one way or the other as each contains an Email Address and Each should be sent to the Respective Customer. When it is this way, My sending program will send the Entire file to one or all customer while this is confidential information. Unless You can tell me of a way to send each Customer his details without Splitting the File, I really want to do this.
+Pie Number of slices to send: Send
You're creating a new BufferedWriter in every iteration of your for loop. You only need to create a new BufferedWriter when you find a divider line - although you seem to have taken out the check for this now. Why ?
Look at the pseudo code I gave you in an earlier post. Just randomly rearranging your code is not going to fix the problem. You need to think about what you are doing.
+Pie Number of slices to send: Send
I tried the Pseudocode and I got A write error. That was why I was trying out new ways of Splitting the File. I do not care the Method I just want to Separate those Records. I modified the Code according to the Pseudocode to this And I still Got the Write error:

+Pie Number of slices to send: Send
 

Stanley Mungai wrote:Winston, I have No choice over this. I have to split that file one way or the other as each contains an Email Address and Each should be sent to the Respective Customer. When it is this way, My sending program will send the Entire file to one or all customer while this is confidential information. Unless You can tell me of a way to send each Customer his details without Splitting the File, I really want to do this.


I'm not saying don't split the file; I'm just saying don't create a whole bunch of other files. There would be nothing to stop you, for example, splitting it into an array of Strings (or streams), and then passing those off to a send module.

If I was an admin, I'd be very leery of any design that asks my system to handle the creation of potentially hundreds or thousands of (probably temporary) files, simply because the designer couldn't think of anything better.

Winston
+Pie Number of slices to send: Send
That is not a Problem Winston as they Get deleted as soon as they are sent and a Log file of the records sent is the only one that remains of the Events. am really new to Java And I need a Way out of this.
+Pie Number of slices to send: Send
 

Stanley Mungai wrote:I tried the Pseudocode and I got A write error.


And that's because you ignored what I told you in my next post. You are assigning your new FileOutputStream to a different variable (fos1) but you are still using the old variable (bw) to call the write method. But you've closed that stream so obviously you will get an error.
Assign the new FileOutputStream to fos, create a new BufferedWriter using fos and assign this to bw.
Also on line 37 you should use bw.close() not fos.close(). This will close both the BufferedWriter and the FileOutputStream.
+Pie Number of slices to send: Send
 

Assign the new FileOutputStream to fos, create a new BufferedWriter using fos and assign this to bw.



I Seriously do not understand this or how it is done. I am very new to Java. Every time I try to do this I get an Error that fos is already defined in main.

+Pie Number of slices to send: Send
 

Stanley Mungai wrote:Every time I try to do this I get an Error that fos is already defined in main.


I'm guessing that is because you are redeclaring it as well as reassigning it. You only need to declare a variable once. You can then assign values to it as many times as you want
For example

On line 1, y is declared to be an int and assigned the value 2
On line 4 we don't need to say what type y is, the compiler already knows. We can just assign the value 5 to it.
Do you see the difference ?
+Pie Number of slices to send: Send
Is there really a way of what am trying to do or should I explore another way of doing this. I just do not seem to be getting anywhere. Thank all the same for those who tried to help.
+Pie Number of slices to send: Send
They say a picture paints a thousand words. So as my words are apparently not working...

Now do you see what I was trying to explain ?
+Pie Number of slices to send: Send
Joanne, You are Simply the Best, Got it, I got It This time I got . You are right , A picture Paints a thousand Words. Thank you So much I almost gave up on this one. Once More, Thank you.
+Pie Number of slices to send: Send
 

Stanley Mungai wrote:Joanne, You are Simply the Best, Got it, I got It This time I got . You are right , A picture Paints a thousand Words. Thank you So much I almost gave up on this one. Once More, Thank you.


I will say again (assuming you are listening): you do not necessarily need to create a FileOutputStream; you could simply send a basic Outputstream to wherever you need to send your data.

Your decision appears to have been to create files; and all I can say is that I hope you can defend it:
If this is a personal project, fine; otherwise you have given your admins a pile of files that they will now have to deal with, and that have nothing to do with them; and I also suspect that you will not have either (a) asked, or (b) notified them.

Can you imagine what a nightmare it would be to administer a system where every developer thought the way you do?

Winston (old admin)
+Pie Number of slices to send: Send
Winston, Oh am sorry, I thought from one dimension. But like I said, the files are deleted a Minute after that. I wish I could have found a better way to do that, but with the agency these So Called Admins and the Pressure they put, This is what I could have given them at this time. I am really new to Java, Please Understand me.
+Pie Number of slices to send: Send
 

Stanley Mungai wrote:Winston, Oh am sorry, I thought from one dimension. But like I said, the files are deleted a Minute after that. I wish I could have found a better way to do that, but with the agency these So Called Admins and the Pressure they put, This is what I could have given them at this time. I am really new to Java, Please Understand me.


No probs. I just wanted to let you know the impact of such a decision. I used to be an administrator, you see.

Also: creating and deleting lots of files is
(a) Very slow.
(b) Can still put strain on the filesystem, particularly if it is a FAT-style one, because it will fragment it. With ufs it's not quite so bad, but the design still sounds to me like you're using the fs like a database.

Winston
+Pie Number of slices to send: Send
Now when the Pressure is minimal as they have at least something working, I will have the time to explore other Fronts. I really appreciate you and the entire forum for the Guidance.
2
+Pie Number of slices to send: Send
 

Stanley Mungai wrote:Now when the Pressure is minimal as they have at least something working, I will have the time to explore other Fronts.


Hmmm. Seems there's always enough time to do something wrong, but never enough to get it right.

I suspect very strongly that your solution will become a shining example of the "if it ain't broke, don't fix it" mentality, and lumber your admins for many moons to come.

<Grumble, Grumble>

Winston
+Pie Number of slices to send: Send
Am not trying to give something that is not working to the admins Deliberately. These Records are supposed to be sent to their respective customers as an Attachment. I viewed that having them in files form first before attaching them would be more effective and something that I understand with my limited java Experience. Trust me Winston I have all over the net trying to get A solutions. If my Admins were patient with me, I guess I would have given them something better to work with. I Just do not know how to send these things as an attachments without having them as files first. Trust me if I had, I would have gone for that option smiling "JAVA" from ear to ear.
+Pie Number of slices to send: Send
 

Stanley Mungai wrote:Trust me Winston I have all over the net trying to get A solutions. If my Admins were patient with me, I guess I would have given them something better to work with. I Just do not know how to send these things as an attachments without having them as files first. Trust me if I had, I would have gone for that option smiling "JAVA" from ear to ear.


Well, just one possibility (as I suggested before) would be to split the file into Strings and then send those directly to a mail program (I've never done it myself, but there must be many Java mail clients around). Since they're attachments, it's quite possible that it would end up creating temp files anyway, but it wouldn't be your doing; and many OS's (even Windows) have areas in the filesystem specifically designed for temporary files.

We used to call them SPAs - Scratch Pad Areas - but I don't even see the term in Wikipedia now. Shows how old I am.

Winston
+Pie Number of slices to send: Send
You are not old yet, You know you are getting Old When the candles cost more than the cake (It's my birthday today by the way). But honestly sir, I would not know how to

split the file into Strings and then send those directly to a mail program

Because I had tried to store the Splits into different arrays and use the arrays instead of the files, But I did not find an attachment to send and mind you, these files are very big and My Admins and customers want them as PDF Attachments. So that says that am Converting them to PDF first before Sending them. I hope you can now see Why I needed files. For conversion to PDF and for sending as attachments. Am using I-text for conversion.
+Pie Number of slices to send: Send
 

Stanley Mungai wrote:I hope you can now see Why I needed files. For conversion to PDF and for sending as attachments. Am using I-text for conversion.


Yes, I can see why you might think you need files (and it is possible that you do), but, for example, does I-text not give you the option of outputting to a Stream or a byte array instead? That way you might be able to send the array, or even a pipe the output directly, to a mail client.

Winston

[Edit] Oh, and Happy Birthday.
+Pie Number of slices to send: Send
Ok, I give in sir, I will do my assignment better next time when I do not have the admins after my poor soul. I had already created the Program to send the files. I had just not foreseen a combined report and that was probably why I would not have the time to overhaul the entire Program. Come on , Am running out of excuses!
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 12023 times.
Similar Threads
Need help with using Greek letters read in from file
IO package
Code generation help
please help with text formating in java
problem with writing file , please help
Split a Text File Following a Criteria.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 22:01:47.