aspose file tools*
The moose likes Beginning Java and the fly likes Read Content Between Pipes Delimiter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Read Content Between Pipes Delimiter" Watch "Read Content Between Pipes Delimiter" New topic
Author

Read Content Between Pipes Delimiter

Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

HI Guys I am Writing A code That Reads a text file with fields delimited By a Pipe Character Which I am Able to do. The code Reads the Field Number and The Size. I need To Be able T0 Print the Content of each field Too. Here Is the code I am Using.





Currently Ny output is in the Form:



The Original File is in the Form




I need the output in the form:



Give a beggar a fish; feed him for a day. Teach him how to fish; Feed him for a lifetime.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6



should be



because '|' is a special character in regex and needs to be escaped if you want to use it literally.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Hello Stanley Mungai,

You are almost there

Hint : You are printing length of a String. You also want to print actual String


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Thans Jeff For Noticing That I already Have that in the Original Code. That was a Typing error Otherwise the Code would not have Such Output. That Is there In the Original Code.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Anayonkar Shivalkar: That is what I do not Know whow to do it. I need Help In That.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7653
    
  19

Jeff Verdegan wrote:...should be

Or alternatively:
line.split("[|]");
Personally, I hate all those darn backslashes.

Winston
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Thank you Winston for that new Idea. I need to Know how to Print out the Content Between The Pipes.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Stanley Mungai wrote:Thank you Winston for that new Idea. I need to Know how to Print out the Content Between The Pipes.


So, you wrote that code yourself, did you?

And you wrote this line?


And you understand how that line works?

And now you're saying you don't know how to append the String itself to that line, even though you've already figured out how to append that String's length? Really? Are you sure about that?

I think either you didn't write the code, or else you're just thinking too hard about it and making it more complicated than it is.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1505
    
    5

Stanley Mungai wrote:Anayonkar Shivalkar: That is what I do not Know whow to do it. I need Help In That.

You mean to say that you know how to print String's length, but not how to print String itself?
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Thank you Guys for helping me figure that Out:



Got it
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Cool. Glad you figured it out.

Was it a case of just staring at it too long to where your mind got fuzzy and you couldn't see what was staring you in the face? We've all been there.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Jeff: Haha, Guess So, Am so Surprised I could Not see that after Writing So many Codes Like these. I guess I am Tired. I am Going to have some Sleep. Thanks Guys.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
There is a lot to comment, apart from your still using "|" when you should use "\\|". You only managed to get anything because you are probably using the wrong reader. But two errors which cancel each other out do not cease to be errors. Look at the Java Tutorial about regular expressions and the documentation for String#split(java.lang.String) which takes a regiular expression and look at the documentation for FileInputStream which recommends you consider FileReader for text files. Also look in the Java Tutorials about I/O (try where it says Buffered Streams). That should give examples of reading and writing to files.

Why are you creating a new folder just one level from the root of your C drive? Shouldn’t you have that folder in MyDocuments or similar?
Why are you deleting files in line 25? In fact, what do line 22-26 mean at all?
Why are you using a FileInputStream? If you are reading a text file, you are better off using a FileReader.
Why have you got the loop which you never enter in lines 33-35?
Why are you only reading one line from each file? Are you certain they only contain one line each? Shouldn’t you be reading lines in a loop?
Don’t call flush(), least of all before your last writing instruction. You don’t need it. But you most certainly need to close your readers and writers when you finish each file.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

Ritchie:
1. We are all not Java Geeks Like Probably You are.
2. Yes the File many Lines But am Only interested In the First Line.
3. I am deleting Files Because there were Other Files Named "File" in that Drive Created By other Processes
4. I decided to Create the Folder In Drive C (I Think That Choice is Mine)
5. This is the Reason I should Always remember To Post Only the Relevant Part of the Code
6. This Issue was resolved More than 24 Hours Ago and am Now Only Interested In the beginning Position Of the Field.
7. You removed my Other Link That I was Following Up with the Position.
8. Unless you are Helping Concerning Position, I suggest that the Above Points do not help At all.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Stanley Mungai wrote:Ritchie:
1. We are all not Java Geeks Like Probably You are.
What’s wrong with being a geek?
2. Yes the File many Lines But am Only interested In the First Line.
Thank you for explaining that, which was not clear earlier.
3. I am deleting Files Because there were Other Files Named "File" in that Drive Created By other Processes
4. I decided to Create the Folder In Drive C (I Think That Choice is Mine)
As long as you are using your own computer and don’t mind losing those files. If you are putting code onto somebody else’s computer they might want to keep those files.
5. This is the Reason I should Always remember To Post Only the Relevant Part of the Code
6. This Issue was resolved More than 24 Hours Ago
But there is still a serious issue with flush and close, which remains in the code you posted today
and am Now Only Interested In the beginning Position Of the Field.
7. You removed my Other Link That I was Following Up with the Position.
Sorry, but after your request I restored it.
8. Unless you are Helping Concerning Position, I suggest that the Above Points do not help At all.
One reason for caring about the quality of your code and programming is that I might find myself using it. If anybody is producing code for me to use, I want it to be of the best quality possible, so it will be good for me to use.
Also, many people here are learning to write code to pass exams, The better the quality of the code, the better marks they will receive.
Stanley Mungai
Ranch Hand

Joined: Dec 09, 2011
Posts: 155

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Read Content Between Pipes Delimiter