Note that there are some explanatory texts on larger screens.

plurals
  1. POlosing data while writing through asynchronousFileChannel in java
    primarykey
    data
    text
    <p>I am trying to use asynchronousFileChannel to write the date into a text file. I made 3 jar file of the program with the AsynchronousFileChannel and compiled all 3 jars simultaneously through command prompt to read 3 different text files and output to one common temporary file</p> <p>I have 2000 records in my test files(3) to be read,but the output in the common temporary file is missing some of the records,the output should have 6000 records but it shows only 5366 or 5666 or sometimes less than that.</p> <p>I am not able to figure out why some data is lost as it is the functionality of a asynchronousFileChannel.<br> Here is the code for the java program using asynchronousfilechannel.</p> <pre><code> class Writer(){ public void writeOut(ReadableData fileData) throws InterruptedException { Path file = null; AsynchronousFileChannel asynchFileChannel = null; String filePath = tempFileName; try { file = Paths.get(filePath); asynchFileChannel = AsynchronousFileChannel.open(file, StandardOpenOption.WRITE, StandardOpenOption.CREATE); CompletionHandler&lt;Integer, Object&gt; handler = new CompletionHandler&lt;Integer, Object&gt;() { @Override public void completed(Integer result, Object attachment) { if (result == Integer.MAX_VALUE) { log.debug("Attachment: " + attachment + " " + result + " bytes written"); log.debug("CompletionHandler Thread ID: " + Thread.currentThread().getId()); } result++; } @Override public void failed(Throwable e, Object attachment) { try { throw e; } catch (Throwable e1) { e1.printStackTrace(); } log.debug("File Write Failed Exception:"); e.printStackTrace(); } }; String printData = fileData.getId() + "|" + fileData.getName() + "|" + fileData.getEmpId() + "|" + fileData.getServieId() + "|" + "\n"; asynchFileChannel.write(ByteBuffer.wrap(printData.getBytes()), asynchFileChannel.size(), "file write", handler); log.debug(printData); } } catch (IOException e) { e.printStackTrace(); log.error(e.getMessage()); } finally { } } </code></pre> <p>} }</p> <p>and this is my class to read data from 3 files:</p> <pre><code> public class FileReader1 { static Logger log = Logger.getLogger(FileHandlerNorthBoundMain.class .getName()); Writer wrO=new Writer(); public static void main(String[] args) throws IOException, IllegalFileFormatException, InterruptedException { String filePath = "C:\\Users\\Public\\testdata1.csv"; //"C:\\Users\\Public\\testdata2.csv"; "C:\\Users\\Public\\testdata3.csv"; File file = new File(filePath); log.info("Fetching data.... from: " + filePath); ArrayList&lt;ReadableData&gt; list = new ArrayList&lt;ReadableData&gt;(); FileInputStream fs = null; BufferedReader reader = null; String Name; int Id, EmpId, ServiceId; ReadableData readableData = null; int count = 0; fs = new FileInputStream(file); reader = new BufferedReader(new InputStreamReader(fs)); String line = reader.readLine(); while (line != null) { StringTokenizer st = new StringTokenizer(line, "\\|"); while (st.hasMoreTokens()) { try { Id = Integer.parseInt(st.nextToken()); Name = st.nextToken(); EmpId = Integer.parseInt(st.nextToken()); ServiceId = Integer.parseInt(st.nextToken()); readableData = new ReadableData(Id, , Name, EmpId,ServiceId); wrO.writeOut(readableData); list.add(count, readableData); count = count++; } catch (Exception ex) { log.error("Illegal File Format"); throw new IllegalFileFormatException("Illegal File Format"); } } line = reader.readLine(); } reader.close(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload