Reputation: 4860
Please I would like your help with the following issue:
This is a sample code customized solely for clarifying my question:
File accounts_File = new File("Sample_Folder\\example.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String str_String = ""; //For storing the input from the file.
fis = new FileInputStream(accounts_File);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0)
{
str_String = dis.readLine(); //Reading the line from the file.
StringTokenizer st = new StringTokenizer(str_String);
while (st.hasMoreTokens())
{
//I need some help here
}
}
fis.close();
bis.close();
dis.close();
The Input File (example.txt) looks as follows: James>Hector>AUS>25
The Objective is simply scanning and replacing "Hector" with "Anderson". The problem is that the name "Hector" is not known; the only thing known is its location (after the first ">").
Note: I can change the format of the input file if that makes it easier. As an example I can change the format to something like:
FirstName: James
LastName: Hector
Country: AUS
Age: 25
*Where the program now will scan for the keyword "LastName:" and replace everything following it with "Anderson".
Thanks in advance for your help!
Any Suggestions are greatly appreciated.
Upvotes: 0
Views: 3888
Reputation: 114767
Keep the format, it looks like a standard csv file (comma separated values, where the "comma" is the ">" char) and csv files are easy to read and write!
As you can't replace file content "on the disk", you'll have to read the file into memory, rewrite it in memory with the changes needed and rewrite it to the disk.
A simple model for the file is a two dimensional string array, each line of the file is a row, each value a column, which makes 4 columns. The last name (the string, you want to replace) now is always at matrix[lineNumber][1]
. Change the value on the second column for each row and your done.
For production code, I'd code a class to represent a record. Then, your code could look like this:
public void replace(File inputFile, String newName) {
List<Person> persons = new ArrayList<Person>();
// read the file
List<String> lines = readFile(inputFile);
// create a person instance for each line
for (String line:lines) {
String[] values = line.split(">");
persons.add(new Person(values));
}
// change the last name
for (Person person:persons) {
person.setLastName(newName);
}
// store all persons to file
writeFile(inputFile, persons);
}
Note, that the actual work (file IO, changing names) is done in different methods and classes. This keeps the main algorithm inside the replace method readable.
Upvotes: 3
Reputation: 18061
I'd suggest using a regex to replace the match between the first and the second ">"
. I confess I'm neither an expert in Java nor an expert with regular expressions so I can't provide "the codes" right away.
Your question suggests that you could structure the source file to your desire - so why not make that an Xml document? I that case of course you wouldn't do text parsing.
Upvotes: 2
Reputation: 954
Try the following...
File accounts_File = new File("Sample_Folder\\example.txt");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
String str_String = ""; //For storing the input from the file.
fis = new FileInputStream(accounts_File);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0)
{
str_String = dis.readLine(); //Reading the line from the file.
final int firstIndex = str_String.indexOf('>');
if(firstIndex != -1 && firstIndex != str_String.length() - 1) {
final int secondIndex = str_String.indexOf('>', firstIndex + 1);
if(secondIndex != -1) {
final StringBuilder builder = new StringBuilder();
builder.append(str_String.substring(0, firstIndex + 1));
builder.append("Anderson");
builder.append(str_String.substring(secondIndex));
System.out.println(builder.toString());
}
}
}
dis.close();
Upvotes: 1