本文整理了Java中java.nio.file.Files.createTempFile()
方法的一些代码示例,展示了Files.createTempFile()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Files.createTempFile()
方法的具体详情如下:
包路径:java.nio.file.Files
类名称:Files
方法名:createTempFile
暂无
代码示例来源:origin: oracle/helidon
/**
* Creates a new temporary file with a generated unique name.
* @return the new File
* @throws IOException in case of error creating the new file
*/
public File newFile() throws IOException {
return Files.createTempFile(root, "test", "file").toFile();
}
代码示例来源:origin: eclipse-vertx/vert.x
public String perform() {
try {
Path tmpFile;
if (parentDir != null) {
Path dir = vertx.resolveFile(parentDir).toPath();
if (attrs != null) {
tmpFile = Files.createTempFile(dir, prefix, suffix, attrs);
} else {
tmpFile = Files.createTempFile(dir, prefix, suffix);
}
} else {
if (attrs != null) {
tmpFile = Files.createTempFile(prefix, suffix, attrs);
} else {
tmpFile = Files.createTempFile(prefix, suffix);
}
}
return tmpFile.toFile().getAbsolutePath();
} catch (IOException e) {
throw new FileSystemException(e);
}
}
};
代码示例来源:origin: lets-blade/blade
@Override
public OutputStreamWrapper outputStream() throws IOException {
File file = Files.createTempFile("blade", ".temp").toFile();
OutputStream outputStream = new FileOutputStream(file);
return new OutputStreamWrapper(outputStream, file);
}
代码示例来源:origin: lets-blade/blade
@Override
public OutputStreamWrapper outputStream() throws IOException {
File file = Files.createTempFile("blade", ".temp").toFile();
OutputStream outputStream = new FileOutputStream(file);
return new OutputStreamWrapper(outputStream, file);
}
代码示例来源:origin: MovingBlocks/Terasology
@Override
public Path call() throws IOException {
Path folder = target.getParent();
String prefix = target.getFileName().toString();
Path tempModuleLocation = Files.createTempFile(folder, prefix + "_", ".tmp");
tempModuleLocation.toFile().deleteOnExit();
logger.debug("Downloading {} from {}", target, url);
long length = -1;
URLConnection conn = url.openConnection();
if (conn instanceof HttpURLConnection) {
length = ((HttpURLConnection) conn).getContentLengthLong();
}
try (InputStream is = url.openStream();
OutputStream os = Files.newOutputStream(tempModuleLocation)) {
copy(is, os, length, listener);
Files.move(tempModuleLocation, target, StandardCopyOption.REPLACE_EXISTING);
}
return target;
}
代码示例来源:origin: wildfly/wildfly
@Override
public void data(final ByteBuffer buffer) throws IOException {
this.currentFileSize += buffer.remaining();
if (this.maxIndividualFileSize > 0 && this.currentFileSize > this.maxIndividualFileSize) {
throw UndertowMessages.MESSAGES.maxFileSizeExceeded(this.maxIndividualFileSize);
}
if (file == null && fileName != null && fileSizeThreshold < this.currentFileSize) {
try {
if (tempFileLocation != null) {
file = Files.createTempFile(tempFileLocation, "undertow", "upload");
} else {
file = Files.createTempFile("undertow", "upload");
}
createdFiles.add(file);
FileOutputStream fileOutputStream = new FileOutputStream(file.toFile());
contentBytes.writeTo(fileOutputStream);
fileChannel = fileOutputStream.getChannel();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (file == null) {
while (buffer.hasRemaining()) {
contentBytes.write(buffer.get());
}
} else {
fileChannel.write(buffer);
}
}
代码示例来源:origin: apache/incubator-gobblin
private static File copyToLocal(FileSystem fs, Path keyPath) throws IOException {
java.nio.file.Path tmpKeyPath = Files.createTempFile(GoogleCommon.class.getSimpleName(), "tmp",
PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------")));
File copied = tmpKeyPath.toFile();
copied.deleteOnExit();
fs.copyToLocalFile(keyPath, new Path(copied.getAbsolutePath()));
return copied;
}
代码示例来源:origin: robolectric/robolectric
@Implementation
protected static FileDescriptor nCreate(String name, int size) throws ErrnoException {
maybeThrow(fakeCreateException);
TempDirectory tempDirectory = RuntimeEnvironment.getTempDirectory();
try {
// Give each instance its own private file.
File sharedMemoryFile =
Files.createTempFile(
tempDirectory.createIfNotExists("SharedMemory"), "shmem-" + name, ".tmp")
.toFile();
RandomAccessFile randomAccessFile = new RandomAccessFile(sharedMemoryFile, "rw");
randomAccessFile.setLength(0);
randomAccessFile.setLength(size);
filesByFd.put(randomAccessFile.getFD(), sharedMemoryFile);
return randomAccessFile.getFD();
} catch (IOException e) {
throw new RuntimeException("Unable to create file descriptior", e);
}
}
代码示例来源:origin: deeplearning4j/nd4j
/**
* Get a temp file from the classpath, and (optionally) place it in the specified directory<br>
* Note that:<br>
* - If the directory is not specified, the file is copied to the default temporary directory, using
* {@link Files#createTempFile(String, String, FileAttribute[])}. Consequently, the extracted file will have a
* different filename to the extracted one.<br>
* - If the directory *is* specified, the file is copied directly - and the original filename is maintained
*
* @param rootDirectory May be null. If non-null, copy to the specified directory
* @return the temp file
* @throws IOException If an error occurs when files are being copied
* @see #getTempFileFromArchive(File)
*/
public File getTempFileFromArchive(File rootDirectory) throws IOException {
InputStream is = getInputStream();
File tmpFile;
if(rootDirectory != null){
//Maintain original file names, as it's going in a directory...
tmpFile = new File(rootDirectory, FilenameUtils.getName(path));
} else {
tmpFile = Files.createTempFile(FilenameUtils.getName(path), "tmp").toFile();
}
tmpFile.deleteOnExit();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile));
IOUtils.copy(is, bos);
bos.flush();
bos.close();
return tmpFile;
}
代码示例来源:origin: prestodb/presto
@Override
public FSDataOutputStream create(Path path, FsPermission permission, boolean overwrite, int bufferSize, short replication, long blockSize, Progressable progress)
throws IOException
{
if ((!overwrite) && exists(path)) {
throw new IOException("File already exists:" + path);
}
if (!stagingDirectory.exists()) {
createDirectories(stagingDirectory.toPath());
}
if (!stagingDirectory.isDirectory()) {
throw new IOException("Configured staging path is not a directory: " + stagingDirectory);
}
File tempFile = createTempFile(stagingDirectory.toPath(), "presto-s3-", ".tmp").toFile();
String key = keyFromPath(qualifiedPath(path));
return new FSDataOutputStream(
new PrestoS3OutputStream(s3, getBucketName(uri), key, tempFile, sseEnabled, sseType, sseKmsKeyId, multiPartUploadMinFileSize, multiPartUploadMinPartSize, s3AclType),
statistics);
}
代码示例来源:origin: eclipse-vertx/vert.x
/**
* Create a temp file that does not exists.
*/
public static File tmpFile(String suffix) throws Exception {
File tmp = Files.createTempFile("vertx", suffix).toFile();
assertTrue(tmp.delete());
return tmp;
}
代码示例来源:origin: apache/drill
private File getTempFile() throws Exception {
return java.nio.file.Files.createTempFile("drill-hive-test", ".txt").toFile();
}
代码示例来源:origin: apache/hive
tmpFile = Files.createTempFile(metricsDir, "hmsmetrics", "json", FILE_ATTRS);
} catch (IOException e) {
LOG.error("failed to create temp file for JSON metrics", e);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(tmpFile.toFile()))) {
bw.write(json);
} catch (IOException e) {
if (tmpFile.toFile().exists()) {
代码示例来源:origin: apache/hive
tmpFile = Files.createTempFile(metricsDir, "hmetrics", "json", FILE_ATTRS);
} catch (IOException e) {
LOGGER.error("failed to create temp file for JSON metrics", e);
try (BufferedWriter bw = new BufferedWriter(new FileWriter(tmpFile.toFile()))) {
bw.write(json);
} catch (IOException e) {
} finally {
if (tmpFile != null && tmpFile.toFile().exists()) {
代码示例来源:origin: GoogleContainerTools/jib
Path temporarySelectorFile = Files.createTempFile(null, null);
temporarySelectorFile.toFile().deleteOnExit();
Blobs.writeToFileWithLock(Blobs.from(layerDigest.getHash()), temporarySelectorFile);
代码示例来源:origin: apache/geode
/**
*
* @return Path to the zip file that has all the filtered files, null if no files are selected to
* export.
*/
public Path export() throws IOException {
Path tempDirectory = Files.createTempDirectory("exportLogs");
if (baseLogFile != null) {
for (Path logFile : findLogFiles(baseLogFile.toPath().getParent())) {
Path filteredLogFile = tempDirectory.resolve(logFile.getFileName());
writeFilteredLogFile(logFile, filteredLogFile);
}
}
if (baseStatsFile != null) {
for (Path statFile : findStatFiles(baseStatsFile.toPath().getParent())) {
Files.copy(statFile, tempDirectory.resolve(statFile.getFileName()));
}
}
Path zipFile = null;
if (tempDirectory.toFile().listFiles().length > 0) {
zipFile = Files.createTempFile("logExport", ".zip");
ZipUtils.zipDirectory(tempDirectory, zipFile);
LOGGER.info("Zipped files to: " + zipFile);
}
FileUtils.deleteDirectory(tempDirectory.toFile());
return zipFile;
}
代码示例来源:origin: deeplearning4j/dl4j-examples
File tempFile = Files.createTempFile(baseName, "." + ext).toFile();
代码示例来源:origin: OryxProject/oryx
private static Path copyAndUncompress(Path compressed) throws IOException {
Path tempFile = Files.createTempFile("part", ".csv");
tempFile.toFile().deleteOnExit();
try (InputStream in = new GZIPInputStream(Files.newInputStream(compressed))) {
Files.copy(in, tempFile, StandardCopyOption.REPLACE_EXISTING);
}
return tempFile;
}
代码示例来源:origin: pxb1988/dex2jar
final Path realJar;
if (Files.isDirectory(apkIn)) {
realJar = Files.createTempFile("d2j", ".jar");
tmp = realJar;
System.out.println("zipping " + apkIn + " -> " + realJar);
signer.sign(apkIn.toFile(), output.toFile());
代码示例来源:origin: spring-projects/spring-batch
@Before
public void setUp() throws Exception {
File file = Files.createTempFile("test", "json").toFile();
this.resource = new FileSystemResource(file);
}
内容来源于网络,如有侵权,请联系作者删除!