File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Edit specific line in a file

 
Anne Buena
Greenhorn
Posts: 14
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi! Can anyone check my codes? I'm having a hard time solving this one out. What I want to happen, is to read a file, edit a specific line and overwrite all of them in the same file. Is this possible?

Btw, here is my code:



And the file is in the attachments. Thank you for your time
 
Anne Buena
Greenhorn
Posts: 14
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't attach the file but it is a txt file that contains:


1012012*Anne Buena*17*11-20-1957*Filipino*09163889468*33-7913734-2*19-051147559-0*8-4568-3*
1022012*Ralph Osuna*17*11-20-1936*Filipino*09163889468*78-8569352-4*18-546231456-0*7-8752-8*
1032012*Jelly Lagumen*17*11-20-1952*Filipino*09163889468*92-1254698-3*74-253145698-0*5-8423-7*
1042012*Emmanuel Aishiteru*17*11-48-1993*Filipino*09163889468*41-7541963-7*87-124154632-0*6-7852-9*
1052012*Jake Toledo*17*11-20-1932*Filipino*09163889468*58-3564256-8*65-412563458-0*3-2546-3*
1062012*Cedie Domingo*17*11-20-1939*Filipino*09163889468*36-9632451-2*73-256423589-0*5-9635-6*
1072012*Francis Antolin*17*11-20-1991*Filipino*09163889468*43-4562134-7*14-356982469-0*1-2136-2*
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's way too much code. Since you're asking about manipulating a file, you should provide an SSCCE that shows only the code that's modifying the file. It doesn't even have to be the real file. Just code that turns file

into


would be fine.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I will say, however, that this is bad:


Unless you have a very good reason to do so--like you're receiving data from or sending it to some other system that expects it in that format and you can't change it--the "parallel arrays" approach ignores one of the main features of OO programming. You should define a class, maybe called Employee, if that's what you're dealing with here, that has empnum, name, age, etc., and create an array of those Objects.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the bits I have left are also bad.
addActionListener(this) is non-object-oriented programming, and the large block of if-elses is difficult to maintain.
Empty catch blocks are dangerous.
You probably don’t need those many fields; they can probably all be local variables in the constructor or initGUI method.
Anne Buena wrote: . . . . . .
You are going about this exercise quite in the wrong way. You should not even think of creating a GUI until you have got the logic working at the command line/a terminal. You should have the writing and reading done in one class, and the display in a different class. You need to make the GUI match the logic, whereas you would appear to be trying to squeeze the logic into the shape of the GUI.
 
Winston Gutkowski
Bartender
Pie
Posts: 9486
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You are going about this exercise quite in the wrong way. You should not even think of creating a GUI until you have got the logic working at the command line/a terminal.

Amen.

@Anne: What Campbell said is part of a larger paradigm called "division of responsibility", and usually involves understanding the problem.

There's absolutely no point in trying to write a program for, for example, a game, unless you understand the game itself.
The corollary to that is that you're usually much better off writing a "mill" (old computer term; but might be worth looking up) that actually does what you want before you write anything else. You can always tack on the fancy GUI stuff afterwards (if you designed it properly ).

Winston
 
Rob Spoor
Sheriff
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anne, I can clearly remember having told you twice to use a temporary file instead of writing to the file in place. RandomAccessFile is for binary files only, and replacing content using it should only be done if you need to replace one piece of binary data with another piece of binary data of exactly the same size.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic