Results 1 to 8 of 8
Thread: How to Merge and split a file
- 01-04-2012, 07:32 AM #1
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
-
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.
- 01-04-2012, 11:09 AM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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
- 01-05-2012, 07:14 AM #4
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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
- 01-05-2012, 10:55 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 01-06-2012, 05:49 AM #6
Member
- Join Date
- Feb 2010
- Posts
- 80
- Rep Power
- 0
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?]
- 02-02-2012, 06:16 AM #7
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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 chunksLast edited by kewlkeny; 02-02-2012 at 06:19 AM.
- 02-02-2012, 06:22 AM #8
Member
- Join Date
- May 2011
- Posts
- 13
- Rep Power
- 0
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
-
Merge file
By skidrow in forum Advanced JavaReplies: 12Last Post: 06-14-2011, 06:24 PM -
how to split large xml file into small xml file in java
By enggvijaysingh@gmail.com in forum XMLReplies: 2Last Post: 02-07-2011, 09:34 AM -
How to split a file into 2?
By syntrax in forum New To JavaReplies: 3Last Post: 09-26-2009, 06:28 AM -
how to split a file
By nagaraaju in forum New To JavaReplies: 0Last Post: 03-14-2008, 08:45 AM -
PDF Split and Merge 0.7 beta 1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 06-24-2007, 08:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks