List of usage examples for java.nio.file StandardOpenOption TRUNCATE_EXISTING
StandardOpenOption TRUNCATE_EXISTING
To view the source code for java.nio.file StandardOpenOption TRUNCATE_EXISTING.
Click Source Link
From source file:org.wso2.carbon.apimgt.core.util.APIFileUtils.java
/** * Writes the string content to a file// w w w. j a v a2 s . c om * * @param path path of the file to be written. * @param content Content to be written. * @throws APIMgtDAOException if an error occurs while writing to file */ public static void writeToFile(String path, String content) throws APIMgtDAOException { try { Files.write(Paths.get(path), content.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } catch (IOException e) { String msg = "I/O error while writing to file at: " + path; log.error(msg, e); throw new APIMgtDAOException(msg, e); } }
From source file:nl.salp.warcraft4j.casc.cdn.util.CascFileExtractor.java
private Optional<Path> extractFile(Path destination, long filenameHash) throws CascExtractionException, DataReadingException, DataParsingException { Optional<Path> file; if (isWritableFile(destination)) { try {// w w w . j av a 2 s. c om Files.createDirectories(destination.getParent()); try (DataReader in = context.getFileDataReader(filenameHash); OutputStream out = Files.newOutputStream(destination, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) { while (in.hasRemaining()) { int chunkSize = (int) Math.min(CHUNK_SIZE, in.remaining()); byte[] chunk = in.readNext(DataTypeFactory.getByteArray(chunkSize)); out.write(chunk); } out.flush(); file = Optional.of(destination); } catch (CascEntryNotFoundException e) { file = Optional.empty(); } } catch (IOException e) { throw new CascExtractionException( format("Error while extraction CASC file to %s: %s", destination, e.getMessage()), e); } } else { throw new CascExtractionException(format( "Unable to extract a file to %s, the path already exists and is not a file or not writable.", destination)); } return file; }
From source file:it.greenvulcano.configuration.BaseConfigurationManager.java
@Override public void install(String name, byte[] archive) throws IOException { Path configurationPath = getConfigurationPath(name); try (ZipInputStream zipArchive = new ZipInputStream(new ByteArrayInputStream(archive))) { if (zipArchive.getNextEntry() != null) { Files.write(configurationPath, archive, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); } else {// www . java 2s . c om throw new IOException("Empty or invalid zip archive"); } } }
From source file:org.cryptomator.webdav.jackrabbit.DavLocatorFactoryImpl.java
@Override public void writePathSpecificMetadata(String encryptedPath, byte[] encryptedMetadata) throws IOException { final Path metaDataFile = fsRoot.resolve(encryptedPath); Files.write(metaDataFile, encryptedMetadata, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.DSYNC); }
From source file:neembuu.uploader.v2tov3conversion.ConvertUploaderClass.java
public void writeTo(Path out) throws IOException { Files.write(out, out_lines, Charset.defaultCharset(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); }
From source file:org.kalypso.grid.BinaryGeoGrid.java
/** * Crates a new grid file with the given size and scale.<br> * The grid is then opened in write mode, so its values can then be set.<br> * The grid must be disposed afterwards in order to flush the written information. * * //from ww w.j av a 2 s .co m * @param fillGrid * If set to <code>true</code>, the grid will be initially filled with no-data values. Else, the grid values * are undetermined. */ public static BinaryGeoGrid createGrid(final File file, final int sizeX, final int sizeY, final int scale, final Coordinate origin, final Coordinate offsetX, final Coordinate offsetY, final String sourceCRS, final boolean fillGrid) throws GeoGridException { try { final FileChannel channel = FileChannel.open(file.toPath(), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); return new BinaryGeoGrid(channel, sizeX, sizeY, scale, origin, offsetX, offsetY, sourceCRS, fillGrid); } catch (final IOException e) { throw new GeoGridException("Could not find binary grid file: " + file.getAbsolutePath(), e); } }
From source file:com.gmt2001.datastore.IniStore.java
private void SaveFile(String fName, IniFile data) { if (data == null) { return;// ww w. jav a2 s. c om } try { String wdata = ""; Object[] adata = data.data.keySet().toArray(); Object[] akdata; Object[] avdata; for (int i = 0; i < adata.length; i++) { if (i > 0) { wdata += "\r\n"; } if (!((String) adata[i]).equals("")) { wdata += "[" + ((String) adata[i]) + "]\r\n"; } akdata = data.data.get(((String) adata[i])).keySet().toArray(); avdata = data.data.get(((String) adata[i])).values().toArray(); for (int b = 0; b < akdata.length; b++) { wdata += ((String) akdata[b]) + "=" + ((String) avdata[b]) + "\r\n"; } } if (!Files.isDirectory(Paths.get("./" + inifolder + "/"))) { Files.createDirectory(Paths.get("./" + inifolder + "/")); } Files.write(Paths.get("./" + inifolder + "/" + fName + ".ini"), wdata.getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); changed.remove(fName); } catch (IOException ex) { com.gmt2001.Console.err.printStackTrace(ex); } }
From source file:org.apache.bookkeeper.common.conf.ConfigDef.java
public void save(Path path) throws IOException { try (OutputStream stream = Files.newOutputStream(path, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) { save(stream);/*from w w w . j av a 2 s . c om*/ } }
From source file:at.ac.tuwien.infosys.util.ImageUtil.java
private void saveFile(URL url, Path file) throws IOException { ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileChannel channel = FileChannel.open(file, EnumSet.of(StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)); channel.transferFrom(rbc, 0, Long.MAX_VALUE); channel.close();/*from w w w . j av a 2s . co m*/ }
From source file:org.tinywind.springi18nconverter.converter.ExcelConverter.java
@Override @SuppressWarnings("unchecked") public void decode(File sourceFile, String targetDir, String targetEncoding, Boolean describeByNative) { final String sourceFileName = sourceFile.getName().toLowerCase(); if (Arrays.stream(JS_POSTFIX_ARRAY).filter( postfix -> sourceFileName.lastIndexOf(postfix) == sourceFileName.length() - postfix.length()) .count() == 0)/* w w w . j a v a 2 s . c o m*/ return; try { final Map<String, List<String>> stringListMap = new HashMap<>(); final FileInputStream file = new FileInputStream(sourceFile); Iterator<Row> rowIterator; try { final XSSFWorkbook workbook = new XSSFWorkbook(file); final XSSFSheet sheet = workbook.getSheetAt(0); rowIterator = sheet.iterator(); } catch (OfficeXmlFileException e) { System.err.println(" exception:" + e.getMessage()); final HSSFWorkbook workbook = new HSSFWorkbook(file); final HSSFSheet sheet = workbook.getSheetAt(0); rowIterator = sheet.iterator(); } while (rowIterator.hasNext()) { final Row row = rowIterator.next(); final String key = row.getCell(COLUMN_KEY).getStringCellValue(); final String language = row.getCell(COLUMN_LANG).getStringCellValue(); final String value = row.getCell(COLUMN_VALUE).getStringCellValue().trim(); if (StringUtils.isEmpty(key) || StringUtils.isEmpty(language) || StringUtils.isEmpty(value)) continue; List<String> stringList = stringListMap.get(language); if (stringList == null) { stringList = new ArrayList<>(); stringListMap.put(language, stringList); } final String newLine = "\\\n"; String lastValue = "", token; final BufferedReader reader = new BufferedReader(new StringReader(value)); while ((token = reader.readLine()) != null) lastValue += token + newLine; reader.close(); if (lastValue.lastIndexOf(newLine) == lastValue.length() - newLine.length()) lastValue = lastValue.substring(0, lastValue.length() - newLine.length()); addProperty(stringList, key, lastValue, describeByNative); } for (String language : stringListMap.keySet()) { Files.write(Paths.get(new File(targetDir, messagesPropertiesFileName(language)).toURI()), stringListMap.get(language), Charset.forName(targetEncoding), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE); } } catch (Exception e) { System.err.println(" FAIL to convert: " + sourceFile.getAbsolutePath()); e.printStackTrace(); } }