import java.io.*; import java.util.*; class Main{ //Ex1 static public void Ex1 (String arg[]) { File f1 = new File ("Ex1Swap1.txt"); File f2 = new File ("Ex1Swap2.txt"); int i = 1; int j = 2; System.out.println("Start:\ni = " + i + " and j = " + j); // //HOW TO SWAP? // int tmp=0; // // System.out.println("First step:\ni = " + i + "\nj = " + j + "\ntmp = "+ tmp); // tmp = i; // System.out.println("Second step:\ni = " + i + "\nj = " + j + "\ntmp = "+ tmp); // i=j; // System.out.println("Third step:\ni = " + i + "\nj = " + j + "\ntmp = "+ tmp); // j= tmp; // System.out.println("Fourth step:\ni = " + i + "\nj = " + j + "\ntmp = "+ tmp); // System.out.println("End:\ni = " + i + " and j = " + j); //we want to do the same with files //However the File method renameTo() has weel-known problem on Windows //It can not rename existing files... //this my solution to this problem: create new files //I create two new File object tmp1 and tmp2 which will be the new names of f1 and f2 f1.renameTo(new File ("tmp1")); f2.renameTo(new File ("tmp2")); //to operate on these new files i create two new File objects File tmp1 = new File ("tmp1"); File tmp2 = new File ("tmp2"); //then again, i create two new File objects and I rename tmp's with these names tmp1.renameTo(new File ("Ex1Swap2.txt")); tmp2.renameTo(new File ("Ex1Swap1.txt")); //here I used some string because the name of the files are given, //but you can have a more general method recording the two file name //in string created by using the getName() method //Ex2 public static void Ex2() throws Exception{ //create the file and use it to create the scanner File gar = new File ("Garbage.txt"); Scanner sGar = new Scanner(gar); //print the next line untill the end of the file while(sGar.hasNext()) { System.out.println(sGar.nextLine()); } sGar.close(); //close the scanner //open a writer, write the sentence and close it PrintWriter pGar = new PrintWriter(gar); pGar.println("I am going to be erased"); pGar.close(); //open a NEW scanner for read the new content of the file, print it and then close Scanner newSGar = new Scanner(gar); while(newSGar.hasNext()) { System.out.println(newSGar.nextLine()); } newSGar.close(); //delede the file gar.delete(); //just a message System.out.println("Has Garbage.txt been removed? " + gar.exists()); } //EX3: COPY EVEN LINES public static void Ex3() throws Exception{ int i = 1; File fa = new File("a.txt"); File fb = new File("b.txt"); //create a scanner which reads the file fa Scanner sa = new Scanner (fa); //create a printwriter which writes in the file fb PrintWriter pb= new PrintWriter(fb); //starting the loop: untill there is something to read while (sa.hasNext()) { //verify if I am in a even line (see counter i), in this case I write the line in b if(i % 2==0) { pb.println(sa.nextLine()); } //increase the line counter i++; } //close the scanner and the printer sa.close(); pb.close(); } //Ex4 public static void Ex4() throws Exception{ Scanner user = new Scanner(System.in); //scan the name of the two files System.out.println("Give the name of the file to be processed: "); String name1 = user.nextLine(); System.out.println("Give the name of the output file: "); String name2 = user.nextLine(); //create the File objects for these files File f1 = new File (name1); File f2 = new File (name2); //use the first to create a scanner and the second for the printer Scanner s1 = new Scanner(f1); PrintWriter p2 = new PrintWriter (f2); //set the counters and allocate a variable for string references int linesCounter=0; int charactersCounter = 0; String s; //scan the file until the end, increase the line number every newline and //character counter by adding the lenght of each line while (s1.hasNext()) { s=s1.nextLine(); charactersCounter = charactersCounter+s.length(); linesCounter++; } //use the data to print the content in the file p2.println("File name:" + name1); p2.println("Number of lines: " + linesCounter + "\nNumber of characters: " + charactersCounter); //remember to close all the scanners/printers s1.close(); p2.close(); user.close(); } //Ex5: public static void Ex5() throws Exception{ //create scanner and printer directly (one single passage) PrintWriter pA = new PrintWriter(new File("A.txt")); Scanner sB =new Scanner(new File("B.txt")); Scanner sC =new Scanner(new File("C.txt")); //scan the file B and C until one of the two is non-empty //and write the newline in A (an empty line when the file finish) while( sB.hasNext() && sC.hasNext() ) { if(sB.hasNext()) { pA.println(sB.nextLine()); }else { pA.println(""); } if(sC.hasNext()) { pA.println(sC.nextLine()); }else { pA.println(""); } } pA.close(); sB.close(); sC.close(); } }