Click here to Skip to main content
15,901,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
code that should create multiple files is

package FileSystem;

import java.io.File;
import java.io.IOException;

public class CreateFiles
{
    public static void main( String[] args ) throws IOException
    {	
        try
        {
        String[] Names = new String[20]; 
        Names[0] = "Android";
        Names[1] = "java";
        Names[2] = "computerscience";
        Names[3] = "satellite";
        Names[4] = "communication";
        Names[5] = "navigator";
        Names[6] = "science";
        Names[7] = "a";
        Names[8] = "b";
        Names[9] = "c";
        Names[10] = "d";
        Names[11] = "e";
        Names[12] = "f";
        Names[13] = "g";
        Names[14] = "h";
        Names[15] = "i";
        Names[16] = "j";
        Names[17] = "l";
        Names[18] = "m";
        Names[19] = "n";
        
        for(int j = 0; j<Names.length; j++)
        {
            File file = new File("F:\\" + Names + ".txt");                        	                    
        }
        }
        catch(Exception e)            
        {
            e.printStackTrace();
        }    
        finally
        {
        }
    }
}


there is no error message but the files are not created, similar code for single file works fine which is :

import java.io.File;
import java.io.IOException;

public class CreatesFile 
{
    public static void main( String[] args )
    {	
    	try {
    		 
	      File file = new File("c:\\newfile.txt");
	      
	      if (file.createNewFile()){
	        System.out.println("File is created!");
	      }else{
	        System.out.println("File already exists.");
	      }
	      
    	} catch (IOException e) {
	      e.printStackTrace();
	}
    }
}



the assignment is to create multiple files with different names

What I have tried:

changed code several times and searched
Posted
Updated 5-May-18 7:54am

1 solution

Have a look at the file creation
Java
File file = new File("F:\\" + Names + ".txt");  

You're referring to the array itself, not to an individual member. So you should use your counter variable j to specify the index from the array
 
Share this answer
 
Comments
four systems 6-May-18 11:42am    
File file = new File("F:\\" + j + ".txt"); is this is what you suggest, did that still does not create files, however found another way with

public class ForFilesWriter
{

public static void main(String[] args) throws IOException
{
BufferedWriter writer = null;
for(int i=1 ; i<=5 ; i++){
String fileName = "name" + i + ".txt";
PrintWriter printer = new PrintWriter(fileName, "UTF-8");
writer.write("Java is object oriented");
writer.close();
}}}

now would want to create files with names as the array has


public class ForFilesWriter
{

public static void main(String[] args) throws IOException
{
BufferedWriter writer = null;
String[] Names = new String[5];
Names[0] = "Android";
Names[1] = "java";
Names[2] = "computerscience";
Names[3] = "satellite";
Names[4] = "communication";
for(int i=1 ; i<=Names.length ; i++){
String fileName = "name" + i + ".html";
PrintWriter printer = new PrintWriter(fileName, "UTF-8");
printer.write("Java is object oriented");
writer.write("Java is object oriented");
writer.close();
}}}
Wendelius 6-May-18 13:51pm    
No, that's not what I suggested.

You already use indexes when adding strings to the array. You use the individual values the same way.

Try
File file = new File("F:\\" + Names[j] + ".txt");    
four systems 6-May-18 14:05pm    
import java.io.File;
import java.io.IOException;

public class CreateFiles
{
public static void main( String[] args ) throws IOException
{
try
{
String[] Names = new String[20];
Names[0] = "Android";
Names[1] = "java";
Names[2] = "computerscience";
Names[3] = "satellite";
Names[4] = "communication";
Names[5] = "navigator";
Names[6] = "science";
Names[7] = "a";
Names[8] = "b";
Names[9] = "c";
Names[10] = "d";
Names[11] = "e";
Names[12] = "f";
Names[13] = "g";
Names[14] = "h";
Names[15] = "i";
Names[16] = "j";
Names[17] = "l";
Names[18] = "m";
Names[19] = "n";

for(int j = 0; j<Names.length; j++)
{
File file = new File("F:\\" + Names[j] + ".txt");
}
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
}
}
}
"the program runs but no files are created and there is no esception"
Wendelius 6-May-18 14:10pm    
You still have to call the createNewFile method like you did in your example in the post.
four systems 6-May-18 14:25pm    
wow, the files are created thanks

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