0

Currently, my plugin creates a java file in my project(IProject). But I want that java file within a specified Package. How to do it.

IFile sampleFile = parentFolder.getFile("Sample.java");
        if(!sampleFile.exists())   FileInputStream fileStream = new FileInputStream("C:\Users\Uma\Desktop\treasureHunt\Application.java"); sampleFile.create(fileStream, false, null);  

This is my current piece of code. How can I create the sampleFile within a package. For example: in package com.mdh.se as com.mdh.se.Sample.java

0

3 Answers 3

2

If you have a "package" (e.g. "com.mdh.se") then you'll have a corresponding subdirectory (for example, "c:\users\uma\desktop\treasurehunt\com\mdh\se"). Simply write your file there.

Sign up to request clarification or add additional context in comments.

Comments

0

I think, that the only thing you need to do is to create folders representing your package structure. So your path should look like C:\Users\Uma\Desktop\treasureHunt\com\mdh\se\Sample.java for your example.

2 Comments

Yes, you are right. I will try this way. But in eclipse, packages are represented in a different way right? They look like special kind of folders!
I think, that the representation depends on the IDE itself, but the source code must be structured in this way, because it's standard java source files organization. But you may have to register the new source somehow to your IDE or make your IDE refresh the project.
0

You can get a special file inside the package by calling

java.net.URL imgURL = ResourceManager.class.getResource( "ResourceManager.class" );

From these URL you can extract the directory, the file is placed.

A new file you can create with

new File(directory,filename);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.