Results 1 to 13 of 13
- 06-29-2011, 04:18 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Searching Text Files Then Removing (My First Post!)
I am having a problem with deleting people from the list, to do this I was trying to make an array for every line in the file, then delete the file, and remake it excluding the name that was deleted.
But what's happening is the file is either not being deleted at all, or being remade with the same lines as before times two and another line named "null".
(I am trying to make a "friends list", I have adding and reading through the friends list already made, as you add people the text file will go down a line, then it will read through it line-by-line)
This is something like my code:
if(s1.startsWith(".dll ")){
String stn = s1.substring(5);
try{
String file = "C:/Friends.txt";
java.io.FileReader fr = new FileReader( file ) ;
java.io.BufferedReader reader = new BufferedReader( fr ) ;
String line = null ;
int xyz = 1;
String aString[];
aString = new String[100];
while( ( line = reader.readLine() ) != null )
{
aString[xyz] = line;
xyz++;
}
File bkup = new File("C:/Friends.txt");
bkup.delete();
BufferedWriter out = new BufferedWriter(new FileWriter("C:/Friends.txt", true));
for(int xxxx = 1; xxxx <= xyz; xxxx++){
if(aString[xxxx] != stn){
out.append(aString[xxxx]);
out.newLine();
}
}
out.close();
}catch(Exception e){
}
- 06-29-2011, 04:23 AM #2
Never ever do this. You have no idea if an exception was thrown or not. At the very least do:Java Code:}catch(Exception e){ }
Java Code:}catch(Exception e){ e.printStackTrace(); }
- 06-29-2011, 04:55 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Alright, thanks. Do you know what my problem is?
- 06-29-2011, 05:12 AM #4
Run your code again and see if there is an exception. If there is copy and paste the exact and full message.
- 06-29-2011, 05:55 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
No exceptions.
- 06-29-2011, 06:01 AM #6
A couple of things...
You cannot delete the file as you already have it open. But you don't even have to worry about deleting it. Just create the FileWriter not in append mode and it will simply overwrite what is already in the file.
- 06-29-2011, 06:07 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Consider posting a SSCCE - a small amount of runnable code that illustrates your problem.This is something like my code:
It is unlikely that the SSCCE will be your actual code (unless yours is a very small program!) but it should compile and run. And it should result in the erroneous output.
(Another small matter is that it would help if you use the "code" tags. You put [code] at the start and [/code] at the end. That way the code will retain its formatting when it is displayed in a web page.)
---------
I haven't really looked at the something-like code, but the following is almost certainly wrong:
Compare String instances (and instances of other things) with the equals() method. This method defines an appropriate way for instances to be compared with one another.Java Code:aString[xxxx] != stn
Java Code:if(!aString[ndx].equals(stn)) {
- 06-29-2011, 06:10 AM #8
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
I changed the out.append to out.write, and deleted the deleting of the file, but I don't think that's what you mean't. Can you explain more?
else if(s1.startsWith(".dll ")){
String stn = s1.substring(5);
try{
String file = "C:/Friends.txt";
java.io.FileReader fr = new FileReader( file ) ;
java.io.BufferedReader reader = new BufferedReader( fr ) ;
String line = null ;
int xyz = 1;
String aString[];
aString = new String[100];
while( ( line = reader.readLine() ) != null )
{
aString[xyz] = line;
xyz++;
}
BufferedWriter out = new BufferedWriter(new FileWriter("C:/Friends.txt", true));
for(int xxxx = 1; xxxx <= xyz; xxxx++){
if(aString[xxxx] != stn){
out.write(aString[xxxx]);
out.newLine();
}
}
out.close();
}catch(Exception e){
e.printStackTrace();
}
}
- 06-29-2011, 06:13 AM #9
The boolean second parameter creates a FileWriter in append mode. Create a FileWriter without the boolean and it will not append.Java Code:new FileWriter("C:/Friends.txt", true));
- 06-29-2011, 06:29 AM #10
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
Hmm it's still not working :S
String stn = s1.substring(5);
try{
String file = "C:/Friends.txt";
java.io.FileReader fr = new FileReader( file ) ;
java.io.BufferedReader reader = new BufferedReader( fr ) ;
String line = null ;
int xyz = 1;
String aString[];
aString = new String[100];
while( ( line = reader.readLine() ) != null )
{
aString[xyz] = line;
xyz++;
}
BufferedWriter out = new BufferedWriter(new FileWriter("C:/Friends.txt"));
for(int xxxx = 1; xxxx <= xyz; xxxx++){
if(aString[xxxx] != stn){
out.append(aString[xxxx]);
out.newLine();
}
}
out.close();
}catch(Exception e){
e.printStackTrace();
}
I even tried replacing out.append with out.write, what it's doing now is adding a new line of "null" to the text, not deleting anything.
- 06-29-2011, 06:34 AM #11
Did you read reply #7 by pbrockway?
- 06-29-2011, 06:40 AM #12
Member
- Join Date
- Jun 2011
- Posts
- 9
- Rep Power
- 0
No, I missed it, but now with
for(int xxxx = 1; xxxx <= xyz; xxxx++){
if(!aString[xxxx].equals(stn)) {
out.append(aString[xxxx]);
out.newLine();
}
}
out.close();
it deletes everything in the Friends.txt
I added in "Jim" and "Bob" then did ".dll Jim" and it deleted everything inside of the txt file.
- 06-29-2011, 06:51 AM #13
Similar Threads
-
Searching through folder to find text inside all files
By dazzabiggs in forum New To JavaReplies: 3Last Post: 05-03-2011, 01:20 PM -
Searching through a directory of files and reading contents
By dazzabiggs in forum New To JavaReplies: 0Last Post: 05-01-2011, 07:23 PM -
Searching directories for folders and .txt files
By XDrew574X in forum New To JavaReplies: 1Last Post: 03-29-2011, 09:41 PM -
java searching from many files
By manish250 in forum Advanced JavaReplies: 0Last Post: 12-31-2010, 07:50 AM -
Help removing pronouns from text
By jessie in forum New To JavaReplies: 11Last Post: 11-19-2010, 05:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks