copy contents into multiple files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sheena1986
    New Member
    • Jan 2010
    • 1

    copy contents into multiple files

    hai everyone,
    Can anybody please tell me how to copy the contents of one file into many files at a time simultaneously? I want a JAVA source code for it please help
    Thank you
  • RedSon
    Recognized Expert Expert
    • Jan 2007
    • 4980

    #2
    Use multiple threads?

    Comment

    • pbrockway2
      Recognized Expert New Member
      • Nov 2007
      • 151

      #3
      Have you read the Basic I/O section of Sun's Tutorial?

      If you are new to the concepts involved this is a good place to start. Also you may find it productive to put the "simultaneo us" bit on hold until you are sure about writing the code needed for basic file copying.

      Comment

      • qlluq
        New Member
        • Jan 2010
        • 3

        #4
        Code:
        import java.io.File;
        import java.io.FileInputStream;
        import java.io.FileOutputStream;
        import java.io.IOException;
        import java.io.InputStream;
        
        public class JFileCopier {
        	
        	private JFileCopier(){
        	}
        	
        	private static final JFileCopier INSTANCE = new JFileCopier();
        	
        	public static JFileCopier getInstance(){
        		return INSTANCE;
        	}
        	
        	public static void main(String[] args) {
        		String[] targetFiles = { "C:\\target1.txt","C:\\target2.txt"};
        		try {
        			JFileCopier.getInstance().copyFileContentToTargetFilesSynch("C:\\source.txt", targetFiles);
        		} catch (IOException e) {
        			e.printStackTrace();
        		}
        	}
        	
        	public void copyFileContentToTargetFilesSynch(String sourceFile , String[] targetFiles) throws IOException{
        		File sFile = new File(sourceFile);
        		InputStream sFileInputStream = new FileInputStream(sFile);
        		long length = sFile.length();
        		
        		byte[] sFileBytes = new byte[(int) length];
        		int offset = 0,numRead = 0;
        		while (offset < sFileBytes.length
        				&& (numRead = sFileInputStream.read(sFileBytes, offset,
        						sFileBytes.length - offset)) >= 0) {
        			offset += numRead;
        		} 
        		sFileInputStream.close();
        		
        		
        		for(String targetFile : targetFiles){
        			new CopierThread(sFileBytes,targetFile).run();
        			
        		}
        		
        	}
        	
        	class CopierThread extends Thread {
        		byte[] content;
        		String targetFileName;
        		public CopierThread(byte[] content, String targetFileName) {
        			super();
        			this.content = content;
        			this.targetFileName = targetFileName;
        		}
        		public void run() {
        			try {
        				FileOutputStream out = new FileOutputStream(targetFileName,true);
        				out.write(content); 
        				out.close(); 
        			} catch (IOException e) {
        				System.err.println(targetFileName + " error : " + e.getMessage());
        			} 
        		} 
        	}
        
        }

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          Can you explain what do you mean by paragraph. from what type of file you are reading. from simple text file or doc file.

          In general sense a paragraph is a sequence of character without a new line in between. say you have written a 10 line document in note pad without pressing a single enter the entire 10 line would be a paragraph. But if you open any documents, as example, of RFC you will figure out that each paragraph is separated with two new lines.

          If a single new line define the new paragraph then read line is the simplest solution. But if there is different strategy to identify then you may have to work on it.

          But if you are talking about other then pain text file then you may have to look in the reference of that specific file type references.

          By the way there is 5 paragraph above this line according to my definition. there is no newline in any paragraph. But at least to newline after each paragraph :)

          Regards,
          Johny

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            so, I can guess is a single new line is not the paragraph. more than one new line is paragraph.

            what you can do is read line by line. when you are getting a line=="" then you can count is one paragraph is received. as example

            This memo provides information for the Internet community. It does
            not specify an Internet standard of any kind. Distribution of this
            memo is unlimited.


            This document describes the commonly used base 64, base 32, and base
            16 encoding schemes. It also discusses the use of line-feeds in
            encoded data, use of padding in encoded data, use of non-alphabet
            characters in encoded data, and use of different encoding alphabets.


            Look at the above text in bold(you dont have to read the text). there is two paragraph. how do we know that? simple few empty lines between paragraph. But in normal read line function you get 9 lines. so you can judge the paragraph by empty line between paragraph. so write your won program that will look for empty lines. if you get empty lines its a new paragraph. But I am not sure whether you will find a built in function to read paragraph or not. :)

            Regards,
            Johny

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              qlluq,

              Usually we are not in the business of providing full source solutions. Since that doesn't help anyone learn their craft better.

              "You can give a man a fish and he will eat for a day, or you can teach a man to fish and he will eat for a lifetime."

              Comment

              • qlluq
                New Member
                • Jan 2010
                • 3

                #8
                Okay RedSon
                U r right..

                Comment

                Working...