Interview Question
Country: United States
BufferedReader br = new BufferedReader(new FileReader( filename ));
String content="":
String str;
while(str=br.readLine()!=null){
str=str.replace(str.substring(str.Indexof(','),str.lastindexof(',')+1),"");
content=content.concat(str);
}
BufferedWriter bw=new BufferedWriter(new FileWriter( filename ));
bw.write(content);
1. Read each line of the file until end of stream (using StreamReader)
- Anonymous September 26, 20122. Foreach line, split the string by ',' - str.Split(',');
3. print the first and the last elements of the split result array from step2