List of usage examples for java.nio.file StandardOpenOption APPEND
StandardOpenOption APPEND
To view the source code for java.nio.file StandardOpenOption APPEND.
Click Source Link
From source file:com.romeikat.datamessie.core.base.util.FileUtil.java
public synchronized void appendToFile(final File file, final String content) { final Path path = FileSystems.getDefault().getPath(file.getAbsolutePath()); final Charset charset = Charset.defaultCharset(); try (BufferedWriter writer = Files.newBufferedWriter(path, charset, StandardOpenOption.APPEND)) { writer.write(String.format("%s%n", content)); } catch (final Exception e) { LOG.error("Could not apennd to file " + file.getAbsolutePath(), e); }/* w w w. j a v a2s . co m*/ }
From source file:org.apache.kylin.cube.inmemcubing.ConcurrentDiskStore.java
private void openWriteChannel(long startOffset) throws IOException { if (startOffset > 0) { // TODO does not support append yet writeChannel = FileChannel.open(diskFile.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE); } else {//from w w w . j a v a2 s. co m diskFile.delete(); writeChannel = FileChannel.open(diskFile.toPath(), StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE); } }
From source file:com.alibaba.jstorm.yarn.container.Executor.java
/** * set local conf/*from ww w. ja v a2 s . c om*/ * * @param key * @param value * @return */ public boolean setJstormConf(String key, String value) { //todo: how to set conf from a signal // could pull configuration file from nimbus String line = " " + key + ": " + value; try { Files.write(Paths.get("deploy/jstorm/conf/storm.yaml"), line.getBytes(), StandardOpenOption.APPEND); } catch (IOException e) { LOG.error(e); return false; } return true; }
From source file:deincraftlauncher.InstallController.java
private void saveConfig() { String newln = System.getProperty("line.separator"); String Texts[] = new String[2]; Texts[0] = "Deincraft Config 1.0"; Texts[1] = targetPath;//from www . jav a 2s.co m String toWrite = ""; for (String Text : Texts) { toWrite += Text + newln; } try { Files.write(Paths.get(config.toString()), toWrite.getBytes(), StandardOpenOption.APPEND); } catch (IOException ex) { System.err.println(ex); } }
From source file:de.elomagic.carafile.client.CaraFileClient.java
/** * Downloads a file into a {@link OutputStream}. * * @param md {@link MetaData} of the file. * @param out The output stream. It's not recommended to use a buffered stream. * @throws IOException Thrown when unable to write file into the output stream or the SHA-1 validation failed. *///w w w . j a v a2 s.c om public void downloadFile(final MetaData md, final OutputStream out) throws IOException { if (md == null) { throw new IllegalArgumentException("Parameter 'md' must not be null!"); } if (out == null) { throw new IllegalArgumentException("Parameter 'out' must not be null!"); } Map<String, Path> downloadedChunks = new HashMap<>(); Set<String> chunksToDownload = new HashSet<>(); for (ChunkData chunkData : md.getChunks()) { chunksToDownload.add(chunkData.getId()); } try { while (!chunksToDownload.isEmpty()) { PeerChunk pc = peerChunkSelector.getNext(md, chunksToDownload); if (pc == null || pc.getPeerURI() == null) { throw new IOException("No peer found or selected for download"); } Path chunkFile = Files.createTempFile("fs_", ".tmp"); try (OutputStream chunkOut = Files.newOutputStream(chunkFile, StandardOpenOption.APPEND)) { downloadShunk(pc, md, chunkOut); downloadedChunks.put(pc.getChunkId(), chunkFile); chunksToDownload.remove(pc.getChunkId()); chunkOut.flush(); } catch (Exception ex) { Files.deleteIfExists(chunkFile); throw ex; } } MessageDigest messageDigest = DigestUtils.getSha1Digest(); // Write chunk on correct order to file. try (DigestOutputStream dos = new DigestOutputStream(out, messageDigest); BufferedOutputStream bos = new BufferedOutputStream(dos, md.getChunkSize())) { for (ChunkData chunk : md.getChunks()) { Path chunkPath = downloadedChunks.get(chunk.getId()); Files.copy(chunkPath, bos); } } String sha1 = Hex.encodeHexString(messageDigest.digest()); if (!sha1.equalsIgnoreCase(md.getId())) { throw new IOException( "SHA1 validation of file failed. Expected " + md.getId() + " but was " + sha1); } } finally { for (Path path : downloadedChunks.values()) { try { Files.deleteIfExists(path); } catch (IOException ex) { LOG.error("Unable to delete chunk " + path.toString() + "; " + ex.getMessage(), ex); } } } }
From source file:io.spotnext.maven.mojo.TransformTypesMojo.java
private void trackExecution(String message) throws IllegalStateException { if (debug) {//from w w w. j a v a 2s .c o m try { File tempDir = FileUtils.getTempDirectory(); Files.write(Paths.get(tempDir.getAbsolutePath(), "transform-classes.log"), (new Date().toString() + ": " + message + "\n").getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.APPEND); } catch (Exception e) { throw new IllegalStateException("error", e); } } }
From source file:org.opencb.opencga.server.ws.FileWSServer.java
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation(httpMethod = "POST", position = 4, value = "Resource to upload a file by chunks", response = QueryResponse.class) public Response chunkUpload(@FormDataParam("chunk_content") byte[] chunkBytes, @FormDataParam("chunk_content") FormDataContentDisposition contentDisposition, @DefaultValue("") @FormDataParam("chunk_id") String chunk_id, @DefaultValue("false") @FormDataParam("last_chunk") String last_chunk, @DefaultValue("") @FormDataParam("chunk_total") String chunk_total, @DefaultValue("") @FormDataParam("chunk_size") String chunk_size, @DefaultValue("") @FormDataParam("chunk_hash") String chunkHash, @DefaultValue("false") @FormDataParam("resume_upload") String resume_upload, @ApiParam(value = "filename", required = true) @DefaultValue("") @FormDataParam("filename") String filename, @ApiParam(value = "fileFormat", required = true) @DefaultValue("") @FormDataParam("fileFormat") String fileFormat, @ApiParam(value = "bioFormat", required = true) @DefaultValue("") @FormDataParam("bioFormat") String bioFormat, @ApiParam(value = "userId", required = true) @DefaultValue("") @FormDataParam("userId") String userId, // @ApiParam(defaultValue = "projectId", required = true) @DefaultValue("") @FormDataParam("projectId") String projectId, @ApiParam(value = "studyId", required = true) @FormDataParam("studyId") String studyIdStr, @ApiParam(value = "relativeFilePath", required = true) @DefaultValue("") @FormDataParam("relativeFilePath") String relativeFilePath, @ApiParam(value = "description", required = true) @DefaultValue("") @FormDataParam("description") String description, @ApiParam(value = "parents", required = true) @DefaultValue("true") @FormDataParam("parents") boolean parents) { long t = System.currentTimeMillis(); java.nio.file.Path filePath = null; final int studyId; try {//from ww w . ja va2s .c o m studyId = catalogManager.getStudyId(studyIdStr); } catch (Exception e) { return createErrorResponse(e); } try { filePath = Paths.get(catalogManager.getFileUri(studyId, relativeFilePath)); System.out.println(filePath); } catch (CatalogIOException e) { System.out.println("catalogManager.getFilePath"); e.printStackTrace(); } catch (CatalogException e) { e.printStackTrace(); } java.nio.file.Path completedFilePath = filePath.getParent().resolve("_" + filename); java.nio.file.Path folderPath = filePath.getParent().resolve("__" + filename); logger.info(relativeFilePath + ""); logger.info(folderPath + ""); logger.info(filePath + ""); boolean resume = Boolean.parseBoolean(resume_upload); try { logger.info("---resume is: " + resume); if (resume) { logger.info("Resume ms :" + (System.currentTimeMillis() - t)); return createOkResponse(getResumeFileJSON(folderPath)); } int chunkId = Integer.parseInt(chunk_id); int chunkSize = Integer.parseInt(chunk_size); boolean lastChunk = Boolean.parseBoolean(last_chunk); logger.info("---saving chunk: " + chunkId); logger.info("lastChunk: " + lastChunk); // WRITE CHUNK TYPE_FILE if (!Files.exists(folderPath)) { logger.info("createDirectory(): " + folderPath); Files.createDirectory(folderPath); } logger.info("check dir " + Files.exists(folderPath)); // String hash = StringUtils.sha1(new String(chunkBytes)); // logger.info("bytesHash: " + hash); // logger.info("chunkHash: " + chunkHash); // hash = chunkHash; if (chunkBytes.length == chunkSize) { Files.write(folderPath.resolve(chunkId + "_" + chunkBytes.length + "_partial"), chunkBytes); } else { String errorMessage = "Chunk content size (" + chunkBytes.length + ") " + "!= chunk_size (" + chunk_size + ")."; logger.error(errorMessage); return createErrorResponse(new IOException(errorMessage)); } if (lastChunk) { logger.info("lastChunk is true..."); Files.deleteIfExists(completedFilePath); Files.createFile(completedFilePath); List<java.nio.file.Path> chunks = getSortedChunkList(folderPath); logger.info("----ordered chunks length: " + chunks.size()); for (java.nio.file.Path partPath : chunks) { logger.info(partPath.getFileName().toString()); Files.write(completedFilePath, Files.readAllBytes(partPath), StandardOpenOption.APPEND); } IOUtils.deleteDirectory(folderPath); try { QueryResult queryResult = catalogManager.createFile(studyId, File.Format.valueOf(fileFormat.toUpperCase()), File.Bioformat.valueOf(bioFormat.toUpperCase()), relativeFilePath, completedFilePath.toUri(), description, parents, sessionId); return createOkResponse(queryResult); } catch (Exception e) { logger.error(e.toString()); return createErrorResponse(e); } } } catch (IOException e) { System.out.println("e = " + e); // TODO Auto-generated catch block e.printStackTrace(); } logger.info("chunk saved ms :" + (System.currentTimeMillis() - t)); return createOkResponse("ok"); }
From source file:org.apache.nifi.controller.repository.VolatileContentRepository.java
@Override public long exportTo(final ContentClaim claim, final Path destination, final boolean append, final long offset, final long length) throws IOException { if (claim == null) { if (append) { return 0L; }//from w w w . java 2s . c om Files.createFile(destination); return 0L; } final StandardOpenOption openOption = append ? StandardOpenOption.APPEND : StandardOpenOption.CREATE; try (final InputStream in = read(claim); final OutputStream destinationStream = Files.newOutputStream(destination, openOption)) { if (offset > 0) { StreamUtils.skip(in, offset); } StreamUtils.copy(in, destinationStream, length); return length; } }
From source file:org.openbaton.vnfm.generic.GenericVNFM.java
private void saveLogToFile(VirtualNetworkFunctionRecord virtualNetworkFunctionRecord, String script, VNFCInstance vnfcInstance1, String output, boolean error) throws IOException { if (this.old > 0) { String path = ""; if (!error) { path = this.scriptsLogPath + virtualNetworkFunctionRecord.getName() + "/" + vnfcInstance1.getHostname() + ".log"; } else {/*from w w w . j ava2s.c o m*/ path = this.scriptsLogPath + virtualNetworkFunctionRecord.getName() + "/" + vnfcInstance1.getHostname() + "-error.log"; } File f = new File(path); if (!f.exists()) { f.getParentFile().mkdirs(); f.createNewFile(); } if (!error) { Files.write(Paths.get(path), ("Output of Script : " + script + "\n\n").getBytes(), StandardOpenOption.APPEND); Files.write(Paths.get(path), this.parser.fromJson(output, JsonObject.class).get("output") .getAsString().replaceAll("\\\\n", "\n").getBytes(), StandardOpenOption.APPEND); } else { Files.write(Paths.get(path), ("Error log of Script : " + script + "\n\n").getBytes(), StandardOpenOption.APPEND); Files.write(Paths.get(path), this.parser.fromJson(output, JsonObject.class).get("err").getAsString() .replaceAll("\\\\n", "\n").getBytes(), StandardOpenOption.APPEND); } Files.write(Paths.get(path), "\n\n\n~~~~~~~~~~~~~~~~~~~~~~~~~\n#########################\n~~~~~~~~~~~~~~~~~~~~~~~~~\n\n\n" .getBytes(), StandardOpenOption.APPEND); } }
From source file:org.opencb.opencga.server.rest.FileWSServer.java
@POST @Path("/upload") @Consumes(MediaType.MULTIPART_FORM_DATA) @ApiOperation(httpMethod = "POST", position = 4, value = "Resource to upload a file by chunks", response = File.class) public Response upload(@FormDataParam("chunk_content") byte[] chunkBytes, @FormDataParam("chunk_content") FormDataContentDisposition contentDisposition, @FormDataParam("file") InputStream fileInputStream, @FormDataParam("file") FormDataContentDisposition fileMetaData, @DefaultValue("") @FormDataParam("chunk_id") String chunk_id, @DefaultValue("false") @FormDataParam("last_chunk") String last_chunk, @DefaultValue("") @FormDataParam("chunk_total") String chunk_total, @DefaultValue("") @FormDataParam("chunk_size") String chunk_size, @DefaultValue("") @FormDataParam("chunk_hash") String chunkHash, @DefaultValue("false") @FormDataParam("resume_upload") String resume_upload, @ApiParam(value = "filename", required = false) @FormDataParam("filename") String filename, @ApiParam(value = "fileFormat", required = true) @DefaultValue("") @FormDataParam("fileFormat") String fileFormat, @ApiParam(value = "bioformat", required = true) @DefaultValue("") @FormDataParam("bioformat") String bioformat, // @ApiParam(value = "userId", required = true) @DefaultValue("") @FormDataParam("userId") String userId, // @ApiParam(defaultValue = "projectId", required = true) @DefaultValue("") @FormDataParam("projectId") String projectId, @ApiParam(value = "studyId", required = true) @FormDataParam("studyId") String studyIdStr, @ApiParam(value = "Path within catalog where the file will be located (default: root folder)", required = false) @DefaultValue(".") @FormDataParam("relativeFilePath") String relativeFilePath, @ApiParam(value = "description", required = false) @DefaultValue("") @FormDataParam("description") String description, @ApiParam(value = "Create the parent directories if they do not exist", required = false) @DefaultValue("true") @FormDataParam("parents") boolean parents) { long t = System.currentTimeMillis(); if (relativeFilePath.endsWith("/")) { relativeFilePath = relativeFilePath.substring(0, relativeFilePath.length() - 1); }// w w w. ja v a 2s . c o m if (relativeFilePath.startsWith("/")) { return createErrorResponse(new CatalogException("The path cannot be absolute")); } java.nio.file.Path filePath = null; final long studyId; try { studyId = catalogManager.getStudyId(studyIdStr, sessionId); } catch (Exception e) { return createErrorResponse(e); } try { filePath = Paths.get(catalogManager.getFileUri(studyId, relativeFilePath)); System.out.println(filePath); } catch (CatalogIOException e) { System.out.println("catalogManager.getFilePath"); e.printStackTrace(); } catch (CatalogException e) { e.printStackTrace(); } if (chunkBytes != null) { java.nio.file.Path completedFilePath = filePath.getParent().resolve("_" + filename); java.nio.file.Path folderPath = filePath.getParent().resolve("__" + filename); logger.info(relativeFilePath + ""); logger.info(folderPath + ""); logger.info(filePath + ""); boolean resume = Boolean.parseBoolean(resume_upload); try { logger.info("---resume is: " + resume); if (resume) { logger.info("Resume ms :" + (System.currentTimeMillis() - t)); return createOkResponse(getResumeFileJSON(folderPath)); } int chunkId = Integer.parseInt(chunk_id); int chunkSize = Integer.parseInt(chunk_size); boolean lastChunk = Boolean.parseBoolean(last_chunk); logger.info("---saving chunk: " + chunkId); logger.info("lastChunk: " + lastChunk); // WRITE CHUNK TYPE_FILE if (!Files.exists(folderPath)) { logger.info("createDirectory(): " + folderPath); Files.createDirectory(folderPath); } logger.info("check dir " + Files.exists(folderPath)); // String hash = StringUtils.sha1(new String(chunkBytes)); // logger.info("bytesHash: " + hash); // logger.info("chunkHash: " + chunkHash); // hash = chunkHash; if (chunkBytes.length == chunkSize) { Files.write(folderPath.resolve(chunkId + "_" + chunkBytes.length + "_partial"), chunkBytes); } else { String errorMessage = "Chunk content size (" + chunkBytes.length + ") " + "!= chunk_size (" + chunk_size + ")."; logger.error(errorMessage); return createErrorResponse(new IOException(errorMessage)); } if (lastChunk) { logger.info("lastChunk is true..."); Files.deleteIfExists(completedFilePath); Files.createFile(completedFilePath); List<java.nio.file.Path> chunks = getSortedChunkList(folderPath); logger.info("----ordered chunks length: " + chunks.size()); for (java.nio.file.Path partPath : chunks) { logger.info(partPath.getFileName().toString()); Files.write(completedFilePath, Files.readAllBytes(partPath), StandardOpenOption.APPEND); } IOUtils.deleteDirectory(folderPath); try { QueryResult<File> queryResult = catalogManager.createFile(studyId, File.Format.valueOf(fileFormat.toUpperCase()), File.Bioformat.valueOf(bioformat.toUpperCase()), relativeFilePath, completedFilePath.toUri(), description, parents, sessionId); File file = new FileMetadataReader(catalogManager).setMetadataInformation( queryResult.first(), null, new QueryOptions(queryOptions), sessionId, false); queryResult.setResult(Collections.singletonList(file)); return createOkResponse(queryResult); } catch (Exception e) { logger.error(e.toString()); return createErrorResponse(e); } } } catch (IOException e) { System.out.println("e = " + e); // TODO Auto-generated catch block e.printStackTrace(); } logger.info("chunk saved ms :" + (System.currentTimeMillis() - t)); return createOkResponse("ok"); } else if (fileInputStream != null) { logger.info("filePath: {}", filePath.toString()); // We obtain the basic studyPath where we will upload the file temporarily java.nio.file.Path studyPath = null; try { studyPath = Paths.get(catalogManager.getStudyUri(studyId)); } catch (CatalogException e) { e.printStackTrace(); return createErrorResponse("Upload file", e.getMessage()); } if (filename == null) { filename = fileMetaData.getFileName(); } java.nio.file.Path tempFilePath = studyPath.resolve("tmp_" + filename).resolve(filename); logger.info("tempFilePath: {}", tempFilePath.toString()); logger.info("tempParent: {}", tempFilePath.getParent().toString()); // Create the temporal directory and upload the file try { if (!Files.exists(tempFilePath.getParent())) { logger.info("createDirectory(): " + tempFilePath.getParent()); Files.createDirectory(tempFilePath.getParent()); } logger.info("check dir " + Files.exists(tempFilePath.getParent())); // Start uploading the file to the temporal directory int read; byte[] bytes = new byte[1024]; // Upload the file to a temporary folder OutputStream out = new FileOutputStream(new java.io.File(tempFilePath.toString())); while ((read = fileInputStream.read(bytes)) != -1) { out.write(bytes, 0, read); } out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } // Register the file in catalog try { String destinationPath; // Check if the relativeFilePath is not the root folder if (relativeFilePath.length() > 1 && !relativeFilePath.equals("./")) { try { // Create parents directory if necessary catalogManager.createFolder(studyId, Paths.get(relativeFilePath), parents, null, sessionId); } catch (CatalogException e) { logger.debug("The folder {} already exists", relativeFilePath); } destinationPath = Paths.get(relativeFilePath).resolve(filename).toString(); } else { destinationPath = filename; } logger.debug("Relative path: {}", relativeFilePath); logger.debug("Destination path: {}", destinationPath); logger.debug("File name {}", filename); // Register the file and move it to the proper directory QueryResult<File> queryResult = catalogManager.createFile(studyId, File.Format.valueOf(fileFormat.toUpperCase()), File.Bioformat.valueOf(bioformat.toUpperCase()), destinationPath, tempFilePath.toUri(), description, parents, sessionId); File file = new FileMetadataReader(catalogManager).setMetadataInformation(queryResult.first(), null, new QueryOptions(queryOptions), sessionId, false); queryResult.setResult(Collections.singletonList(file)); // Remove the temporal directory Files.delete(tempFilePath.getParent()); return createOkResponse(queryResult); } catch (CatalogException e) { e.printStackTrace(); return createErrorResponse("Upload file", e.getMessage()); } catch (IOException e) { e.printStackTrace(); return createErrorResponse("Upload file", e.getMessage()); } } else { return createErrorResponse("Upload file", "No file or chunk found"); } }