• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading a file line by line and deleting it

 
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I want to read my file line by line and when it hits a certain value from that point it should start deleting the lines until the tag ends.
Just curious will my code actually work at the moment or not because it goes through so many times then goes straight back to the variable declarations at the start of the method and never hits the console print line.


I know it's possible to do I just can't remember how

if <environment id = 'test'>
then delete that line
continue to delete lines
until </environment> is met
but also delete </environment>
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are calling the removeEnvironment method recursively and reopening the files in every call. That means that every call to removeEnvironment will be reading the same part of the file.
You need to create the BufferedReader object before the first call to the method and then pass it in as a parameter.

There may be other problems (for one thing I can't see how you think you are deleting anything from the file), but that is probably a good thing to fix first and then come back with your new code (properly inserted in code tags) and describe any new problem you have.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, you can't edit the contents of a Reader. What you need to do is to write whatever parts should be preserved into a second file under a different name (using a Writer), and then delete the first file and rename the second.
 
Alice Hampton
Ranch Hand
Posts: 217
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Joanne thank you I will try it - both my methods after where the method call takes place use buffered reader so should I just remove the creation of them from both methods and create it somewhere before the method call takes place?
@Ulf that sounds pretty tricky to do - do you have any recommended sources or should I just google it?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not that tricky - the File class has methods to delete and rename files.

Using a Writer is pretty analogous to using a Reader; searching for some phrase like "java file write example" should find plenty of examples.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's all in the Java Tutorials. This section. Try the bit about scanning and formatting; a Scanner and a Formatter together might be the easiest to use. Remember to close the Formatter in a finally. If you don't two baleful things might happen:-
  • 1: You never close the file handle and suffer a resource leak.
  • 2: You never flush the stream and all your writing stays in the buffer for ever and not in the file.
  • You can read about the File class in the section called “legacy file i/o” and for once I am not joking about it

    Apart from that, Ulf has given you the solution.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Do you mean to delete the line or to delete part of the line? For deleting part of the line try String#substring or StringBuilder.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic