with out for loop read a file by using parallel thread or executors service
- package com.kartik.file.write;
import java.io.Serializable;
public class Range implements Serializable{
private static final long serialVersionUID = 1L;
private long start;
private long end;
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
public long getEnd() {
return end;
}
public void setEnd(long end) {
this.end = end;
}
}
- <?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
- package com.kartik.xml.to.xml;
package com.kartik.file.write;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
public class FileWrite implements Callable<File> {
long start;
long end;
int threadNo;
File file;
File mainFile=new File("C:\\Users\\kmandal\\Desktop\\test" + 5+ ".txt");
public long getStart() {
return start;
}
public void setStart(long start) {
this.start = start;
}
public long getEnd() {
return end;
}
public void setEnd(long end) {
this.end = end;
}
public int getThreadNo() {
return threadNo;
}
public void setThreadNo(int threadNo) {
this.threadNo = threadNo;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public File getMainFile() {
return mainFile;
}
public void setMainFile(File mainFile) {
this.mainFile = mainFile;
}
FileWrite() {
}
FileWrite(long start, long end, int threadNo, File file) {
this.start = start;
this.end = end;
this.threadNo = threadNo;
this.file = file;
}
@Override
public File call() throws Exception {
FileWrite fff = new FileWrite();
return fff.readFile(file, start, end, threadNo);
}
@SuppressWarnings("resource")
public File readFile(File file, long start, long end, int threadNo) {
System.out.println("------>>>>>"+threadNo);
FileInputStream fin = null;
OutputStream opStream = null;
String strFileContent = null;
String fileName = "C:\\Users\\kmandal\\Desktop\\test" + threadNo
+ ".txt";
File myFile = null;
try {
fin = new FileInputStream(file);
/*
* Create byte array large enough to hold the content of the file.
* Use File.length to determine size of the file in bytes.
*/
byte fileContent[] = new byte[(int) file.length()];
/*
* To read content of the file in byte array, use int read(byte[]
* byteArray) method of java FileInputStream class.
*/
fin.read(fileContent); // read file into bytes[]
byte[] fileContent2 = Arrays.copyOfRange(fileContent, (int) start,
(int) end);
fin.read(fileContent2);
strFileContent = new String(fileContent2); // read file with out for
// loop
byte[] byteContent = strFileContent.getBytes();
//create a new file
myFile = new File(fileName);
// check if file exist, otherwise create the file before writing
if (!myFile.exists()) {
myFile.createNewFile();
}
opStream = new FileOutputStream(myFile);
opStream.write(byteContent);
opStream.flush();
//System.out.println("File content : ");
//System.out.println(strFileContent);
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the file " + ioe);
} finally {
try {
if (opStream != null)
opStream.close();
} catch (Exception ex) {
}
}
return myFile;
}
@SuppressWarnings("resource")
public File readSimpleFile(File file) {
System.out.println("------>>>>>"+threadNo);
FileInputStream fin = null;
OutputStream opStream = null;
String strFileContent = null;
String fileName = "C:\\Users\\kmandal\\Desktop\\test" + 4
+ ".txt";
File myFile = null;
try {
fin = new FileInputStream(file);
/*
* Create byte array large enough to hold the content of the file.
* Use File.length to determine size of the file in bytes.
*/
byte fileContent[] = new byte[(int) file.length()];
/*
* To read content of the file in byte array, use int read(byte[]
* byteArray) method of java FileInputStream class.
*/
fin.read(fileContent); // read file into bytes[]
strFileContent = new String(fileContent); // read file with out for
// loop
byte[] byteContent = strFileContent.getBytes();
//create a new file
myFile = new File(fileName);
// check if file exist, otherwise create the file before writing
if (!myFile.exists()) {
myFile.createNewFile();
}
opStream = new FileOutputStream(myFile);
opStream.write(byteContent);
opStream.flush();
//System.out.println("File content : ");
//System.out.println(strFileContent);
} catch (FileNotFoundException e) {
System.out.println("File not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the file " + ioe);
} finally {
try {
if (opStream != null)
opStream.close();
} catch (Exception ex) {
}
}
return myFile;
}
/**
*
* @param files
* @param mergedFile
*/
public void mergeFiles(List<File> files) {
String strFileContent = null;
OutputStream opStream = getOutputStream();
for (File f : files) {
FileInputStream fis;
try {
fis = new FileInputStream(f);
byte fileContent[] = new byte[(int) f.length()];
fis.read(fileContent);
strFileContent = new String(fileContent);
byte[] byteContent = strFileContent.getBytes();
opStream.write(byteContent);
opStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public OutputStream getOutputStream(){
OutputStream opStream = null;
try {
opStream = new FileOutputStream(mainFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return opStream;
}
}
package com.kartik.file.write; import java.io.File; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class MultiTaskingDemo { public static void main(String args[]) { long timeStartFuture = Calendar.getInstance().getTimeInMillis(); int threadNum = 4; ExecutorService executor = Executors.newFixedThreadPool(threadNum); List<Future<File>> list = new ArrayList<Future<File>>(); long start = 0l; File file = new File("C:\\Users\\kmandal\\Desktop\\FinancialAlert.xml"); for (int threadNo = 0; threadNo < threadNum; threadNo++) { Range range = divsior(file, threadNum, start); Callable<File> callable = new FileWrite(range.getStart(), range.getEnd(), threadNo, file); Future<File> future = executor.submit(callable); list.add(future); start = range.getEnd(); } List<File> ff=new ArrayList<File>(); for (Future<File> future : list) { try { File e = future.get(); ff.add(e); System.out.println("File content length:---->>>: " + e.length()+""); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } FileWrite fffff=new FileWrite(); fffff.mergeFiles(ff); executor.shutdown(); long timeEndFuture = Calendar.getInstance().getTimeInMillis(); long timeNeededFuture = timeEndFuture - timeStartFuture; System.out.println("Result (Future): " + " calculated in " + timeNeededFuture + " ms"); timeStartFuture = Calendar.getInstance().getTimeInMillis(); FileWrite fwriter= new FileWrite(); fwriter.readSimpleFile(file); timeEndFuture = Calendar.getInstance().getTimeInMillis(); timeNeededFuture = timeEndFuture - timeStartFuture; System.out.println("Result (With out Future): " + " calculated in " + timeNeededFuture + " ms"); } public static Range divsior(File file, int div, long start) { long size = file.length(); long end = start + (size / div); Range r = new Range(); r.setStart(start); r.setEnd(end); return r; } }