Connect with Expertise | Find Experts, Get Answers, Share Insights

copy contents into multiple files

 
Join Date: Jan 2010
Posts: 1
#1: Jan 27 '10
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's Avatar
E
C
 
Join Date: Jan 2007
Location: America
Posts: 3,929
#2: Jan 27 '10

re: copy contents into multiple files


Use multiple threads?
E
C
 
Join Date: Nov 2007
Posts: 153
#3: Jan 28 '10

re: copy contents into multiple files


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 "simultaneous" bit on hold until you are sure about writing the code needed for basic file copying.
qlluq's Avatar  
Join Date: Jan 2010
Location: Istanbul / Turkey
Posts: 3
#4: Jan 28 '10

re: copy contents into multiple files


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7.  
  8. public class JFileCopier {
  9.  
  10.     private JFileCopier(){
  11.     }
  12.  
  13.     private static final JFileCopier INSTANCE = new JFileCopier();
  14.  
  15.     public static JFileCopier getInstance(){
  16.         return INSTANCE;
  17.     }
  18.  
  19.     public static void main(String[] args) {
  20.         String[] targetFiles = { "C:\\target1.txt","C:\\target2.txt"};
  21.         try {
  22.             JFileCopier.getInstance().copyFileContentToTargetFilesSynch("C:\\source.txt", targetFiles);
  23.         } catch (IOException e) {
  24.             e.printStackTrace();
  25.         }
  26.     }
  27.  
  28.     public void copyFileContentToTargetFilesSynch(String sourceFile , String[] targetFiles) throws IOException{
  29.         File sFile = new File(sourceFile);
  30.         InputStream sFileInputStream = new FileInputStream(sFile);
  31.         long length = sFile.length();
  32.  
  33.         byte[] sFileBytes = new byte[(int) length];
  34.         int offset = 0,numRead = 0;
  35.         while (offset < sFileBytes.length
  36.                 && (numRead = sFileInputStream.read(sFileBytes, offset,
  37.                         sFileBytes.length - offset)) >= 0) {
  38.             offset += numRead;
  39.         } 
  40.         sFileInputStream.close();
  41.  
  42.  
  43.         for(String targetFile : targetFiles){
  44.             new CopierThread(sFileBytes,targetFile).run();
  45.  
  46.         }
  47.  
  48.     }
  49.  
  50.     class CopierThread extends Thread {
  51.         byte[] content;
  52.         String targetFileName;
  53.         public CopierThread(byte[] content, String targetFileName) {
  54.             super();
  55.             this.content = content;
  56.             this.targetFileName = targetFileName;
  57.         }
  58.         public void run() {
  59.             try {
  60.                 FileOutputStream out = new FileOutputStream(targetFileName,true);
  61.                 out.write(content); 
  62.                 out.close(); 
  63.             } catch (IOException e) {
  64.                 System.err.println(targetFileName + " error : " + e.getMessage());
  65.             } 
  66.         } 
  67.     }
  68.  
  69. }
  70.  
  71.  
johny10151981's Avatar
C
 
Join Date: Jan 2010
Location: Japan
Posts: 292
#5: Jan 28 '10

re: copy contents into multiple files


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
johny10151981's Avatar
C
 
Join Date: Jan 2010
Location: Japan
Posts: 292
#6: Jan 28 '10

re: copy contents into multiple files


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
RedSon's Avatar
E
C
 
Join Date: Jan 2007
Location: America
Posts: 3,929
#7: Jan 28 '10

re: copy contents into multiple files


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."
qlluq's Avatar  
Join Date: Jan 2010
Location: Istanbul / Turkey
Posts: 3
#8: Jan 29 '10

re: copy contents into multiple files


Okay RedSon
U r right..
Reply