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: Moving files to a single list iteratively

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Moving files to a single list iteratively

    Hello,

    I have a folder of around 200 notepad files with ticket information inside.
    Essentially, I need to compile a single PDF or Excel spreadsheet from all the information inside the individual notepad files.

    I don't want to have to open each notepad file, then copy and paste its data manually. I'd prefer to use some sort of loop function that opens each notepad file, extracts the data, and puts it onto an excel or PDF sheet.

    Is this possible?

    Thanks!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Moving files to a single list iteratively

    I have moved your post to a more appropriate forum.
    Yes it is possible. Get the folder, list the files in said folder, and read each one in a loop. See
    Lesson: Basic I/O (The Java™ Tutorials > Essential Classes)

  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Moving files to a single list iteratively

    Hello Growler,

    Welcome to the forums.

    I have some time on my hands so here is a good example to get you going..

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
     
    public class TxtFiles {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
     
    	// Directory path here
    	public static String path = ".";
     
    	public static void main(String[] args) {
    		listFiles();
    	}
     
    	public static void listFiles() {
     
    		String files;
    		File folder = new File(path);
    		File[] listOfFiles = folder.listFiles();
     
    		for (int i = 0; i < listOfFiles.length; i++) {
    			if (listOfFiles[i].isFile()) {
    				files = listOfFiles[i].getName();
    				if (files.endsWith(".txt") || files.endsWith(".TXT")) {
    					// Lists files in console
    					System.out.println(files);
    					openFile(files);
    				}
    			}
    		}
    	}
     
    	public static void openFile(String file) {
     
    		try {
    			FileInputStream in = new FileInputStream(file);
    			BufferedReader br = new BufferedReader(new InputStreamReader(in));
    			String strLine;
     
    			while ((strLine = br.readLine()) != null) {
    				// Prints content of file to console
    				System.out.println(strLine);
    				// Write file method needs to go here
    			}
     
    		} catch (Exception e) {
    			System.out.println(e);
    		}
    	}
     
    }

    This code lists all the .txt files in a directory. It then opens them one by one and displays their content.

    You will need to figure out how to write the content to a single file.

    All the code you need can be found here - Java Code Snippets and Tutorials - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. Trying to crunch a date to a single digit
    By twitch09 in forum Java Theory & Questions
    Replies: 7
    Last Post: October 15th, 2010, 12:14 AM
  3. Getting List of Files Within Jar
    By aussiemcgr in forum Java Theory & Questions
    Replies: 1
    Last Post: October 13th, 2010, 09:38 AM
  4. Moving files in a Directory
    By Sai in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 23rd, 2010, 04:49 AM