有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的文件结构

您好,我是Java新手,所以我想了解一些关于如何用Java组织项目文件的指南。目前,我正在使用GUI构建一个应用程序,因此我需要两个文件,一个与GUI相关,另一个与从第一个GUI调用的任何函数相关。现在我已经命名了第二个文件实用程序。java,如下所示:

package Directory;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SwingWorker;


public class Utilities {

    public class FileCopy{
        private File  Source;
        private File Destination;
        private long totalBytes=0L;

        FileCopy(File source,File destination){
            Source=source;
            Destination=destination;
            retrieveTotalBytes(source);
        }

        File getSource(){return Source;}
        File getDestination(){return Destination;}
        Long gettotalBytes(){return totalBytes;}

        private void retrieveTotalBytes(File sourceFile)
        {
                if(sourceFile.isDirectory()==false){
                    totalBytes = sourceFile.length();
                }
                else{
                    File[] files = sourceFile.listFiles();
                    for(File file : files)
                    {
                        if(file.isDirectory()) retrieveTotalBytes(file);
                        else totalBytes += file.length();
                    }
                }
                System.out.print("Done retrieving");
         }  
    }


    public class Copy extends SwingWorker<Void,Integer>
    {

      File src,dest;
      InputStream in;
      OutputStream out;
      JProgressBar progressBar;
      JProgressBar all;
      JTextArea txt;
      public int progress;
      //private int all_progress;
      private long totalBytes = 0L;
      private long copiedBytes = 0L;
      boolean keepStructure=false;
      boolean delete=false;

          public Copy(File source,File dst,JProgressBar br,JTextArea text,boolean keep,boolean delete)
          {
             src=source;
             dest=dst;
             progressBar=br;
             txt=text;
             progressBar.setValue(0);
             progressBar.setVisible(true);
             txt.setText("Copying " + src.getName());
             keepStructure=keep;
             this.delete=delete;
          }

            @Override
            public Void doInBackground() throws Exception
            {
                txt.setText(src.getName());
                //retrieveTotalBytes(src);
                copyFiles(src, dest);

                return null;
            }

            @Override
            public void process(java.util.List<Integer> chunks)
            {
                for(int i : chunks)
                {
                    progressBar.setValue(i);

                }
            }

            @Override
            public void done()
            {
                setProgress(100);
            }


            public String GetParent(String input){
                short pos=(short) input.lastIndexOf(File.separatorChar);
                return input.substring(0, pos);
            }

            private void copyFiles(File sourceFile, File targetFile) throws IOException
            {
                if(sourceFile.isDirectory())
                {
                    if(!targetFile.exists()) targetFile.mkdirs();

                    String[] filePaths = sourceFile.list();

                    for(String filePath : filePaths)
                    {
                        File destFile;
                        File srcFile = new File(sourceFile, filePath);

                        if(keepStructure==true)
                            destFile= new File(targetFile, filePath);
                        else{
                            String filepath2=GetParent(dest.toString())+File.separatorChar+srcFile.getName();
                            destFile=new File(filepath2);
                        }

                        System.out.print("\n\n name="+destFile.toString()+"\n");
                        System.out.print("dest to string =" +GetParent(dest.toString()) + "  srcFile.getName()="+srcFile.getName()+"\n" );

                        copyFiles(srcFile, destFile);
                    }
                }
                else
                {
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceFile));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile));

                    long fileBytes = sourceFile.length();
                    long soFar = 0L;

                    int theByte;

                    while((theByte = bis.read()) != -1)
                    {
                        bos.write(theByte);

                        setProgress((int) (copiedBytes++ * 100 / totalBytes));
                        publish((int) (soFar++ * 100 / fileBytes));
                    }

                    bis.close();
                    bos.close();
                    if(delete==true)
                        sourceFile.delete();
                    publish(100);
                    txt.setText("Copying " + src.getName() + "complete");
                  }
            } 
       }
}

问题1: 注意,在该文件中,我有两个子类{FileCopy,Copy},它们完全不同。这是组织代码的好方法,还是应该在每个文件上移动每个类

问题2: 另外,在我的主要工作中,我尝试从每个类创建和创建对象,但我做了一些错误的事情。我已经添加了文件的导入,但是当我尝试创建一个对象时,例如

Copy worker = new Copy(source,dest,progressBar,textArea, keep_Structure,false);

我收到这个错误:

No enclosing instance of type Utilities is accessible. Must qualify the allocation with an enclosing instance of type Utilities (e.g. x.new A() where x is an instance of Utilities).


共 (1) 个答案

  1. # 1 楼答案

    在Java中,您应该(至少在您还在学习基础知识的时候)将每个类保存在自己的文件中

    您的文件中有3个(而不是2个)类:UtilitiesFileCopyCopy,后两个是Utilities的内部类(Utilities类本身不做任何事情)。这就是为什么不先实例化Utilities就不能实例化Copy

    我认为您应该有一个名为utilities的包,其中包含两个文件:FileCopy.javaCopy.java,每个文件都包含自己的类。如果您想要一种方法来区分应用程序的各个部分,那么这是一个很好的开始:有一个包含所有gui相关类的包,另一个包用于应用程序的其余部分

    这将解决您的错误

    嵌套类的官方教程:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html