I have created a text file named 'Month.txt' which contains this String: "January February March". This is my program below which deletes "February" from the text file:
import java.io.*;
import java.util.*;
class Delete_element_from_txtfile
{
public static void main()throws IOException
{
FileReader fr=new FileReader("Month.txt");
BufferedReader br=new BufferedReader(fr);
FileWriter fw=new FileWriter("Month.txt");
BufferedWriter bw=new BufferedWriter(fw);
PrintWriter pw=new PrintWriter(bw);
String str;
String newstr="";
while((str=br.readLine())!=null)
{
StringTokenizer S=new StringTokenizer(str);
while(S.hasMoreTokens()==true)
{
String month=S.nextToken();
if(month.equals("February"))
{
continue;
}
else
newstr=newstr+(month+" ");
}
}
pw.print(newstr);
pw.close();
bw.close();
fw.close();
br.close();
fr.close();
}
}
However after running the program, when I open the file, it's all empty. I have just started file handling in java, so I have no clue of what's going on. I would like some help on this problem. Thanks!