本文整理匯總了Java中java.nio.file.Files.move方法的典型用法代碼示例。如果您正苦於以下問題:Java Files.move方法的具體用法?Java Files.move怎麽用?Java Files.move使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.file.Files
的用法示例。
在下文中一共展示了Files.move方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createTransactionOutput
import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public OutputStream createTransactionOutput() throws IOException {
Path tmpFile = m_transactionTmpDir.resolve(getFileName(m_tmpCounter.getAndIncrement(), TX_EXT));
return new BufferedOutputStream(Files.newOutputStream(tmpFile)) {
@Override
public void close() throws IOException {
super.close();
synchronized (m_commitLock) {
long transactionId = m_nextTxId++;
Path txFile = getTxFile(transactionId);
Files.move(tmpFile, txFile);
m_commitLock.notifyAll();
}
}
};
}
示例2: rename
import java.nio.file.Files; //導入方法依賴的package包/類
@Override public Resource rename(String text) {
try {
File sourceFile = path.toFile();
File destinationFile = path.resolveSibling(text).toFile();
Path moved;
if (!sourceFile.getParent().equals(destinationFile.getParent())) {
moved = Files.move(path, path.resolveSibling(text));
} else {
sourceFile.renameTo(destinationFile);
moved = destinationFile.toPath();
}
FileResource to = new FileResource(moved.toFile());
Event.fireEvent(this, new ResourceModificationEvent(ResourceModificationEvent.MOVED, this, to));
return to;
} catch (IOException e) {
FXUIUtils.showMessageDialog(null, "Unable to rename file: " + e.getMessage(), e.getClass().getName(), AlertType.ERROR);
return null;
}
}
示例3: renameFile
import java.nio.file.Files; //導入方法依賴的package包/類
public static Boolean renameFile(String fromFile, String toName) {
try {
File src = new File(fromFile);
if (src.exists()) {
File target = new File(src.getParent(), toName);
if (target.exists()) {
LOGGER.log(Level.INFO, "A File with Name '{1}' already exists, failed to rename '{0}'",
new Object[]{fromFile, toName});
return false;
}
Files.move(src.toPath(), target.toPath());
}
} catch (IOException ex) {
Logger.getLogger(FileUtils.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
LOGGER.log(Level.INFO, "{0} Renamed to {1}", new Object[]{fromFile, toName});
return true;
}
示例4: create
import java.nio.file.Files; //導入方法依賴的package包/類
private boolean create() throws IOException {
JmodFileWriter jmod = new JmodFileWriter();
// create jmod with temporary name to avoid it being examined
// when scanning the module path
Path target = options.jmodFile;
Path tempTarget = jmodTempFilePath(target);
try {
try (JmodOutputStream jos = JmodOutputStream.newOutputStream(tempTarget)) {
jmod.write(jos);
}
Files.move(tempTarget, target);
} catch (Exception e) {
try {
Files.deleteIfExists(tempTarget);
} catch (IOException ioe) {
e.addSuppressed(ioe);
}
throw e;
}
return true;
}
示例5: z2zmove
import java.nio.file.Files; //導入方法依賴的package包/類
private static void z2zmove(FileSystem src, FileSystem dst, String path)
throws IOException
{
Path srcPath = src.getPath(path);
Path dstPath = dst.getPath(path);
if (Files.isDirectory(srcPath)) {
if (!Files.exists(dstPath))
mkdirs(dstPath);
try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
for (Path child : ds) {
z2zmove(src, dst,
path + (path.endsWith("/")?"":"/") + child.getFileName());
}
}
} else {
//System.out.println("moving..." + path);
Path parent = dstPath.getParent();
if (parent != null && Files.notExists(parent))
mkdirs(parent);
Files.move(srcPath, dstPath);
}
}
示例6: update
import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public void update(Session session) {
final String sessionId = ensureStringSessionId(session);
final Path oldPath = sessionId2Path(sessionId);
if (!Files.exists(oldPath)) {
throw new UnknownSessionException(sessionId);
}
try {
final Path newPath = Files.createTempFile(tmpDir, null, null);
Files.write(newPath, serialize(session));
Files.move(newPath, oldPath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new SerializationException(e);
}
}
示例7: generateJAXPProps
import java.nio.file.Files; //導入方法依賴的package包/類
static void generateJAXPProps(String content) throws IOException {
Path filePath = getJAXPPropsPath();
Path bakPath = filePath.resolveSibling(JAXP_PROPS_BAK);
System.out.println("Creating new file " + filePath +
", saving old version to " + bakPath + ".");
if (Files.exists(filePath) && !Files.exists(bakPath)) {
Files.move(filePath, bakPath);
}
Files.write(filePath, content.getBytes());
}
示例8: update
import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public void update(File updateFile, Plugin plugin) throws IOException {
Path src = updateFile.toPath();
Path trg = UpdateMethod.getPluginFile(plugin).toPath();
try {
Files.move(src, trg, ATOMIC_MOVE);
} catch (Exception e) {
//Fallback
Files.move(src, trg, REPLACE_EXISTING);
}
}
示例9: close
import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public void close() throws IOException {
if (!closed) {
// close new entry
os.closeEntry();
// copy existing entries
if (Files.exists(zipFilePath)) {
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry zipEntry = e.nextElement();
if (!zipEntry.getName().equals(fileName)) {
os.putNextEntry(zipEntry);
try (InputStream zis = zipFile.getInputStream(zipEntry.getName())) {
ByteStreams.copy(zis, os);
}
os.closeEntry();
}
}
}
}
// close zip
super.close();
// swap with tmp zip
Path tmpZipFilePath = getTmpZipFilePath(zipFilePath);
Files.move(tmpZipFilePath, zipFilePath, StandardCopyOption.REPLACE_EXISTING);
closed = true;
}
}
示例10: renameFile
import java.nio.file.Files; //導入方法依賴的package包/類
@SneakyThrows(IOException.class)
private void renameFile() {
Path path = places.closeAndGetRenameFolderPlace();
String nameOfFolder = name.getText();
Path pathRename = path.getParent().resolve(nameOfFolder);
Files.move(path, pathRename);
}
示例11: main
import java.nio.file.Files; //導入方法依賴的package包/類
public static void main(String[] args) {
Path src = Paths.get(System.getProperty("user.home") + "myfile.txt");
Path dest = Paths.get(System.getProperty("user.home") + "/test/myfile.txt");
try {
// 如果目標文件存在則先刪除,防止幹擾move的結果
Files.delete(dest);
Files.move(src, dest, REPLACE_EXISTING); // |\longremark{可以嘗試去掉REPLACE\_EXISTING參數看看運行結果有什麽不同?}|
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("move file success? " + Files.exists(dest));
}
示例12: moveFilesDirs
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Move files (or directories) to specified path
* @param originPath String that contains the original file/folder to be moved
* @param destinationPath String that contains the destination path to save the old file/directories
* @param replaceExisting boolean that indicates if replace any existing file/directory if duplicated
*/
public static void moveFilesDirs(String originPath, String destinationPath, boolean replaceExisting) {
try {
if (replaceExisting)
Files.move(Paths.get(originPath), Paths.get(destinationPath), REPLACE_EXISTING);
else
Files.move(Paths.get(originPath), Paths.get(destinationPath));
} catch (IOException e) {
IO.write("There was an error while trying to move the file/dir specified. Full trace: \n");
e.printStackTrace();
}
}
示例13: movefile
import java.nio.file.Files; //導入方法依賴的package包/類
static void movefile(String f) throws IOException {
Path src = Paths.get(testclasses, f);
Path dest = subdir.toPath().resolve(f);
if (!dest.toFile().exists()) {
System.out.printf("moving %s to %s\n", src.toString(), dest.toString());
Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING);
} else if (src.toFile().exists()) {
System.out.printf("%s exists, deleting %s\n", dest.toString(), src.toString());
Files.delete(src);
} else {
System.out.printf("NOT moving %s to %s\n", src.toString(), dest.toString());
}
}
示例14: setup
import java.nio.file.Files; //導入方法依賴的package包/類
@BeforeTest
private void setup() throws Exception {
Files.createDirectory(BAR_DIR);
Path pkgDir = Paths.get("p");
// compile classes in package p
assertTrue(CompilerUtils.compile(SRC_DIR.resolve(pkgDir), BAR_DIR));
// move p.Foo to a different directory
Path foo = pkgDir.resolve("Foo.class");
Files.createDirectories(FOO_DIR.resolve(pkgDir));
Files.move(BAR_DIR.resolve(foo), FOO_DIR.resolve(foo),
StandardCopyOption.REPLACE_EXISTING);
}
示例15: writeLayer
import java.nio.file.Files; //導入方法依賴的package包/類
public CachedLayer writeLayer(UnwrittenLayer layer) throws IOException {
// Writes to a temporary file first because the UnwrittenLayer needs to be written first to
// obtain its digest.
Path tempLayerFile = Files.createTempFile(cache.getCacheDirectory(), null, null);
// TODO: Find a way to do this with java.nio.file
tempLayerFile.toFile().deleteOnExit();
// Writes the UnwrittenLayer layer BLOB to a file to convert into a CachedLayer.
try (CountingDigestOutputStream compressedDigestOutputStream =
new CountingDigestOutputStream(
new BufferedOutputStream(Files.newOutputStream(tempLayerFile)))) {
// Writes the layer with GZIP compression. The original bytes are captured as the layer's
// diff ID and the bytes outputted from the GZIP compression are captured as the layer's
// content descriptor.
GZIPOutputStream compressorStream = new GZIPOutputStream(compressedDigestOutputStream);
DescriptorDigest diffId = layer.getBlob().writeTo(compressorStream).getDigest();
// The GZIPOutputStream must be closed in order to write out the remaining compressed data.
compressorStream.close();
BlobDescriptor compressedBlobDescriptor = compressedDigestOutputStream.toBlobDescriptor();
// Renames the temporary layer file to the correct filename.
Path layerFile =
CacheFiles.getLayerFile(cache.getCacheDirectory(), compressedBlobDescriptor.getDigest());
// TODO: Should probably check for existence of target file and whether or not it's the same.
try {
Files.move(tempLayerFile, layerFile);
} catch (IOException ex) {
throw new IOException(
"Could not rename layer "
+ compressedBlobDescriptor.getDigest().getHash()
+ " to "
+ layerFile,
ex);
}
return new CachedLayer(layerFile, compressedBlobDescriptor, diffId);
}
}