Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import acm.program.ConsoleProgram;
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Task3_1 extends ConsoleProgram {
- public void run(){
- boolean isOdd = true;
- String fileName1="C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\home11\\odd.txt";
- String fileName2="C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\home11\\even.txt";
- // String allFile = fileReader();
- String res1="";
- String res2="";
- if(isOdd){
- res1+= fileReader(fileName1);
- isOdd = false;
- }else{
- res2+= fileReader(fileName2);
- isOdd = true;
- }
- try {
- fileWriter(fileName1,res1);
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- fileWriter(fileName2,res2);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public String fileReader(String outP) {
- FileReader fr = null;
- try {
- fr = new FileReader("C:\\Users\\БарабухаМаріяМаксимі\\Desktop\\home11\\in.txt");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- Scanner scan = new Scanner(fr);
- String str = "";
- boolean f = true;
- while (scan.hasNextLine()) {
- if (f) {
- f = false;
- str = scan.nextLine();
- } else
- str = str + "\n" + scan.nextLine();
- }
- return outP;
- }
- public void fileWriter(String fileName, String str) throws IOException {
- FileWriter nFile = new FileWriter(fileName);
- nFile.write(str+"\n");
- nFile.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement