Answered Jan 20, 2016 · 1 votes
Well, off hand it's rather hard to tell without truly knowing what the addFileTag() method is doing. The only reason I can think of is that the webPage.txt file is still open for either read or write operations.
Your code has accessed the file but never closed it again. You can't rename a file that is open. I would have to assume this is in fact done somewhere within the addFileTag() method.
Because your call to the File.renameTo() method was unsuccessful the "webPage.txt" text file was never renamed to "CompiledWebpage.html" so in essence the "CompiledWebpage.html" file simply does not exist within the system. This however is not the reason why the Windows NotePad application is opening your file instead of the expected default Web Browser:
To begin with the File object variable so conveniently named 'file' was declared and initialized to be related to the "D:/dc1000/Project/webPage.txt" text file and it always will be since it's Class global unless of course that relationship is changed somewhere within your code. To be blunt... it's not and I guess it's a good thing for now because IF your File Rename was successful you would have simply gotten an FileNotFound Exception because the text file related to the 'file' variable would no longer exist due to the simple fact that it was renamed.
What you really want to pass to the DeskTop.browse() method is the File object 'file2' variable which is related to the "D:/dc1000/Project/CompiledWebpage.html" text file. Mind you, you'll still get an FileNotFound Exception because the File.renameTo() method had failed. So you definitely want to make sure you have success here.
Whatever...Why did the Windows NotePad application open instead of the Web Browser?
The Operating System File Associations is what determines which application will open the file when using the DeskTop.browse() method. In the Windows Operating System, by default, a file with the file name extension of ".txt" is automatically opened and displayed within NotePad, a file with the file name extension of ".docx" is automatically opened and displayed in MS Office WORD, a file with the file name extension of ".html" is opened and displayed within the default Web Browser. I think you get the idea here.
Because the 'file' variable is still related to the file "D:/dc1000/Project/webPage.txt" and because the File.renameTo() method failed, Windows simply seen the .txt file extension and displayed the file (as stipulated within the 'file' variable) to NotePad.
So...How do I get all this to actually Work!?
Well, if I may be so bold, do this instead:
Place this somewhere in your code, a button action event or whatever:
String sourceFile = "D:/dc1000/Project/webPage.txt";String destinationFile = "D:/dc1000/Project/CompiledWebpage.html";-boolean success = CompileToWebPage(sourceFile, destinationFile, "This is My Head Text");if (success) { System.out.println("Text File Successfully Compiled!");}else { System.out.println("Text File Compilation FAILED!");}-//Display our new file in the web Browser...try { File htmlFile = new File(destinationFile); Desktop.getDesktop().browse(htmlFile.toURI());} catch (IOException ex) {}-
Here is a new CompileToWebPage() method:
private static boolean CompileToWebPage(final String sourcefilePath, final String destinationFilePath, String... headText) { // headText is OPTIONAL. String headTxt = ""; if (headText.length != 0) { headTxt = headText[0]; }- //Read sourcefilePath file data into a String ArrayList... BufferedReader input; try { input = new BufferedReader(new FileReader(sourcefilePath)); if (!input.ready()) { throw new IOException(); } } catch (FileNotFoundException ex) { JOptionPane.showMessageDialog(null,"CompileToWebPage() Method Error!\n\nThe supplied " + "file path was not found!\n\n" + sourcefilePath, "File NotFound", JOptionPane.ERROR_MESSAGE); return false; } catch (IOException ex) { JOptionPane.showMessageDialog(null,"CompileToWebPage() Method Error!\n\nThe supplied " + "file is not ready to be read!\n\n" + ex.getMessage(), "File Not Ready", JOptionPane.ERROR_MESSAGE); return false; }- // Place required HTML Tags into String ArrayList ArrayList<String> txt = new ArrayList<>(); txt.add("<html>"); txt.add("<head>"); txt.add(headTxt); txt.add("</head>"); txt.add("<body>");- // Read each line of the source text File and add // them to our String ArrayList... try { String str; while((str = input.readLine()) != null){ txt.add("<p>" + str + "</p>"); } input.close(); } catch (IOException ex) { JOptionPane.showMessageDialog(null,"CompileToWebPage() Method Error!\n\n" + "There was a problem reading the source Text from file!\n\n" + ex.getMessage(), "File Read Error", JOptionPane.ERROR_MESSAGE); return false; }- // Place our HTML finishing Tags into our String ArrayList... txt.add("</body>"); txt.add("</html>");- // Write the String ArrayList to our supplied Destination // File Path... try { FileWriter fw = new FileWriter(destinationFilePath); Writer output = new BufferedWriter(fw);- for (int i = 0; i < txt.size(); i++) { // Some Windows applications (such as NotePad require // the \r tag for a new line to actually be accomplished // within a text file. output.write(txt.get(i) + "\r\n"); } output.close(); return true; } catch (IOException ex) { JOptionPane.showMessageDialog(null,"CompileToWebPage() Method Error!\n\n" + "There was a problem writing the Compiled Web Text to file!\n" + "Ensure that permissions are properly set.\n\n" + ex.getMessage(), "File Write Error", JOptionPane.ERROR_MESSAGE); return false; }}-
Well, I hope this has helped you somewhat or at the very least been entertaining.