import java.io.*; public class Linking { // ************************************************************ // * * // * main Method * // * * // ************************************************************ public static void main(String[] args) throws Exception { System.out.println("\n * * * Welcome to the Program * * * \n"); /*CALL*/readFile(); System.out.println("\n * * * End of the Linking * * * \n"); } // ************************************************************ // * * // * readFile Method * // * * // ************************************************************ public static void readFile() { String firstLineSt=""; String secondLineSt=""; try // Read the INPUT FILE { FileReader inputFileReader = new FileReader("Num.txt"); BufferedReader inputFileBuffer = new BufferedReader(inputFileReader); firstLineSt=inputFileBuffer.readLine(); secondLineSt=inputFileBuffer.readLine(); System.out.println(firstLineSt); System.out.println(secondLineSt); writeFile(firstLineSt,secondLineSt); firstLineSt=secondLineSt; while(true) // Until the end of the input file { secondLineSt=inputFileBuffer.readLine(); if(firstLineSt==null) // End of the input file { break; } System.out.println(firstLineSt); System.out.println(secondLineSt); writeFile(firstLineSt,secondLineSt); firstLineSt=secondLineSt; } // End of the WHILE loop (End of the file) inputFileReader.close(); } // End of the TRY catch(FileNotFoundException e) { System.out.println("Unable to Open INPUT File"); } catch(IOException e) { System.out.println("Unable to Close INPUT File"); } } // ************************************************************ // * * // * writeFile Method * // * * // ************************************************************ public static void writeFile(String tempSt1,String tempSt2) { char backslash=92; String umlword="UML"; String firstPartPath=umlword+backslash; String filePath1=""; String filePath2=""; filePath1=firstPartPath.concat(tempSt1).concat(".html"); filePath2=firstPartPath.concat(tempSt2).concat(".html"); // System.out.println(filePath1); // System.out.println(filePath2); tempSt1=(char)10+"Previous"; tempSt2=(char)10+"Next"; replaceStringInFile(filePath2, "XPrevious", tempSt1); replaceStringInFile(filePath1, "XNext", tempSt2); } // ************************************************************ // * * // * replaceStringInFile Method * // * * // ************************************************************ public static void replaceStringInFile(String filePath, String match, String replacingString) { try { RandomAccessFile raf = new RandomAccessFile(filePath, "rw"); long pointer = raf.getFilePointer(); String lineData = ""; while((lineData =raf.readLine()) != null) { pointer = raf.getFilePointer() - lineData.length()-2; if(lineData.indexOf(match) > 0) { System.out.println("Changing string in file "+filePath); raf.seek(pointer); raf.writeBytes(replacingString); // if the replacingString has less number of characters than the matching string line then enter blank spaces. if(replacingString.length() < lineData.length()) { int difference = (lineData.length() - replacingString.length())+1; for(int i=0; i < difference; i++) { raf.writeBytes(" "); } } } } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } catch(Exception ex) { ex.printStackTrace(); } } // ************************************************************ // ************************************************************ // ********************End Of the Program********************** // ************************************************************ // ************************************************************ }