over write file y or n shell script code example

Example 1: You will be provided a file path for input I, a file path for output O, a string S, and a string T. Read the contents of I, replacing each occurrence of S with T and write the resulting information to file O. You should replace O if it already exists.

# You will be provided a file path for input I, a file path for output O, a string S, and a string T.
# Read the contents of I, replacing each occurrence of S with T and write the resulting information to file O.
# You should replace O if it already exists.

file1 = open(I, 'r')
Icontent = file1.read()
editedData = Icontent.replace(S, T)
file2 = open(O, 'w')
file2.write(editedData)
file1.close()
file2.close()

Example 2: how to calculate min, max and average and write the output into into a text file in java

package TrialPrograms;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class Test {

    public static void main(String[] args) throws IOException {



        FileInputStream fi = new FileInputStream("F:\\Test\\file.txt");
        FileOutputStream fo = new FileOutputStream("F:\\Test\\output.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(fi));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fo));

        String strLine;
        while ((strLine = br.readLine()) != null)   {

              String[] arr = strLine.split(" ");
              String[] nos = arr[1].split(",");

              Set<Integer> set = new HashSet<Integer>();  
              for(int i = 0; i<nos.length; i++){
                  int no = Integer.parseInt(nos[i]);
                       set.add(no); 
                    }
              TreeSet<Integer> sortedSet = new TreeSet<Integer>(set); 

              switch(arr[0]) {

              case "Min:":
                  String msg1="The Min of [" +arr[1]+ "] is " +(Integer)sortedSet.first();
                  bw.write(msg1);
                  bw.newLine();

                  break;

              case "Max:":
                  String msg2="The Max of [" +arr[1]+ "] is " +(Integer)sortedSet.last();
                  bw.write(msg2);
                  bw.newLine();
                  break;

              case "Avg:":
                  Object[] noarray = sortedSet.toArray();
                  int noarraysize = noarray.length-1;
                  int sum=0;
                  for(int i=0;i<=noarraysize;i++) {

                      int no=Integer.valueOf(noarray[i].toString());
                      sum = sum + no;
                      if(i==noarraysize) {
                          String msg3="The Avg of [" +arr[1]+ "] is  " +(double)sum/noarray.length;
                          bw.write(msg3);
                          bw.newLine();
                                                  }
                  }
                  break;

              case "Sum:":
                  Object[] noarray1 = sortedSet.toArray();
                  int noarraysize1 = noarray1.length-1;
                  int sum1=0;
                  for(int i=0;i<=noarraysize1;i++) {
                      int no=Integer.valueOf(noarray1[i].toString());
                      sum1 = sum1 + no;
                      if(i==noarraysize1) {
                          String msg4="The Sum of [" +arr[1]+ "] is  " +sum1;
                          bw.write(msg4);
                          bw.newLine();
                                                  }
                  }
                  break;

            }

    }
        br.close();
        bw.close();

    }
}