Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default How to get the list of Files inthe Directory and write data into them One by One

    Hi Forum,

    I have 100 text files, I need to write data into them. I am reading data from another file.

    Please provide me some guideline for doing this...

    Thanks in Advance...

  2. #2
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Anything tried yet? Or nothing attempted?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  3. #3
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    HI
    Java Code:
    import java.io.*;
    import java.io.Writer;
    
    public class CreateFile2{
        public static void main(String[] args) throws IOException{
          File f;
    	   
     try{
        String strDirectoy ="test";
        String strManyDirectories="Application\\Test\\";
    
        // Create directory
          boolean  success = (new File(strManyDirectories)).mkdirs();
    
        if (success) {
          System.out.println("Directories: " + strManyDirectories + " created");
    
    
    for (int i=0;i<=100 ;i++)
    	  {   
    				  
    		  f=new File( ""+ strManyDirectories +""+"Application"+i+".txt");
             f.createNewFile();
    
    	 FileWriter fstream = new FileWriter(f,true);
             BufferedWriter out = new BufferedWriter(fstream);
    	 out.write("Hello Java");
    	 out.close();
    
    	  }
                    
        }
          }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
        }	   
    
    	}
    Above Code will create the file from application0.txt to the end of loop.
    I need to write data into them
    bye selecting files one by one

  4. #4
    Join Date
    Mar 2011
    Posts
    52
    Rep Power
    0

    Default

    Sorry for my English...

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Don't do your operation on the basis of,
    Java Code:
    if (success) {
    Instead, create the directory and check if it exists. Something like,
    Java Code:
    File dir = new File(strManyDirectories);
    dir.mkdirs();
    
    if(dir.exists()){ ....//Rest of the code.
    Currently your code isn't getting passed through the if statement because when the directory exists it doesn't create it second time and returns false.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Create a table with buttons and the files list of a directory.
    By danielpereira in forum Advanced Java
    Replies: 1
    Last Post: 11-22-2010, 07:43 PM
  2. List directory
    By dman in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 02:32 AM
  3. Populating a gui list box with the files in a directory
    By josejvelezcolon in forum New To Java
    Replies: 1
    Last Post: 08-10-2009, 05:50 PM
  4. how to list certain files in a given directory in java
    By corpusluteum in forum New To Java
    Replies: 6
    Last Post: 09-24-2008, 01:19 AM
  5. How can I get list of files in a directory
    By karma in forum New To Java
    Replies: 2
    Last Post: 12-15-2007, 12:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •