Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7044859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:27:35+00:00 2026-05-28T02:27:35+00:00

I have a source folder with Java source files. These Java files have different

  • 0

I have a source folder with Java source files. These Java files have different packages. Using javac command I can generate the package structure, but the packages will contain the class files not the source files.

Is there any API which generates package structure from Java files and puts Java files into the specific package

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:27:36+00:00Added an answer on May 28, 2026 at 2:27 am

    Here’s what I came up for this problem.It opens the java file, reads the package name, generates the structure and copies the file to that structure. Suggestions for improvement are welcome. 🙂

    public final class FileListing {
    
    private Map packageMap;
    
    
    public void createPackageStructure(String sourceDir) throws FileNotFoundException 
    {
        FileListing fileListing = new FileListing();
    File startingDirectory= new File(sourceDir);
    
        fileListing.packageMap = new HashMap();
        List<File> files = fileListing.getFileListing(startingDirectory,   fileListing.getPackageMap());
    
        fileListing.moveFiles(fileListing.packageMap);
    
    }
    
    
    public List<File> getFileListing(File aStartingDir, Map packageMap) throws   FileNotFoundException 
    {
        validateDirectory(aStartingDir);
        List<File> result = getFileListingNoSort(aStartingDir,packageMap);
        Collections.sort(result);
        return result;
    }
    
    
    private List<File> getFileListingNoSort(File aStartingDir, Map packageMap) throws FileNotFoundException 
    {  
        List<File> result = new ArrayList<File>();
        File[] filesAndDirs = aStartingDir.listFiles();
        List<File> filesDirs = Arrays.asList(filesAndDirs);
    
        for(File file : filesDirs) 
        {
           result.add(file); 
           if(file.isFile())
           {
               packageMap.put(file, readPackageName(file.getAbsolutePath()).replace(".", "/").replace(";", "/"));
           }
           else 
           {
               //must be a directory
               //recursive call!
               List<File> deeperList = getFileListingNoSort(file,packageMap);
               result.addAll(deeperList);
           }
        }
    return result;
    }
    
    public String readPackageName(String filePath)
    {
      String packageName=null;
      String line;
      String temp[] = new String[2];
      BufferedReader br=null;
      try{
          File javaFile =  new File(filePath);
          br = new BufferedReader(new FileReader(javaFile));
          while((line=br.readLine())!=null)
          {
              if(line.indexOf("package")!=-1)
              {
                  temp = line.split(" ");
                  break;
              }
          }
          br.close();
    
      }catch(FileNotFoundException fnfe)
      {
          fnfe.printStackTrace();
      }catch(IOException ioe)
      {
          ioe.printStackTrace();
      }
      return temp[1];
    }
    
    public void moveFiles(Map packageMap)
    {
     Set keySet = packageMap.keySet();
     Iterator it = keySet.iterator();
         File sourceFile, destFile, destDirs;
     InputStream in = null;
     OutputStream out = null;
     byte[] buf = new byte[1024];
     int len;
    
         try{
         while(it.hasNext())
             {
            sourceFile = (File)it.next();
            destDirs = new File("src/"+(String)packageMap.get(sourceFile));
            destFile = new File("src/"+   (String)packageMap.get(sourceFile)+"/"+sourceFile.getName());
            destDirs.mkdirs();
            in = new FileInputStream(sourceFile);
            out = new FileOutputStream(destFile);
    
            while((len = in.read(buf)) > 0){
                out.write(buf, 0, len);
            }
             }
       }catch(FileNotFoundException fnfe)
       {
           fnfe.printStackTrace();
       }catch(IOException ioe)
       {
           ioe.printStackTrace();
       }
    }
    
    static private void validateDirectory (File aDirectory) throws FileNotFoundException 
    {
      if (aDirectory == null) {
        throw new IllegalArgumentException("Directory should not be null.");
      }
      if (!aDirectory.exists()) {
        throw new FileNotFoundException("Directory does not exist: " + aDirectory);
      }
      if (!aDirectory.isDirectory()) {
        throw new IllegalArgumentException("Is not a directory: " + aDirectory);
      }
      if (!aDirectory.canRead()) {
        throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
      }
    }
    
    public Map getPackageMap()
    {
      return this.packageMap;
    }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a grails project with some additional java source files under src/java folder.
i'm coming into C++ from Java/AS3-land, and i'm used to the package-cum-folder structure for
Most of our Eclipse projects have multiple source folders, for example: src/main/java src/test/java When
I have a set of source folders. I use a Java class to build
I currently have a source control folder in TFS with all of my source
For example: I'm on MS DOS, I have a source code in the folder
I have a project folder with sources, headers, resources, etc. Most files are subject
I have a java project. The working folder from someone else's Eclipse project (It
I want to call a C subroutine from Java. I'm using JNI. I have
Here's the problem: I have a java dynamic web project under source control and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.