LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-15-2009, 11:53 AM   #1
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Rep: Reputation: 16
[Java] How to append something to each line in a text file ?


Hello everyone,

I am thinking of appending something to each line in a text file with Java. I prefer not write a new file with content appended from the old one.

That 'something' would probably be Time Stamp when the file is created (which is same for each line).

I am not sure Java provide some easy way for it or not.

Appreciate any idea from you guys,


Thanks in advance,

-Kun
 
Old 04-15-2009, 12:19 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 85
I don't think the standard Java library provides anything ready-built to do this (though I did a quick search and it looks like a few people have had a go at implementing sed in Java). In fact, sed -i does create a new temporary file, outputs the modifications to it, then overwrites the original. You can see this if you attempt to use it in a folder in which you do not have write permissions:

Code:
rob:secdir$ sed -i "s/$/ $date/" file
sed: couldn't open temporary file ./sedXlilKP: Permission denied
... In other words,
Code:
sed -i "s/$/ $date/" file
is equivalent to:
Code:
sed "s/$/ $date/" file > tmp_file && mv tmp_file file
You could, of course, read the contents of the file into an ArrayList, append the timestamp to each element of this, and then truncate the file and write the data back again (so long as it was not too large to hold efficiently in memory).

Last edited by Robhogg; 04-15-2009 at 02:22 PM.
 
Old 04-15-2009, 02:38 PM   #3
Kunsheng
Member
 
Registered: Mar 2009
Posts: 82

Original Poster
Rep: Reputation: 16
Thanks, Rob! I think I will take this instead of Javaing.


Quote:
Originally Posted by Robhogg View Post
I don't think the standard Java library provides anything ready-built to do this (though I did a quick search and it looks like a few people have had a go at implementing sed in Java). In fact, sed -i does create a new temporary file, outputs the modifications to it, then overwrites the original. You can see this if you attempt to use it in a folder in which you do not have write permissions:

Code:
rob:secdir$ sed -i "s/$/ $date/" file
sed: couldn't open temporary file ./sedXlilKP: Permission denied
... In other words,
Code:
sed -i "s/$/ $date/" file
is equivalent to:
Code:
sed "s/$/ $date/" file > tmp_file && mv tmp_file file
You could, of course, read the contents of the file into an ArrayList, append the timestamp to each element of this, and then truncate the file and write the data back again (so long as it was not too large to hold efficiently in memory).
 
Old 04-15-2009, 09:08 PM   #4
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
Well, most if not all of that can be done if you learn java regular expressions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
append a line to text file in script roadrash Linux - Newbie 4 10-01-2008 09:03 AM
read line by line form text file in java. spank Programming 1 10-18-2006 02:46 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 09:28 AM


All times are GMT -5. The time now is 03:56 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration