读取文件中的内容并同济字符串

读取文件“任务书t”中的内容,编程实现以下功能: ①统计字符串“网上书店”的个数 ②将“网上书店”全部替换成“线上鲜花商店


@SpringBootTest
public class EX {
    public static void main(String[] args) {
        InputStreamReader read=null;
        OutputStreamWriter writer=null;
        try {
            File file=new File("e:/a.txt");
            if(file.isFile()&&file.exists()){
                read=new InputStreamReader(new FileInputStream(file), "UTF-8");
                BufferedReader bufferedReader=new BufferedReader(read);

                String line = bufferedReader.readLine();
                //个数
                int count = count(line, "网上书店");
                //替换后再写进去
                String replace=line.replaceAll("网上书店","线上鲜花书店");
                System.out.println("个数:"+count);
                writer=new OutputStreamWriter(
                        new FileOutputStream(file), Charset.forName("UTF-8"));
                writer.write(replace);
                BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter(file));
                bufferedWriter.write(replace);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                read.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    public static int count(String s,String t){
        int count =0;
        try {
            if (s.indexOf(t) == -1) {
                return 0;
            }
            while (s.indexOf(t) != -1) {
                count++;
                s = s.substring(s.indexOf(t) + t.length());
            }
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        return count;
    }
}