文件操作

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 (member instanceof IFolder)

                    {

                        return hasFile((IFolder)member,file);

                    }

                }

            }

            catch (CoreException e)

            {

                e.printStackTrace();

            }

        }

        return false;

       

    }

 

4.新建文件

{//Put global variables into ebus.properties

            IFile propertiesFile =outputFolder.getFile(new Path(

                    "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 = new Properties();

            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(new File(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 = new ResourceSetImpl();

            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 (documentRoot instanceof 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);

        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值