2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.openapi
.vfs
;
19 import org
.jetbrains
.annotations
.NotNull
;
20 import org
.jetbrains
.annotations
.Nullable
;
23 import java
.io
.IOException
;
26 * Utility class for work with both java.io.File and VirtualFile. It helps
27 * to create new VirtualFiles by java.io.Files. It is helpful for storing
28 * information about non-existing files with possibility to create VirtualFile
31 * @author Konstantin Bulenkov
32 * @see com.intellij.openapi.vfs.VirtualFile
35 public class VirtualFileWrapper
{
36 private final File myFile
;
39 * Constructs wrapper based on java.io.File
41 * @param file java.io.File
43 public VirtualFileWrapper(@NotNull File file
) {
48 * Tests whether the file passed to constructor exists.
50 * @return <code>true</code> if and only if the file passed to constructor
51 * exists; <code>false</code> otherwise
53 * @throws SecurityException
54 * If a security manager exists and its <code>{@link
55 * java.lang.SecurityManager#checkRead(java.lang.String)}</code>
56 * method denies read access to the file or directory
58 public boolean exists() {
59 return myFile
.exists();
63 * Refreshes LocalFileSystem and looks for VirtualFile
65 * @return VirtualFile or null if file is not exist
68 public VirtualFile
getVirtualFile() {
69 return LocalFileSystem
.getInstance().refreshAndFindFileByIoFile(myFile
);
73 * Looks for VirtualFile and tries to create new VirtualFile if
74 * <tt>createIfNotExist</tt> is <tt>true</tt> and file is not exist.
76 * @param createIfNotExist if <tt>true</tt> and java.io.File is not exist,
77 * VirtualFileWrapper will try to create new file
79 * @return virtual file
82 public VirtualFile
getVirtualFile(boolean createIfNotExist
) {
83 if (createIfNotExist
&& !myFile
.exists()) {
85 if (!myFile
.createNewFile()) {
88 } catch (IOException e
) {//
91 return getVirtualFile();
95 * Returns original java.io.File
97 * @return original java.io.File
100 public File
getFile() {