Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. import acm.program.ConsoleProgram;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.Scanner;
  8.  
  9. public class Task3_1 extends ConsoleProgram {
  10. public void run(){
  11. boolean isOdd = true;
  12. String fileName1="C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\home11\\odd.txt";
  13. String fileName2="C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\home11\\even.txt";
  14. // String allFile = fileReader();
  15. String res1="";
  16. String res2="";
  17. if(isOdd){
  18. res1+= fileReader(fileName1);
  19. isOdd = false;
  20. }else{
  21. res2+= fileReader(fileName2);
  22. isOdd = true;
  23. }
  24. try {
  25. fileWriter(fileName1,res1);
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. try {
  30. fileWriter(fileName2,res2);
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35.  
  36. public String fileReader(String outP) {
  37. FileReader fr = null;
  38. try {
  39. fr = new FileReader("C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\home11\\in.txt");
  40. } catch (FileNotFoundException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. Scanner scan = new Scanner(fr);
  45. String str = "";
  46.  
  47. boolean f = true;
  48. while (scan.hasNextLine()) {
  49. if (f) {
  50. f = false;
  51. str = scan.nextLine();
  52. } else
  53. str = str + "\n" + scan.nextLine();
  54. }
  55.  
  56. return outP;
  57. }
  58.  
  59. public void fileWriter(String fileName, String str) throws IOException {
  60.  
  61. FileWriter nFile = new FileWriter(fileName);
  62. nFile.write(str+"\n");
  63. nFile.close();
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement