使用文件对象创建多个文件 [英] Create multiple files with file object

查看:68
本文介绍了使用文件对象创建多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应创建多个文件的代码是



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

推荐答案

查看文件创建

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



您指的是数组本身,而不是指个人成员。所以你应该用你的计数器变量 j 来指定数组中的索引


这篇关于使用文件对象创建多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