Results 1 to 4 of 4
  1. #1
    Burton333 is offline Member
    Join Date
    Apr 2010
    Rep Power

    Default Saving to text file

    Hello everyone! I have been messing around with this code I have been trying to get to work correctly all night. Basically I was just wanted it to read all the files in one folder, and save the titles to a text file. I have gotten everything else working, but when I attempt to save it to a text file it only saves the very last file name. Can someone please help me out before I drive myself crazy. Here is my code :
    Java Code:
    import java.util.*;
    public class DirectoryReader {
    	private static int arraySize;
    	public static void main(String[] args) {
    		File folder = new File("E:/Videos/Movies");
    		File[] listOfFiles = folder.listFiles();
    		String[] orderedTitles = new String[listOfFiles.length];
    		for (int i = 0; i < listOfFiles.length; i++) {
    			if (listOfFiles[i].isFile()) {
    				String fileName = (listOfFiles[i].getName());
    				int cd1Index = fileName.indexOf("CD1");
    				int cd2Index = fileName.indexOf("CD2");
    				int cd3Index = fileName.indexOf("CD3");
    				int cd4Index = fileName.indexOf("CD4");
    				int endIndex = fileName.indexOf(".");
    				String newName;
    				if (cd1Index > 0) {
    					newName = fileName.substring(0,cd1Index-2);
    				} else if (cd2Index > 0) {
    					newName = fileName.substring(0,cd2Index-2);
    				} else if (cd3Index > 0) {
    					newName = fileName.substring(0,cd3Index-2);
    				} else if (cd4Index > 0) {
    					newName = fileName.substring(0,cd4Index-2);
    				} else {
    					newName = fileName.substring(0,endIndex);
    				orderedTitles[i] = newName;
    			} else {
    				System.out.println("There is an incorrect file present in folder.");
    			arraySize = i;
    		SortedSet set = new TreeSet();
    		for (int k = 0; k < orderedTitles.length; k++) {
    		Iterator it = set.iterator();
    		while (it.hasNext()) {
    			// Get element
    			Object element =;
    	public static void saveFile(Object title) {
    			FileOutputStream fout;
    			try {
    				// Open an output stream
    				fout = new FileOutputStream ("myMovies.txt");
    				// Print a line of text
    		   	new PrintStream(fout).println (title);
    				// Close our output stream
    			// Catches any error conditions
    			catch (IOException e) {
    				System.err.println ("Unable to write to file");
    Thank you very much!

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Rep Power


    every time you open the file, you overwrite it. open it once, write into it, and close it once.

  3. #3
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Rep Power


    You put append true.

    fout = new FileOutputStream ("myMovies.txt",true);

  4. #4
    Burton333 is offline Member
    Join Date
    Apr 2010
    Rep Power


    That would make a lot of sense, I did not even think of that. Thanks iluxa. I tried just adding true like you said RamyaSivakanth, and it seems to be working fine now. Thanks a lot, you both were a lot of help.

Similar Threads

  1. saving the text in textpane in a file with styles.
    By jogendar in forum Advanced Java
    Replies: 9
    Last Post: 03-13-2010, 09:29 PM
  2. Creating&Saving as a different Text file
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 07:17 AM
  3. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 10
    Last Post: 10-24-2008, 07:21 PM
  4. Saving To A New Line Using A Text File
    By jadaleus in forum Advanced Java
    Replies: 1
    Last Post: 10-24-2008, 12:31 AM
  5. Saving to a Text File
    By jadaleus in forum Advanced Java
    Replies: 2
    Last Post: 10-17-2008, 06:50 PM

Posting Permissions

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