Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Getting filesnames from directory and write to file

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Location
    Moldova
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting filesnames from directory and write to file

    Hello guys.

    I was trying to get filenames from a directory and then put as content to another file.

    Here is what I've tried:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
     
    public class ReadFromDirectory {
     
    	public static void main(String[] args) {
     
    		String directoryName = "C:" + File.separator + "test";
    		String outputFile = "C:" + File.separator + "test" + File.separator + "test2";
    		File dir = new File(directoryName);
    		visitAllFiles(dir);
     
    		File objOutFile = new File(outputFile);
    		try {
    			//Create a stream for our output
    			FileOutputStream hamletOutputStream = new FileOutputStream(objOutFile);
    			// Write our output to our stream
    			// and thus to our file
    			hamletOutputStream.write('\n');
    			hamletOutputStream.write(dir);
    			//Close the output stream
    			hamletOutputStream.close();
    		}
    		catch (FileNotFoundException fnfe) {
    			System.out.println("Couldn't find a file called" + outputFile);
    		} catch (IOException ioe) {
    			System.out.println("Couldn't find a file called" + outputFile);
    		}
     
     
     
    	}
     
    	public static void visitAllFiles(File dir) {
    	    if (dir.isDirectory()) {
    	        String[] children = dir.list();
    	        for (int i=0; i<children.length; i++) {
    	            visitAllFiles(new File(dir, children[i]));
    	        }
    	    } else {
    	        process(dir);
    	    }
    	}
     
    }

    There are some mistakes and I can't figure out how to fix them, or may be I didn't understand the method. Help me to solve that. Thanks.
    Last edited by Waickem; July 12th, 2012 at 06:04 AM.
    /*CODE is what I see*/


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Getting filesnames from directory and write to file

    What does the process method do? It seems to me that visitAllFiles is not actually changing anything at the moment.
    Are you getting any errors or exceptions or anything?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Location
    Moldova
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting filesnames from directory and write to file

    Thanks for posting.
    Actually it shoud list the files in a directory, I took it from here: Listing the Files or Subdirectories in a Directory | Example Depot
    But it uses recursion and I can't understand.

    I find out another way to solve my problem. It works, but I don't know how to not replace the existing data in the file.

     
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    //import java.io.FileNotFoundException;
    //import java.io.IOException;
    import java.io.*;
     
    public class ReadFromDirectory {
     
    	public static void main(String[] args) {
     
    		String directoryName = "C:" + File.separator + "test";
    		String outputFile = "C:" + File.separator + "test" + File.separator + "test2.txt";
     
    		 String files;
     
    		  File folder = new File(directoryName);
    		  File[] listOfFiles = folder.listFiles(); 
     
    		  for (int i = 0; i < listOfFiles.length; i++) 
    		  {
     
    		   if (listOfFiles[i].isFile()) 
    		   {
    		   files = listOfFiles[i].getName();
    		   System.out.println(files);
    		   try{
    		   FileWriter outFile = new FileWriter(outputFile);
    		   PrintWriter out = new PrintWriter(outFile);
    		   out.write(files);
    		   out.close();
    		   }
    		   catch (Exception e){
    			   System.out.println("Exception");
    		   }
     
    		   }
    		  }
     
     
     
     
     
     
     
     
    	}
     
    }

    The output is the last file name processed, but I don't want it. I want to have all the names from the directory. How can I merge all the filenames like bunch of strings in my test2.txt?
    Last edited by Waickem; July 12th, 2012 at 07:26 AM.
    /*CODE is what I see*/

Similar Threads

  1. Checkin if a file exist within a directory
    By tash876 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 23rd, 2011, 02:16 PM
  2. How to upload a pdf file to mysql from a directory?
    By A4Andy in forum JDBC & Databases
    Replies: 1
    Last Post: October 19th, 2011, 08:49 AM
  3. Java I/O File code; how to read/write file
    By ryu2le in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: September 18th, 2011, 05:51 PM
  4. Help Remove file from pad directory
    By georgybaja in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: January 8th, 2011, 05:29 PM
  5. Virutal File Directory from Given dAta
    By hugsheals in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: July 28th, 2009, 03:39 AM