0
public List<BatchInfo> bulkUpsertAccountShare(obj gsr) throws UnsupportedEncodingException, IOException, AsyncApiException
{
    log.info("BulkUpsertAccountShare!:");
    SForceConnector sfc = new SForceConnector();
    if ( ! sfc.login2()) {

        return null;

      }
    String title = gsr.getName()+"_AccountShare.csv";

    JobInfo job = new JobInfo();
    job.setObject("AccountShare");
    job.setOperation(OperationEnum.upsert);
    job.setConcurrencyMode(ConcurrencyMode.Parallel);
    job.setContentType(ContentType.CSV);
    job.setExternalIdFieldName("id");
    job = sfc.getBulkConnection().createJob(job);
    List<BatchInfo> batchInfos = new ArrayList<BatchInfo>();
    BufferedReader rdr = new BufferedReader(new InputStreamReader(new FileInputStream(title)));
    byte[] headerBytes = (rdr.readLine() + "\n").getBytes("UTF-8");
    int headerBytesLength = headerBytes.length;
    File tmpFile = File.createTempFile("bulkAPIInsert", ".csv");

    try {
        FileOutputStream tmpOut = new FileOutputStream(tmpFile);
        int maxBytesPerBatch = 10000000; // 10 million bytes per batch
        int maxRowsPerBatch = 10000; // 10 thousand rows per batch
        int currentBytes = 0;
        int currentLines = 0;
        String nextLine;
        while ((nextLine = rdr.readLine()) != null) {
            byte[] bytes = (nextLine + "\n").getBytes("UTF-8");
            // Create a new batch when our batch size limit is reached
            if (currentBytes + bytes.length > maxBytesPerBatch
              || currentLines > maxRowsPerBatch) {
                createBatch(tmpOut, tmpFile, batchInfos, sfc.getBulkConnection(), job);
                currentBytes = 0;
                currentLines = 0;
            }
            if (currentBytes == 0) {
                tmpOut = new FileOutputStream(tmpFile);
                tmpOut.write(headerBytes);
                currentBytes = headerBytesLength;
                currentLines = 1;
            }
            tmpOut.write(bytes);
            currentBytes += bytes.length;
            currentLines++;
        }
        // Finished processing all rows
        // Create a final batch for any remaining data
        if (currentLines > 1) {
            createBatch(tmpOut, tmpFile, batchInfos, sfc.getBulkConnection(), job);
        }
    } finally {
        tmpFile.delete();
    }

    long sleepTime = 0L;
    Set<String> incomplete = new HashSet<String>();
    for (BatchInfo bi : batchInfos) {
        incomplete.add(bi.getId());
    }
    while (!incomplete.isEmpty()) {
        try {
            Thread.sleep(sleepTime);
        } catch (InterruptedException e) {}
        log.info("Awaiting results..." + incomplete.size());
        sleepTime = 10000L;
        BatchInfo[] statusList =
                sfc.getBulkConnection().getBatchInfoList(job.getId()).getBatchInfo();
        for (BatchInfo b : statusList) {
            if (b.getState() == BatchStateEnum.Completed
              || b.getState() == BatchStateEnum.Failed) {
                if (incomplete.remove(b.getId())) {
                    log.info("BATCH STATUS:\n" + b);
                }
            }
        }
    }



    for (BatchInfo b : batchInfos) {
        CSVReader rdr2 =new CSVReader(sfc.getBulkConnection().getBatchResultStream(job.getId(), b.getId()));
        List<String> resultHeader = rdr2.nextRecord();
        int resultCols = resultHeader.size();

        List<String> row;
        while ((row = rdr2.nextRecord()) != null) {
            Map<String, String> resultInfo = new HashMap<String, String>();
            for (int i = 0; i < resultCols; i++) {
                resultInfo.put(resultHeader.get(i), row.get(i));
            }
            boolean success = Boolean.valueOf(resultInfo.get("Success"));
            boolean created = Boolean.valueOf(resultInfo.get("Created"));
            String id = resultInfo.get("Id");
            String error = resultInfo.get("Error");
            if (success && created) {
           //     log.info("Created row with id " + id);
            } else if (!success) {
           //     log.info("Failed with error: " + error);
            }
        }
    }

    Path source = Paths.get(title);
    Files.move(source, source.resolveSibling(gsr.getName()+"_AccountShare_Processed.csv"));
    return batchInfos;
}

I want to rename my csv after its processed, but I'm getting

"The process cannot access the file because it is being used by another process."

error. I'm not too familiar with java, so I'm not entirely sure what to do to address this issue. Thanks.

closed as off-topic by Martin Lezer, Santanu Boral, Mohith Shrivastava, Lance Shi, Ratan Paul Mar 22 '17 at 6:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – Martin Lezer, Santanu Boral, Mohith Shrivastava, Lance Shi, Ratan Paul
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Seems like a better fit for Stack Overflow... – Adrian Larson Mar 21 '17 at 16:37
  • I agree with Adrian here, Salesforce is only incidental to your actual problem, which lies with Java. – Derek F Mar 21 '17 at 16:40
2

You have to close the file before attempting to rename it. Something like:

CSVReader.Close();

docs

Not the answer you're looking for? Browse other questions tagged or ask your own question.