Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import acm.program.ConsoleProgram;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.util.Scanner;
  6.  
  7.  
  8.  
  9. public class Task1 extends ConsoleProgram {
  10. public void run() {
  11. String allFile = fileReader();
  12. CharSequence toReplace = readLine();
  13. CharSequence newLine = readLine();
  14.  
  15. println(doTask(allFile,toReplace,newLine));
  16. }
  17.  
  18. public String fileReader() {
  19. FileReader fr = null;
  20. try {
  21. fr = new FileReader("C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\workspace\\pract11\\src\\pract11\\in.txt");
  22. } catch (FileNotFoundException e) {
  23. // TODO Auto-generated catch block
  24. e.printStackTrace();
  25. }
  26. Scanner scan = new Scanner(fr);
  27. String str = "";
  28. boolean f = true;
  29. while (scan.hasNextLine()) {
  30. if (f) {
  31. f = false;
  32. str = scan.nextLine();
  33. } else
  34. str = str + "\n" + scan.nextLine();
  35. }
  36.  
  37. return str;
  38. }
  39.  
  40.  
  41. public String doTask(String allFile, CharSequence toReplace, CharSequence newLine) {
  42. return allFile.replace(toReplace,newLine);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement