文件操作

来源:互联网 发布:java 全角半角 空格 编辑:程序博客网 时间:2024/06/04 18:53

File ioFile = newFile(location.toString() +"\\ebus.properties");

ioFile.createNewFile();

 

1. CommonSearchUtil.searchFilesWithinProject(container,

 

 

2. foundation.common.core.util

/**

 * 为常用的字符串定义了一些常量

 */

public interface IStringConstants

3.删除文件 processFile.delete(IResource.FORCE,new NullProgressMonitor());

4.判断文件是否在某工程文件夹内

 private booleanhasFile(IFolderfolder, IFile file)

    {

        if (folder != null && file != null)

        {

            IFile resultFile = folder.getFile(StringUtils.defaultString(file.getName()));

            if (resultFile.isAccessible())

            {

                return true;

            }

            try

            {

                for (IResource member : folder.members())

                {

                    if (memberinstanceof IFolder)

                    {

                        returnhasFile((IFolder)member,file);

                    }

                }

            }

            catch (CoreException e)

            {

                e.printStackTrace();

            }

        }

        return false;

       

    }

 

4.新建文件

{//Put global variables into ebus.properties

            IFile propertiesFile =outputFolder.getFile(newPath(

                    "WEB-INF/classes/ebus/ebus.base.properties"));

            propertiesFile.create(null, IResource.DEPTH_ZERO,null);

// outputFolder是工程文件夹,propertiesFile是工程文件

            Map<String, String>map = newHashMap<String,String>();

            initGlobalVariables(GlobalVariablesUiPlugin.getDefault()

                    .getStateLocation()

                    .append(".globalvariables"), map);

            Properties properties = newProperties();

            for (Entry<String,String>entry : map.entrySet())

            {

                if (globalVarKeys.contains(entry.getKey()))

                {

                    properties.setProperty(entry.getKey(), entry.getValue());

                }

            }

            PropertiesUtil.writePropertiesFile(propertiesFile.getLocation()

                    .toFile(), properties);

// propertiesFile.getLocation().toFile()是物理文件

            propertiesFile.refreshLocal(IResource.DEPTH_ZERO,null);

//刷新之后才能把修改保存到文件。

        }

5.新建文件并获取其模型(无需editingDomain)

(1)新建文件

IFile file = folder.getFile(IProcessUiNavigatorConsts.DEFINITION_VARIABLE_FILE_NAME);

       

        if (!file.isAccessible())

        {

            URL url = ProcessUiNavigatorPlugin.getDefault()

                    .getBundle()

                    .getEntry("resource/definitionvariable.xml");

            FileUtils.copyFileToDirectory(newFile(FileLocator.resolve(url)

                    .getPath()), folder.getLocation().toFile());

            file.refreshLocal(IResource.DEPTH_ZERO,new NullProgressMonitor());

 

(2)创建资源

            URI uri = URIUtil.convertIFiletoURI(file);

       ResourceSetImpl resourceSet =  = editingDomain.getResourceSet();

            Map<String, ?>description = resourceSet.getURIConverter()

                    .contentDescription(uri,Collections.EMPTY_MAP);

 

       //若无editingDomain 则自建资源集resourceSet = newResourceSetImpl();

            String contentTypeIdentifier= (String)description.get(ContentHandler.CONTENT_TYPE_PROPERTY);

            // Load the resource through the editing domain.

            Resource resource = resourceSet.createResource(uri, contentTypeIdentifier);

(3)加载资源

            resource.load(Collections.EMPTY_MAP);

            if (resource.getContents().size() == 1)

            {

(4)获取模型

                EObject documentRoot =resource.getContents().get(0);

               

                if (documentRootinstanceof DocumentRoot)

                {

                    final Definitions rootModel =((DocumentRoot)documentRoot).getDefinitions();

                    RecordingCommandrCommand = newRecordingCommand(

                            editingDomain)

                    {

                        @Override

                        protected void doExecute()

                        {

                            rootModel.setId(definitionId);

                        }

                    };

                    editingDomain.getCommandStack().execute(rCommand);

                }

            }

            resource.save(Collections.EMPTY_MAP);

        }