Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void simpleReplace(String file, String oldChar, String newChar){
- FileReader fr = null;
- PrintWriter pw = null;
- File f = new File(file);
- boolean b = f.exists();
- boolean fileNotEmpty = true;
- try{
- fr = new FileReader(file + ".txt");
- BufferedReader br = new BufferedReader(fr);
- if (b) {
- pw = new PrintWriter(new FileWriter(file + ".txt", true));
- }
- else {
- pw = new PrintWriter(new FileWriter(file + ".txt", false));
- }
- while (fileNotEmpty){
- String s = br.readLine();
- if (s == null)
- {
- fileNotEmpty = false;
- break;
- }
- s.toLowerCase();
- for (int i=0; i<s.length(); i++){
- oldChar = s.substring(i-1,i);
- s.replaceAll(oldChar,newChar);
- pw.println(s);
- pw.flush();
- }
- fr.close();
- }
- }
- catch(FileNotFoundException e){
- System.out.println("File not found.");
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- finally {
- if (pw != null){
- pw.close();
- }
- }
- }
Add Comment
Please, Sign In to add comment