Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Code that reads word from file creates file with that name and writes content

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class FileWriteHtmlWithFor
{     
   public static void main(String[] args)
    {
		BufferedWriter bw = null;
		FileWriter fw = null;                                 
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                try
                {                                                       
                BufferedReader in = new BufferedReader(new java.io.FileReader("F:\\words.txt"));
                String str;                
                
                String fileName = "F:\\words.txt";
                
                
                List<String> Satellite = Files.readAllLines(Paths.get(fileName), Charset.defaultCharset());                
                               
                 
                for(String Ad : Satellite)
                {                      
                String FILENAME = "F:\\"+Ad+".html";                    
                try
                {
                System.out.print("Word: " + Ad);                 
                String French = reader.readLine();                 
                                                             
                System.out.print("WordA: ");
                String WordA = reader.readLine();
                
                System.out.print("WordB: ");
                String WordB = reader.readLine();
                
                System.out.print("WordC: ");
                String WordC = reader.readLine();
                
                System.out.print("WordD: ");
                String WordD = reader.readLine();    
                
                System.out.print("a: ");            
                String a = reader.readLine();   
                        
                System.out.print("b: ");
                String b = reader.readLine();   
            
                System.out.print("c: ");
                String c = reader.readLine();   
            
                System.out.print("d: ");
                String d = reader.readLine();  
                
                
                fw = new FileWriter(FILENAME);
		bw = new BufferedWriter(fw);  
                bw.write("<html>");
                bw.write("<head>");
                
                bw.write("<script src='https://code.jquery.com/jquery-1.12.4.min.js'>"); 
                bw.write("</script>");
                
                bw.write("<script type='text/javascript'>");
                bw.write("$(document).ready(function(){");
                bw.write("$('#btnGetValue').click(function() {");
                bw.write("var selValue = $('input[name=rbnNumber]:checked').val();");
                bw.write("$('p').html('<br/> ' + selValue + '');");
                bw.write("});");
                bw.write("});");
                bw.write("</script>");
                
                bw.write("<script>");
                bw.write("function buttonClickHandler()");
                bw.write("{");
                bw.write("}");
                bw.write("</script>");
                
                bw.write("<Table align='center' color = 'cyan'>"); 
                bw.write("<TR>");            
                bw.write("<TD align = 'center' width ='145'>");
                bw.write(Ad);
//                bw.write(WordA); 
//                bw.write(WordB);
//                bw.write(WordC);
//                bw.write(WordD);                
//                bw.write("Android is Advanaced Java"); 
                bw.write("</TD>");
                bw.write("</TR>");           
                bw.write("<TR>");            
                bw.write("<TD width ='45'>");            
                bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + WordA + "  which is "+ a +" the correct answer' />(a) "  + WordA + "<br/>");   
                bw.write("</TD>");
                bw.write("</TR>");  
                bw.write("<TR>");            
                bw.write("<TD>");            
                bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + WordB + "  which is "+ b +" the correct answer' />(a) "  + WordB + "<br/>");   
                bw.write("</TD>");
                bw.write("</TR>");  
                bw.write("<TR>");            
                bw.write("<TD>");            
                bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + WordC + "  which is "+ c +" the correct answer' />(a) "  + WordC + "<br/>");   
                bw.write("</TD>");
                bw.write("</TR>");  
                bw.write("<TR>");            
                bw.write("<TD>");            
                bw.write("<input type='radio' name='rbnNumber' value='You selected (a) " + WordD + "  which is "+ d +" the correct answer' />(a) "  + WordD + "<br/>");   
                bw.write("</TD>");
                bw.write("</TR>");   
                bw.write("</Table>");   
                
                bw.write("<Table align = 'center'>");   
                bw.write("<TR>");            
                bw.write("<TD>");            
                bw.write("<input type='button' id='btnGetValue' Value='Get Value' />");            
                bw.write("</TR>");            
                bw.write("</TD>");            
                bw.write("</Table>");   
            
                bw.write("<Table align = 'center'>");   
                bw.write("<TR>");            
                bw.write("<TD>");                        
                bw.write("<p></p>");
                bw.write("</TR>");            
                bw.write("</TD>");            
                bw.write("</Table>");
                bw.write("<Title>");
                bw.write(Ad);
                bw.write("</Title>");                               
                bw.write("</html>");                             
                }
                catch(IOException d)
                {
                    d.printStackTrace();                
                }
                finally{
			try {
				if (bw != null)
					bw.close();

				if (fw != null)
					fw.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}                              
                }              
                }
                catch(IOException d)
                {
                    d.printStackTrace();
                }               
                finally {
			try {
				if (bw != null)
					bw.close();

				if (fw != null)
					fw.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}

		}
    }              
}       


