Concatenate files


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Concatenate files

Hybrid View

  1. #1
    Join Date
    Oct 2004
    Posts
    34

    Question Concatenate files

    I have attached two files, ListOfFiles.txt and Concatenate.txt.
    I'm using a command line (dos window)
    running the command....
    java Concatenate file1.txt file2.txt

    The program concatenates the contents of the two files to the console.

    I want to know how I can store the results in an another file(outagain.txt)?

    Thanks,

    the results in the file now gives me a '?' symbol.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2005
    Location
    Reisterstowwn, MD
    Posts
    72
    The while loop in your Concatenate class is wrong.

    It should be:

    Code:
    while ((c = s.read()) != -1){
    	   bw.write(c);
               System.out.write(c);
    }
        System.out.flush();
        s.close();
        bw.close();
    You were writing c to your file stream after it already goes through the sequence of streams. So it writes the value of -1 into the stream (it came out as a question mark in the text file). Also, if you are going to write bytes directly to the out stream, then you need to flush the stream (unless the byte is a newline character, ASCII value of 10, then it automatically flushes). You could also simply use System.out.print or .println. Anyways, there ya go, should work fine now. One more thing to think about. It is probably better if in your ListOfFiles class you have a private array of input streams rather then Strings. In the constructor pass it an array of strings and convert them to FileInputStreams storing them inside the InputStream array. Java by default uses polymorphism, so there is no problem doing this. This way would make your nextElement function a lot cleaner. Like this:

    Code:
    public class ListOfFiles implements Enumeration  {
    	private InputStream[] listOfStreams;
        private int current = 0;
    
        public ListOfFiles(String[] listOfFiles) {
    		this.listOfStreams = new InputStream[listOfFiles.length];
    		for(int i=0; i<listOfFiles.length; i++){
    			try {
    				listOfStreams[i] = new FileInputStream(listOfFiles[i]);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    		}
        }
    
        public boolean hasMoreElements() {
            if (current < listOfStreams.length)
                return true;
            else
                return false;
        }
    	
    	public InputStream nextElement(){
    		InputStream temp;
    		if(hasMoreElements()){
    			temp = listOfStreams[current];
    			current++;
    			return(temp);
    		}
    		else{
    			throw new NoSuchElementException("No more files.");
    		}
    	}
    }
    Either way the program works, and i guess it just comes down to personal preference.
    Java has 99 problems but a pointer ain't one

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center