Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Appending specific lines in a *.txt file

 
Jamie Craven
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone
I have a file called "info.txt" within this file is the following information:
Albert
Einstein
Bill
Gates
George
Bush
As you can see its the firstname & surname of three famous people, now I have used the FileReader & BufferedReader as a means of reading this information in line by line please see this code below:
//=================================
import java.io.*;
class Readingfromfile
{
static String[] line = new String[6];
public static void main(String[] args)
{
try
{
FileReader file = new FileReader("info.txt");
BufferedReader buff = new BufferedReader(file);
for (int row=0;row<line.length;row++)
{
line[row] = buff.readLine();
System.out.println(line[row]);
}
buff.close();
}
catch (IOException e){
System.out.println("Error" + e.toString());
}
//=================================
the next thing I wanted to learn is to be able to write additional information to the "info.txt" file, see below code for what I use:
//=================================
try {
BufferedWriter out = new BufferedWriter(new FileWriter("info.txt", true));
out.write("aString" + " " + "hiya"); //This is what gets written to the file
out.close();
}
catch (IOException e) {
}
}
}
//=================================
But the problem that I'm having is for example say person two in my "info.txt" file who is "Bill" & "Gates" what I might want to do is change Bill's surname to "testsurname"
but I dont know how I get to bill's surname so that I can change it.
please help me..
Thankyou all
 
Joe Ess
Bartender
Posts: 9340
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To change data in a file you either need to:
1. read out of info.txt and write to info2.txt handling the replacement of the data along the way. Then delete info.txt and rename info2.txt.
2. use java.io.RandomAccessFile in read/write mode. You can scan through the file using readLine() to find the position of the data you are going to replace. You will either:
1. overwrite , reposition data, trim file
2. lengthen file, reposition data, write
depending on the length of the data you are inserting.
The Java Tutorial has a chapter on IO. It's worth the read since Java IO streams can get confusing.
 
Jamie Craven
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the actual length and layout of my "info.txt" file.
Its information stands for this:
Firstname
Surname
Age
Weightloss
Cycle
Tread
Steps
Skis
Weights
Then this information repeats again but for a different person so parts of the information will be different.
//=========================================================
**********ACTUAL CONTENTS OF TEXT FILE SEEN BELOW**********
//=========================================================
Dave
Jones
43
No
Yes
Yes
No
No
Yes
Yes
Valerie
Mendham
29
Yes
No
Yes
Yes
Yes
No
No
Tracey
Smith
38
No
Yes
No
No
No
No
Yes
Trevor
Williams
22
No
Yes
Yes
Yes
No
No
Yes
Scott
Henley
18
Yes
Yes
No
Yes
No
Yes
Yes
George
Strutt
37
No
Yes
No
Yes
No
Yes
No
Mary
Briggs
42
Yes
No
Yes
No
Yes
No
Yes
Julie
Bartham
56
No
Yes
No
Yes
Yes
No
Yes
Mark
Allsop
24
No
Yes
Yes
Yes
No
No
Yes
Wendy
Scott
33
Yes
Yes
No
Yes
No
No
Yes
Andrew
Wise
52
No
Yes
Yes
No
No
No
Yes
Carol
Wood
27
Yes
No
Yes
No
Yes
No
No
Marie
Smith
44
No
Yes
No
Yes
No
Yes
Yes
Colin
Knight
49
No
Yes
Yes
Yes
No
No
Yes
Andrew
Fisher
50
Yes
Yes
No
No
No
Yes
Yes
Carl
Smith
37
No
Yes
Yes
Yes
No
Yes
No
Frank
Southall
36
Yes
No
Yes
No
Yes
No
Yes
Ken
Green
31
No
Yes
No
Yes
Yes
No
Yes
Katherine
Corn
24
No
Yes
Yes
Yes
No
Yes
Yes
David
Lowe
38
Yes
Yes
No
Yes
No
Yes
Yes
Stephen
Leggett
32
No
Yes
No
Yes
Yes
Yes
No
Timothy
Quibble
36
Yes
No
Yes
No
Yes
No
Yes
Matthew
Clark
31
No
Yes
No
Yes
Yes
No
Yes
Barabara
Reynolds
24
No
Yes
Yes
Yes
No
Yes
Yes
Sally
Cottingham
38
Yes
Yes
No
Yes
No
Yes
Yes
 
Jamie Craven
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm my tutor said there is a much simpler way of finding a pice of information and replacing it with something else rather than using the readline() command & she also doesnt want me to copy the file and rename it to another name to make the changes and putting back that original file name either
I'm confused, I cant see anyother way of actually updating information in the txt file I've been given.
I guess I'll have to wait, lol I mean this is an assignment thats not due until April 30th 2004, its just I have finished my Janurary assignment and wanted to make a head start on this one, always best to be ahead of yourself workwise...
guess I'll relax over xmas and just have a few instead
 
Joe Ess
Bartender
Posts: 9340
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jamie Craven:

I'm confused, I cant see anyother way of actually updating information in the txt file I've been given.

I'm intrigued. You could read the whole file into memory, do the replacement, then write the file back out, but that would be a Bad Idea except in the most trivial programs. . .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic