Results 1 to 8 of 8
  1. #1
    kewlkeny is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default How to Merge and split a file

    Hello, Can any one please tell me how to merge two text files into a another text file and how to split a huge data text file in to small chunks ? Thnank you :)

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: How to Merge and split a file

    Read both files, create a new file and write the content from both to it.

    Or in the second case, read the content of one file, query the length, copy the first half to one string and the second half to another string and then write both strings to separate files.

    You could use FileReader and FileWriter to read and write to a file.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: How to Merge and split a file

    For the second case, assuming you have some sort of idea of where you are breaking (some specific bit of text to look for, or number of lines) then read and write at the same time. There is no need to store more than a single line in memory for that job.
    Java Code:
    Open file reader
    Open file writer
    for each line {
        write line
        if current output is complete {
            close file writer
            open new file writer (to new file)
        }
    }
    close file writer
    clode file reader

  4. #4
    kewlkeny is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to Merge and split a file

    Thank you for the reply .

    Can you please tell me how to merge and split the files with the same memory size, for example i have a few no of file with different sizes, now i want merge and split them into same memory !!!

    Thank you

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: How to Merge and split a file

    Without knowing why, or what it is you are actually trying to do, or the contents of the files, that's almost impossible to answer.

    You can get the size of a file (length()) and use that if you read byte by byte. However, that'll probably destroy any useful information in the file as few things neatly fit into a single byte.

  6. #6
    clydedoris is offline Member
    Join Date
    Feb 2010
    Posts
    80
    Rep Power
    0

    Default Re: How to Merge and split a file

    this is some kind of weird programming task. would you also mind sharing what you are trying to accomplish?
    [why are you annoyed with my sig?]

  7. #7
    kewlkeny is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to Merge and split a file

    public class FileSpltr {

    static Logger logger = LoggerFactory.getLogger(FileSpltr.class);

    public void FileSplitter(String source, String destination, long size) throws IOException{

    File file = new File(source);
    FileInputStream fis = new FileInputStream(file);
    long fsize = file.length();
    logger.debug("main file size is" + " " + fsize);

    long sepsize = fsize / size;
    long mod = fsize % size;
    if (mod != 0) {
    sepsize++;
    }
    for (int i = 0; i < sepsize; i++) {
    String str;
    str = i + " ";
    FileOutputStream fos = new FileOutputStream(
    destination + str + ".");

    logger.info("FOS" + destination);

    out: for (long j = 0; j < size; j++) {
    int ch;
    ch = fis.read();
    if (ch == -1)
    break out;
    fos.write(ch);

    }
    fos.close();
    }
    fis.close();

    logger.info("Splitted the file to splitted Folder");
    }
    }


    This is the code it will work to split the file in equal size of chunks
    Last edited by kewlkeny; 02-02-2012 at 06:19 AM.

  8. #8
    kewlkeny is offline Member
    Join Date
    May 2011
    Posts
    13
    Rep Power
    0

    Default Re: How to Merge and split a file

    And for merging of files

    public class FileMergr {
    static Logger logger = LoggerFactory.getLogger(FileMergr.class);

    public void mergeFiles(String source, String destin) throws IOException{
    PrintWriter pw = new PrintWriter(new FileOutputStream(
    destin));

    File file = new File(source);
    File[] files = file.listFiles();
    for (int i = 0; i < files.length; i++) {

    logger.debug("Processing " + files[i].getPath() + "... ");
    BufferedReader br = new BufferedReader(new FileReader(
    files[i].getPath()));
    String line = br.readLine();
    while (line != null) {
    pw.println(line);
    line = br.readLine();
    }

    br.close();
    }
    pw.close();
    logger.info("Merged the files of temporary folder to Merged Folder"
    + destin);
    }
    }

Similar Threads

  1. Merge file
    By skidrow in forum Advanced Java
    Replies: 12
    Last Post: 06-14-2011, 06:24 PM
  2. how to split large xml file into small xml file in java
    By enggvijaysingh@gmail.com in forum XML
    Replies: 2
    Last Post: 02-07-2011, 09:34 AM
  3. How to split a file into 2?
    By syntrax in forum New To Java
    Replies: 3
    Last Post: 09-26-2009, 06:28 AM
  4. how to split a file
    By nagaraaju in forum New To Java
    Replies: 0
    Last Post: 03-14-2008, 08:45 AM
  5. PDF Split and Merge 0.7 beta 1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 06-24-2007, 08:46 AM

Posting Permissions

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