473,387 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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
Jan 27 '10 #1
7 5368
RedSon
5,000 Expert 4TB
Use multiple threads?
Jan 27 '10 #2
pbrockway2
151 Expert 100+
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.
Jan 28 '10 #3
qlluq
3
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.  
Jan 28 '10 #4
johny10151981
1,059 1GB
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
Jan 28 '10 #5
johny10151981
1,059 1GB
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
Jan 28 '10 #6
RedSon
5,000 Expert 4TB
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."
Jan 28 '10 #7
qlluq
3
Okay RedSon
U r right..
Jan 29 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Tess | last post by:
Hi, Long time reader, first time poster... Any help is appreciated. I have a few questions regarding Winform controls embedded within an html page. For more info please see the appendix. Now,...
5
by: SOR | last post by:
Assuming you had a template folder and files on the webspace and you wanted to make a exact copy/rename of it with a username and password being the one and only difference - how would one do it ?...
15
by: Sourcerer | last post by:
Hi all. Can I do this to duplicate the contents of struct a in struct b struct myStruct a, b, *aptr, *bptr; aptr = a; bptr = b; Do something to initialize contents of structure a...
2
by: Steve Franks | last post by:
The Copy Web tool provided with VS.NET 2005 is very convenient. However every once in a while it seems to think the files on the remote server have changed, which they have not. Then when I use...
8
by: luis molina Micasoft | last post by:
it seems that when i do file.copy the svchost.exe is hanged, i mean if i make 40 threads of file.copy , 40 copys of files at same time the system is going down and stop responding, this is when i'm...
11
by: MarkusR | last post by:
Good day, I need to safely copy files from one directory to another. I need to make sure that I do not lock the file making it unreadable by others. I am not sure if this is the default...
1
by: surimca2002 | last post by:
How do u copy multiple files from a lsbox to somewhere else? Does a CopyFile method work in this case? If so, how do I manage to copy multiple files? using vb.net pls give me solutions...
1
by: mridulmurari | last post by:
Hey. I need help with a certain problem I'm facing in VB6. I need to display the contents of multiple items in a listview. The situation is this : Everytime i double-click on an item in a...
8
by: jonathan184 | last post by:
Hi I am trying to copyfiles and dirs keeping there permissions intact and copy to multiple destinations which are on a data.txt file on the txt file i put test1 test2 So where ami going wrong?...
1
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
Using .NET 2.0 is it more efficient to copy files to a single folder versus spreading them across multiple folders. For instance if we have 100,000 files to be copied, Do we copy all of them to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.