Use this void to delete any String from any file.
Code:
public void removeString(File fileArg, String stringArg)
{
try
{
File target = new File(fileArg);
Scanner reader = new Scanner(target);
int count = 0;
String a = "";
ArrayList<String> fileInfo = new ArrayList<String>();
while(reader.hasNextLine())
{
a = reader.nextLine();
if(!a.startsWith(stringArg))
fileInfo.add(a);
}
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(fileArg)));
for(int i = 0; i < fileInfo.size(); i++)
{
writer.write(fileInfo.get(i));
writer.newLine();
}
writer.flush();
} catch(Exception e) {
System.out.println("An error? What the **** did you do!?");
} finally {
//
}
}
The way this is used.
Type:
Code:
removeString(new File(****PUT YOUR FILE PATH HERE****, ****PUT THE STRING YOU WOULD LIKE TO REMOVE HERE****));
This is a very specific string finder.
Example of what it will remove:
Example file Path -- (C:/names.txt)
Code:
ben
amorie
amy
Lsia
karoe
removeString(new File("C:\names.txt"), "L");
The output of this would be:
Code:
ben
amorie
amy
karoe
It deletes whole lines. No matter what.
But lets say I did this instead ( Same file )
removeString(new File("C:\names.txt"), "en");
Nothing gets removed. It's checking for what the line starts with. Like if you wanted to delete an item in charecters inventory... Do this.
removeString(new File("C:\caharacters\buzzywuzzy.txt"), "player-item = 5234");
It would remove that line, thus ridding the play of that item.
Enjoy.