CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2010
    Posts
    4

    java file copying

    hai
    can any body just tell me how to copy paragraph by paragraph of one file to different files at a time in java? please help me in coding

    thank you

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java file copying

    1. Open the file to read in.
    2. Open a file to write to.
    3. Repeatedly read in a line and write it to the output file until the end of paragraph or end of file is found.
    4. Close the output file.
    5. If the end of paragraph was found go back to step 2.
    6. Close the input file.

  3. #3
    Join Date
    Jan 2010
    Posts
    4

    Re: java file copying

    can anyone please help me with the code?

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: java file copying

    Certainly. Post up the code you've got so far, and explain which part you're stuck on and we'll help you fix it.

    Programming is an explanatory activity...
    R. Harper
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Jan 2010
    Posts
    4

    Re: java file copying

    # 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();
    # //String s;
    # 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;
    #
    for(int i=0;sFile=="\n\n";i++){
    #
    #
    #
    # for(String targetFile : targetFiles){
    # new CopierThread(sFileBytes,targetFile[i]).run();
    #
    # }
    }
    # sFileInputStream.close();
    # }
    #
    # 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());
    # }
    # }
    # }
    #
    # }

  6. #6
    Join Date
    Jan 2010
    Posts
    4

    Re: java file copying

    please help me out

  7. #7
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: java file copying

    Ok, so that's your code. What part isn't working?

    The more clearly you explain what you want to happen and what is actually happening (remembering to include all error messages and stack traces) the more likely someone is to be able to help you.

    Posting the code in code tags so it stays formatted would also help.

  8. #8
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: java file copying

    Straightaway I can see a problem - all your code lines start with a '#', so none of it is valid Java.

    If you want us to make the effort to help you, you're going to have to make a reasonable effort to help us do that. Posting valid Java code that (preferably) compiles is a good start. Explaining exactly what you're stuck on is essential.

    Eighty percent of success is showing up...
    W. Allen
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured