Reputation: 4696
I have about thousand lines of the type:
<option value="1436">Some text
<option value="36">Some text
I want to store a fresh file, without <...>
part in it.
The code I'm writing is:
try{
FileInputStream fs= new FileInputStream(f);
Scanner s= new Scanner(fs);
while (s.hasNext()){
String sentence=s.nextLine();
int l=sentence.length();
try{//printing P
BufferedWriter bw = new BufferedWriter(new FileWriter("Ps.txt"));
for (int i=0;i<l;i++){
if (sentence.charAt(i)=='>'){
for (int j=i+1;j<l;j++)
bw.write(sentence.charAt(j));
}
bw.newLine();
}
bw.close();
}
catch(Exception e){}
}
}
However, it just storing 3-4 lines in the output file.
Upvotes: 0
Views: 53
Reputation: 3068
You are opening and closing the file inside your while loop.you'll want to move it outside of the main loop, so you open the file once don't close the file until the end.
Thus, every iteration of the loop, you are overwriting the file, so the contents of the file will only be what was computed by the last iteration of the loop.
Upvotes: 2
Reputation: 46445
If using sed is an option, then
cat file1 | sed 's/\<.*\>//' > file2
Should do the trick
Upvotes: 0