Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to read data from a file in reverse order?  RSS feed

 
Pradeep Kumar Raju
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to read data from a file in reverse order???

File.txt

abc
def
ghi

output should be :

ghi
def
abc

& also how to get :

ihg
fed
cba
 
Pradeep Kumar Raju
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Pradeep Kumar Raju
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
everything is fine but how to get rid of null during reading of file i mean i'm getting as

null
it
to naresh
welcome
 
Tony Docherty
Bartender
Posts: 3203
78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
null is returned to denote the end of the stream so you need to only add values to the list that are not null. Alternatively only execute the while loop if the returned value is not null. A couple of standard ways of doing this are :


or


 
Anatoliy Kmetyuk
Greenhorn
Posts: 5
Netbeans IDE Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you can use Scanner:

It's more convenient for parsing all kind of inputs.
 
Pradeep Kumar Raju
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,

2nd logic that you provided worked thanks for the help & also how to read in reverse i.e.,

file.txt:

abc
def
ghi

output should be :

ihg
fed
cba
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pradeep Kumar Raju wrote: 2nd logic that you provided worked thanks for the help & also how to read in reverse i.e.,

If you almost got the first part, I am sure you can do this one too
Consider this: all the lines are in your array now. You need to iterate[hint] over every line in the array and reverse[hint]it before printing
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!