Java Code Examples for com.intellij.openapi.util.io.FileUtil#rename()

The following examples show how to use com.intellij.openapi.util.io.FileUtil#rename() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileWatcherTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testFileCaseChange() throws Exception {
  if (SystemInfo.isFileSystemCaseSensitive) {
    System.err.println("Ignored: case-insensitive FS required");
    return;
  }

  File topDir = createTestDir("topDir");
  File testFile = createTestFile(topDir, "file.txt", "123");
  refresh(topDir);

  LocalFileSystem.WatchRequest request = watch(topDir);
  try {
    myAccept = true;
    File newFile = new File(testFile.getParent(), StringUtil.capitalize(testFile.getName()));
    FileUtil.rename(testFile, newFile);
    assertEvent(VFilePropertyChangeEvent.class, newFile.getPath());
  }
  finally {
    unwatch(request);
  }
}
 
Example 2
Source File: LocalFileSystemTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testFileCaseChange() throws Exception {
  if (SystemInfo.isFileSystemCaseSensitive) {
    System.err.println("Ignored: case-insensitive FS required");
    return;
  }

  File top = createTempDirectory(false);
  File file = IoTestUtil.createTestFile(top, "file.txt", "test");
  File intermediate = new File(top, "_intermediate_");

  VirtualFile topDir = myFS.refreshAndFindFileByIoFile(top);
  assertNotNull(topDir);
  VirtualFile sourceFile = myFS.refreshAndFindFileByIoFile(file);
  assertNotNull(sourceFile);

  String newName = StringUtil.capitalize(file.getName());
  FileUtil.rename(file, intermediate);
  FileUtil.rename(intermediate, new File(top, newName));
  topDir.refresh(false, true);
  assertFalse(((VirtualDirectoryImpl)topDir).allChildrenLoaded());
  assertTrue(sourceFile.isValid());
  assertEquals(newName, sourceFile.getName());

  topDir.getChildren();
  newName = newName.toLowerCase(Locale.ENGLISH);
  FileUtil.rename(file, intermediate);
  FileUtil.rename(intermediate, new File(top, newName));
  topDir.refresh(false, true);
  assertTrue(((VirtualDirectoryImpl)topDir).allChildrenLoaded());
  assertTrue(sourceFile.isValid());
  assertEquals(newName, sourceFile.getName());
}
 
Example 3
Source File: ActionWithTempFile.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void rollbackChanges() throws IOException {
  try {
    FileUtil.delete(mySourceFile);
  }
  finally {
    try {
      FileUtil.rename(myTempFile, mySourceFile);
    }
    finally {
      FileUtil.delete(myTempFile);
    }
  }
}
 
Example 4
Source File: ArchivesBuilder.java    From consulo with Apache License 2.0 4 votes vote down vote up
private static void renameFile(final File fromFile, final File toFile, final Set<String> writtenPaths) throws IOException {
  FileUtil.rename(fromFile, toFile);
  writtenPaths.add(toFile.getPath());
}
 
Example 5
Source File: RemoteFileInfo.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void finished(@Nullable final FileType fileType) {
  final File localIOFile;

  synchronized (myLock) {
    LOG.debug("Downloading finished, size = " + myLocalFile.length() + ", file type=" + (fileType != null ? fileType.getName() : "null"));
    if (fileType != null) {
      String fileName = myLocalFile.getName();
      int dot = fileName.lastIndexOf('.');
      String extension = fileType.getDefaultExtension();
      if (dot == -1 || !extension.equals(fileName.substring(dot + 1))) {
        File newFile = FileUtil.findSequentNonexistentFile(myLocalFile.getParentFile(), fileName, extension);
        try {
          FileUtil.rename(myLocalFile, newFile);
          myLocalFile = newFile;
        }
        catch (IOException e) {
          LOG.debug(e);
        }
      }
    }

    localIOFile = myLocalFile;
  }

  VirtualFile localFile = WriteAction.compute(() -> {
    final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(localIOFile);
    if (file != null) {
      file.refresh(false, false);
    }
    return file;
  });
  LOG.assertTrue(localFile != null, "Virtual local file not found for " + localIOFile.getAbsolutePath());
  LOG.debug("Virtual local file: " + localFile + ", size = " + localFile.getLength());
  synchronized (myLock) {
    myLocalVirtualFile = localFile;
    myPrevLocalFile = null;
    myState = RemoteFileState.DOWNLOADED;
    myErrorMessage = null;
  }
  for (FileDownloadingListener listener : myListeners) {
    listener.fileDownloaded(localFile);
  }
}
 
Example 6
Source File: DownloadUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * Downloads content of {@code url} to {@code outputFile} atomically.<br/>
 * {@code outputFile} isn't modified if an I/O error occurs or {@code contentChecker} is provided and returns false on the downloaded content.
 * More formally, the steps are:
 * <ol>
 *   <li>Download {@code url} to {@code tempFile}. Stop in case of any I/O errors.</li>
 *   <li>Stop if {@code contentChecker} is provided, and it returns false on the downloaded content.</li>
 *   <li>Move {@code tempFile} to {@code outputFile}. On most OS this operation is done atomically.</li>
 * </ol>
 *
 * Motivation: some web filtering products return pure HTML with HTTP 200 OK status instead of
 * the asked content.
 *
 * @param indicator   progress indicator
 * @param url         url to download
 * @param outputFile  output file
 * @param tempFile    temporary file to download to. This file is deleted on method exit.
 * @param contentChecker checks whether the downloaded content is OK or not
 * @returns true if no {@code contentChecker} is provided or the provided one returned true
 * @throws IOException if an I/O error occurs
 */
public static boolean downloadAtomically(@Nullable ProgressIndicator indicator,
                                         @Nonnull String url,
                                         @Nonnull File outputFile,
                                         @Nonnull File tempFile,
                                         @Nullable Predicate<String> contentChecker) throws IOException
{
  try {
    downloadContentToFile(indicator, url, tempFile);
    if (contentChecker != null) {
      String content = FileUtil.loadFile(tempFile);
      if (!contentChecker.test(content)) {
        return false;
      }
    }
    FileUtil.rename(tempFile, outputFile);
    return true;
  } finally {
    FileUtil.delete(tempFile);
  }
}
 
Example 7
Source File: ActionWithTempFile.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void init() throws IOException {
  myTempFile = FileUtil.createTempFile(TMP_PREFIX, TMP_SUFFIX);
  FileUtil.delete(myTempFile);
  FileUtil.rename(mySourceFile, myTempFile);
}