What I have tried:

java classes, refactored code. it worced fine till recently but now gives the error

java.io.FileNotFoundException: F:\Wonderful			.html (The filename, directory name, or volume label syntax is incorrect)
	at java.io.FileOutputStream.open0(Native Method)
	at java.io.FileOutputStream.open(FileOutputStream.java:270)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
	at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
	at java.io.FileWriter.<init>(FileWriter.java:63)
	at html.FileWriteHtmlWithFor.main(FileWriteHtmlWithFor.java:65)
Posted
Updated 20-Aug-18 3:50am
Comments
OriginalGriff 18-Aug-18 5:57am    
And?
What does it do that you didn't expect, or not do that you did?
Where are you stuck?
What help do you need?
Richard MacCutchan 18-Aug-18 6:19am    
That error message is quite explicit. I would suggest you remove all those spaces in the filename.
[no name] 18-Aug-18 17:26pm    
Why spaces should be a Problem in filenames? At least for Windows "1 . txt" is a valid file Name. Leading and trailing spaces are ignored by Windows file System.
Richard MacCutchan 19-Aug-18 3:09am    
Yes, but if you try it from Java you get the above error message. Try without the spaces and everything is fine. I will try the same from C++ and C# to see how they behave.
Richard MacCutchan 19-Aug-18 3:36am    
Same problem from C/C++. Obviously Windows Explorer has found a way round this. I may try to find out how.

There were two problems with the original code:
1. Your filename was not being created correctly and included a number of spaces. This (fortunately) caused the program to crash thus exposing an error that was fairly easy to correct.
2. Trying to create a filename that contains embedded spaces will not work if the string is in ASCII, which was the case here. In order to correct this possible problem in the future you can add the following to your code:
Java
try {

    String sfn = "F:\\"+Ad+".html";             // create the basic name
    byte[] bfn = sfn.getBytes("UTF16");         // convert to an array of Unicode bytes
    String FILENAME = new String(bfn, "UTF16"); // create a Unicode string from those bytes
    FileWriter fw = new FileWriter(FILENAME);   // create the file from the Unicode string
}
catch (Exception ex) {
      ex.printStackTrace();
}
 
Share this answer
 
Comments
[no name] 20-Aug-18 9:53am    
A 5.
Richard MacCutchan 20-Aug-18 10:33am    
Thanks; 5 points for a morning's work :)
Try this :
Java
...
                for(String Ada : Satellite)
                {             
                String Ad = Ada.trim(); // <-- trim the spaces from the string           
                String FILENAME = "F:\\"+Ad+".html";  
...
 
Share this answer
 
Comments
[no name] 18-Aug-18 17:54pm    
fyi: space is a valid filename char
Mehdi Gholam 19-Aug-18 1:03am    
Yes, but the error is file not found.
solar os 19-Aug-18 12:21pm    
that worcs now the same code was fine earlier then suddenly the error, the words. file has two words but it creates + 1 i.e three files why is that ty found that it was the word which !was trimmed
java.io.FileNotFoundException: F:\Wonderful			.html (The filename, directory name, or volume label syntax is incorrect)

You are told that the file name "F:\Wonderful       .html" is not legal. One can guess the spaces in name are the problem.
You need to remove end spaces and tabs when you build the file name.
 
Share this answer
 
Comments
[no name] 18-Aug-18 17:25pm    
Spaces would not be the Problem I think (Windows accpets spaces for file names), but when I copy/paste the filename it contains Tabs, which are more problematic...
solar os 19-Aug-18 12:28pm    
wonderful and android are words that are read from notepad file what spaces are you talcing about is F:\\"+Ad+".html as F:\\"+ Ad +".html or different ty
solar os 19-Aug-18 12:34pm    
why file not found esception what file is it searching for, the code creates file as the words from file
Patrice T 19-Aug-18 13:08pm    
'at html.FileWriteHtmlWithFor.main(FileWriteHtmlWithFor.java:65)'
What is line 65 in your code ?
solar os 19-Aug-18 12:53pm    
wow man so many answers ty

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900