List of usage examples for java.nio.file Files createTempFile
public static Path createTempFile(String prefix, String suffix, FileAttribute<?>... attrs) throws IOException
From source file:org.apache.marmotta.platform.core.services.config.ConfigurationServiceImpl.java
protected void saveSecure(final PropertiesConfiguration conf) throws ConfigurationException { final File file = conf.getFile(); try {//from ww w. ja va2 s . c o m if (file == null) { throw new ConfigurationException("No file name has been set!"); } else if ((file.createNewFile() || true) && !file.canWrite()) { throw new IOException("Cannot write to file " + file.getAbsolutePath() + ". Is it read-only?"); } } catch (IOException e) { throw new ConfigurationException(e.getMessage(), e); } log.debug("Saving {}", file.getAbsolutePath()); final String fName = file.getName(); try { int lastDot = fName.lastIndexOf('.'); lastDot = lastDot > 0 ? lastDot : fName.length(); final Path configPath = file.toPath(); // Create a tmp file next to the original final Path tmp = Files.createTempFile(configPath.getParent(), fName.substring(0, lastDot) + ".", fName.substring(lastDot)); try { Files.copy(configPath, tmp, StandardCopyOption.COPY_ATTRIBUTES, StandardCopyOption.REPLACE_EXISTING); } catch (IOException iox) { log.error("Could not create temp-file {}: {}", tmp, iox.getMessage()); throw iox; } log.trace("using temporary file: {}", tmp); // Save the config to the tmp file conf.save(tmp.toFile()); log.trace("tmp saved, now replacing the original file: {}", configPath); // Replace the original with the tmp file try { try { Files.move(tmp, configPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE); } catch (AtomicMoveNotSupportedException amnx) { log.trace("atomic move not available: {}, trying without", amnx.getMessage()); Files.move(tmp, configPath, StandardCopyOption.REPLACE_EXISTING); } } catch (IOException iox) { log.error("Could not write to {}, a backup was created in {}", configPath, tmp); throw iox; } log.info("configuration successfully saved to {}", configPath); } catch (final Throwable t) { throw new ConfigurationException("Unable to save the configuration to the file " + fName, t); } }
From source file:org.anc.lapps.nlp4j.NLP4JCustomTrain.java
/** This method creates a temporary text file at a certain directory, and writes * the given content into the file. The file will also be set to delete on exit. * * @param fileName The prefix for the temporary file to be created * @param dirPath The path to the directory in which the file should be created * @param fileTxt The text to be written in the file * @param extension The extension to be given to the temporary file * @return A path to the temporary text file that was created *//*from w w w. j av a 2 s . c o m*/ protected Path writeTempFile(String fileName, Path dirPath, String fileTxt, String extension) throws IOException { Path filePath = Files.createTempFile(dirPath, fileName, extension); File file = filePath.toFile(); PrintWriter writer = new PrintWriter(file, "UTF-8"); writer.print(fileTxt); writer.close(); file.deleteOnExit(); return filePath; }
From source file:org.roda.core.model.ModelServiceTest.java
@Test public void testLiteOptionalWithCauseSerialization() throws RODAException, IOException, ClassNotFoundException { final String aipId = CorporaConstants.SOURCE_AIP_ID; AIP aip = model.createAIP(aipId, corporaService, DefaultStoragePath.parse(CorporaConstants.SOURCE_AIP_CONTAINER, CorporaConstants.SOURCE_AIP_ID), RodaConstants.ADMIN);/*w ww . j a v a2 s . c om*/ Optional<LiteRODAObject> lightAIP = model.retrieveLiteFromObject(aip); // cleanup model.deleteAIP(aipId); if (lightAIP.isPresent()) { LiteOptionalWithCause test = LiteOptionalWithCause.of(lightAIP.get()); Path tempFile = Files.createTempFile(basePath, "test", ".tmp"); try (ObjectOutputStream oos = new ObjectOutputStream(Files.newOutputStream(tempFile))) { oos.writeObject(test); } try (ObjectInputStream ois = new ObjectInputStream(Files.newInputStream(tempFile))) { LiteOptionalWithCause test2 = (LiteOptionalWithCause) ois.readObject(); assertEquals(test, test2); } } else { fail("Should be present"); } }