Guest User

Untitled

a guest
Jul 23rd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. public static void simpleReplace(String file, String oldChar, String newChar){
  2. FileReader fr = null;
  3. PrintWriter pw = null;
  4. File f = new File(file);
  5. boolean b = f.exists();
  6. boolean fileNotEmpty = true;
  7. try{
  8. fr = new FileReader(file + ".txt");
  9. BufferedReader br = new BufferedReader(fr);
  10. if (b) {
  11. pw = new PrintWriter(new FileWriter(file + ".txt", true));
  12. }
  13. else {
  14. pw = new PrintWriter(new FileWriter(file + ".txt", false));
  15. }
  16. while (fileNotEmpty){
  17. String s = br.readLine();
  18. if (s == null)
  19. {
  20. fileNotEmpty = false;
  21. break;
  22. }
  23. s.toLowerCase();
  24. for (int i=0; i<s.length(); i++){
  25. oldChar = s.substring(i-1,i);
  26. s.replaceAll(oldChar,newChar);
  27. pw.println(s);
  28. pw.flush();
  29. }
  30. fr.close();
  31. }
  32. }
  33. catch(FileNotFoundException e){
  34. System.out.println("File not found.");
  35. }
  36. catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. finally {
  40. if (pw != null){
  41. pw.close();
  42. }
  43. }
  44. }
Add Comment
Please, Sign In to add comment