This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Edit specific line in a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Edit specific line in a file" Watch "Edit specific line in a file" New topic
Author

Edit specific line in a file

Anne Buena
Greenhorn

Joined: Mar 11, 2012
Posts: 14

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

Joined: Mar 11, 2012
Posts: 14

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 13, 2005
Posts: 37884
    
  22
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

Joined: Mar 17, 2011
Posts: 7484
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Edit specific line in a file
 
Similar Threads
Reading File Problem
jframe not accepting sql query..
Updating File
problem with actionEvent command