Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Tolls

Thread: I'm new and stuck, please help :(

  1. #1
    XoVoX is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default I'm new and stuck, please help :(

    So I've been working on a program to take a directory of documents, add the .txt documents to an array, sort through them replacing specific string matches and then save them all to a combined .txt document.

    The problem I'm having is only the last file in the array is being saved to the new document. It has to be something with the way I'm casting/passing my ArrayList or I'm not doing something correctly on the iterations I'm using to replace the strings.

    Any help would be appreciated, I've linked the workspace directory from Eclipse as I cannot get the Attachment feature to work (dl<dot>dropbox<dot>com/u/1695566/combineTextDocs<dot>zip)

    I'm aware that there are probably a lot better ways of doing what I'm trying to do but please keep in mind that I do not know much about the available API's so I'm doing my best. Also, please forgive any sloppy code.

    Thanks and I hope I'm not violating any posting rules!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: I'm new and stuck, please help :(

    If you seriously want help, then post your relevant code in here using code tags (check the forum FAQ's to see how to do that).

  3. #3
    XoVoX is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: I'm new and stuck, please help :(

    Fair enough, here is my "main". I will follow with my two other classes below but I think my issue lies with the second Try/Catch statement below. As I said, sorry for the "sloppy" code.

    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class combineTextDocs {
    	
    	public static void main(String[] args) throws IOException {
    		
    		String compName = "";
    		String compAbbrev = "";
    		String origDate = "";
    		String updatedDate = "";
    		boolean createPDF = false;
    		String exportToWord = null;
    		
    		
    		//Get all settings from file
    		try {
    			ReadFile settings = new ReadFile(GetExecutionPath() + "\\settings.ini");
    			String[] settingsArray = settings.OpenFile();
    			
    			//Store settings into local variables
    			//Compare each line to match CompName, CompAbbrev, OrigDate, UpdatedDate
    			//If they match, extract the substring between the quotes ""
    			for (int y = 0; y < settingsArray.length; y++) {
    				if (settingsArray[y].matches("(?i)compname.*")) {
    					int indexOne = settingsArray[y].indexOf('\"') + 1;
    					int indexTwo = settingsArray[y].lastIndexOf('\"');
    					compName = settingsArray[y].substring(indexOne,indexTwo);
    				} else if (settingsArray[y].matches("(?i)compabbrev.*")) {
    					int indexOne = settingsArray[y].indexOf('\"') + 1;
    					int indexTwo = settingsArray[y].lastIndexOf('\"');
    					compAbbrev = settingsArray[y].substring(indexOne,indexTwo);
    				} else if (settingsArray[y].matches("(?i)origdate.*")) {
    					int indexOne = settingsArray[y].indexOf('\"') + 1;
    					int indexTwo = settingsArray[y].lastIndexOf('\"');
    					origDate = settingsArray[y].substring(indexOne,indexTwo);
    				} else if (settingsArray[y].matches("(?i)updateddate.*")) {
    					int indexOne = settingsArray[y].indexOf('\"') + 1;
    					int indexTwo = settingsArray[y].lastIndexOf('\"');
    					updatedDate = settingsArray[y].substring(indexOne,indexTwo);
    				} else if (settingsArray[y].matches("(?i)exporttoword.*")) {
    					//Append Template.dotx to GetExecutionPath() and store value for use later
    					exportToWord = GetExecutionPath() + "\\Template.dotx";
    				} else if (settingsArray[y].matches("(?i)createpdf.*")) {
    					//Assign true to createPDF variable for use later
    					createPDF = true;
    				} else {
    					System.out.println("The line \"" + settingsArray[y] + "\" doesn't match any settings.");
    				}
    			}
    		}
    		catch (IOException e) {
    			System.out.println("The settings file cannot be found. (Settings.ini)");
    		}		
    		
    		//Loop through each file in listFiles
    		//Update the fields on each fileLines[] and write line to new file
    		try {
    			ReadFile listFiles = new ReadFile(ListManualSections(GetExecutionPath()));
    			String[] fileLines = listFiles.OpenFile();
    			
    			WriteFile createManual = new WriteFile(GetExecutionPath() + "\\completedManual\\" + compName + "_Manual.txt", true);
    			
    			//Replace Company Details by line and write to new file "\\$CompName\\$" + ".txt"
    			//Why does it only write the lines from the last file in the array?
    			for (int i = 0; i<fileLines.length;i++){
    				fileLines[i] = fileLines[i].replaceAll("\\$CompName\\$", compName);
    				fileLines[i] = fileLines[i].replaceAll("\\$CompAbbrev\\$", compAbbrev);
    				fileLines[i] = fileLines[i].replaceAll("\\$OrigDate\\$", origDate);
    				fileLines[i] = fileLines[i].replaceAll("\\$UpdatedDate\\$", updatedDate);
    				createManual.writeToFile(fileLines[i]);
    				//System.out.println(fileLines[i]);
    			}
    		}
    		catch (IOException e) {
    			System.out.println("The file cannot be found.");
    		}
    		
    	}
    	
    
    	public static String GetExecutionPath() {
    		String executionPath = System.getProperty("user.dir");
    		
    		return executionPath;
    	}
    	
    	
    	public static String[] ListManualSections(String file_dir) {
    		file_dir = file_dir + "\\manualSections\\";
    		
    		String fileName;
    		
    		//Create list of all files in the manualSections directory.
    		File folder = new File(file_dir);
    		File[] listOfFiles = folder.listFiles(); 
    		
    		//Dynamic list of text files
    		ArrayList<String> al = new ArrayList<String>();
    		
    		//Print the amount of files
    		//System.out.println("There are " + listOfFiles.length + " files in manualSections");
    		
    		//Add each occurrence of a text file to the ArrayList
    		for (int i = 0; i < listOfFiles.length; i++) {
    			fileName = listOfFiles[i].getName();
    			if (listOfFiles[i].isFile() && fileName.toLowerCase().endsWith(".txt")) {
    				al.add(folder + "\\" + fileName);
    				//System.out.println("File " + i + " is: "+ al);
    			}
    		}
    
    		//Send list back to Main
    		String[] txtFiles = (String[]) al.toArray(new String[al.size()]);
    		return txtFiles;
    	}
    }
    ReadFile:
    Java Code:
    import java.io.IOException;
    import java.io.FileReader;
    import java.io.BufferedReader;
    
    public class ReadFile {
    	
    	private String path;
    	private int totalFiles = 0;
    	
    	public ReadFile(String file_path) {
    		
    		path = file_path;
    	}
    	
    	public ReadFile(String[] txtFiles) {
    		this.totalFiles = txtFiles.length;
    		
    		for (int i = 0;i<txtFiles.length;i++) {
    			this.path = txtFiles[i];
    			System.out.println(txtFiles[i]);
    		}
    	}
    	
    	public String[] OpenFile() throws IOException {
    		
    		FileReader fr = new FileReader(path);
    		BufferedReader textReader = new BufferedReader(fr);
    		
    		int numberOfLines = readLines();
    		String[] textData = new String[numberOfLines];
    		
    		for(int i = 0; i < numberOfLines; i++) {
    			textData[i] = textReader.readLine();
    		}
    		
    		textReader.close();
    		return textData;
    	}
    	
    	int readLines() throws IOException {
    		
    		FileReader file_to_read = new FileReader(path);
    		BufferedReader bf = new BufferedReader(file_to_read);
    		
    		String aLine;
    		int numberOfLines = 0;
    		
    		while ((aLine = bf.readLine()) != null) {
    			numberOfLines++;
    		}
    		
    		bf.close();
    		
    		return numberOfLines;
    	}
    	
    	int  totalFiles() throws IOException {
    		return totalFiles;
    	}
    
    }
    WriteFile:
    Java Code:
    import java.io.FileWriter;
    import java.io.PrintWriter;
    import java.io.IOException;
    
    public class WriteFile {
    	
    	private String path;
    	private boolean append_to_file = false;
    	
    	public WriteFile (String file_path) {
    		path = file_path;
    	}
    	
    	public WriteFile (String file_path, boolean append_value) {
    		path = file_path;
    		append_to_file = append_value;
    	}
    	
    	public void writeToFile (String textLine) throws IOException {
    		FileWriter write = new FileWriter(path, append_to_file);
    		PrintWriter print_line = new PrintWriter(write);
    		
    		print_line.printf("%s" + "%n", textLine);
    		
    		print_line.close();
    	}
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: I'm new and stuck, please help :(

    That just looks wrong.
    Sorry.

    The error is in the ReadFile() constructor taking the array argument.
    Your ReadFile only holds a single path, so how can it open all the files?
    That constructor simply results in the last file in the array being used as the path.

    In something like this the basic flow is:
    Java Code:
    Get file list.
    For each file
        open input.
        for each line
            read line
            process line
            write line.
        close input
    close output
    That's the structure you should be looking for.
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    XoVoX is offline Member
    Join Date
    Jul 2012
    Posts
    3
    Rep Power
    0

    Default Re: I'm new and stuck, please help :(

    I knew it'd be really "messed" up and all over the place but all I've had to go on is very limited experience and a few examples online. Thanks for the feedback, I'll see what I can do with it.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: I'm new and stuck, please help :(

    Good luck.
    That basic layout is the standard for any reading from A/writing to B processing you will do.
    There are slight variations on it, but "read a chunk"/"write a chunk" is what you should be aiming for. It takes the least amount of memory as well.

    Obviously if data from one point in A affects how you process data from another point in A then it gets more complex, but it doesn't sound like that's the case here.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: I'm new and stuck, please help :(

    Please go through the Forum Rules -- particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 4
    Last Post: 11-13-2011, 04:58 PM
  2. Stuck, need help please
    By Johnny2009 in forum New To Java
    Replies: 3
    Last Post: 11-03-2011, 11:25 PM
  3. Stuck in sea
    By programmer_007 in forum JDBC
    Replies: 1
    Last Post: 09-17-2009, 05:00 AM
  4. Im on my last lab!!!! And im stuck...:(
    By clanboru15 in forum New To Java
    Replies: 5
    Last Post: 03-13-2009, 02:44 AM
  5. really stuck now..
    By shongo in forum Advanced Java
    Replies: 2
    Last Post: 11-09-2008, 03:56 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
  •