JAVA GUI中非常缓慢的文本文件拆分

JAVA GUI中非常缓慢的文本文件拆分,java,swing,user-interface,optimization,Java,Swing,User Interface,Optimization,我有一个GUI,我想从中打开一个文件。逻辑如下:首先,程序请求一个目录,在其中为临时文件创建一个新文件夹。然后,它要求一个包含数据的文件(该文件的一行看起来像这样,例如:-20-202435.52421.4)。然后程序继续将该文件拆分为较小的文本文件。这是我想要的,但需要很长时间。我不知道如何使它更快。谁能给我一些建议,或者解释一下,为什么要花这么长时间?我以前在GUI外部使用过分割文件的方法,我没有遇到这个问题,所以我想GUI应该对此负责。 如果有任何帮助,我将不胜感激 源代码: public

我有一个GUI,我想从中打开一个文件。逻辑如下:首先,程序请求一个目录,在其中为临时文件创建一个新文件夹。然后,它要求一个包含数据的文件(该文件的一行看起来像这样,例如:-20-202435.52421.4)。然后程序继续将该文件拆分为较小的文本文件。这是我想要的,但需要很长时间。我不知道如何使它更快。谁能给我一些建议,或者解释一下,为什么要花这么长时间?我以前在GUI外部使用过分割文件的方法,我没有遇到这个问题,所以我想GUI应该对此负责。 如果有任何帮助,我将不胜感激

源代码:

public void OpeningFile() throws FileNotFoundException, IOException {

    File folder;
    String filepath;
    Object[] options = {"Ok","Cancel"};  

    int n = JOptionPane.showOptionDialog(null,
        "Choose a folder in which temporary files will be created, \n then open a text file with Raman Mapping",
        "Information",
        JOptionPane.YES_NO_CANCEL_OPTION,
        JOptionPane.DEFAULT_OPTION,
        null,
        options,
        options[1]);

    if (n == 0) {

    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    fc.setApproveButtonText("Stwórz");
    int returnVal = fc.showOpenDialog(Main.this);


    if (returnVal == JFileChooser.APPROVE_OPTION) {
        folder = fc.getSelectedFile();
        filepath=folder.getAbsolutePath();
        File newFolder=new File(filepath+"/Temp_Raman");
        newFolder.mkdir();

        int returnVal2 = fc2.showOpenDialog(Main.this);


        if (returnVal2 == JFileChooser.APPROVE_OPTION) {
           dane=fc2.getSelectedFile();
           String dane1=dane.getAbsolutePath();
           System.out.println(dane1);
           System.out.println(filepath);
           divideTxt(dane, filepath);
        }
        else{ 
            System.out.println("Cancelled");
        }

    }
    else{ 
        System.out.println("Cancelled");
    }



           // ZAMYKANIE PLIKU:
           try {
             fr.close();
            } catch (IOException e) {
              e.printStackTrace();
            } 
    }
      else{           
        System.out.println("cancelled");       
        }

}

private static void divideTxt(File dane, String filepath) throws FileNotFoundException, IOException{

       List<Integer> XX= new ArrayList<Integer>();
       List<Integer> YY= new ArrayList<Integer>();


       int a=1;
       List<Integer> x= new ArrayList<Integer>();
       List<Integer> y= new ArrayList<Integer>();
       List<Double> t= new ArrayList<Double>();
       List<Double> u= new ArrayList<Double>();


       Scanner sc = new Scanner(dane);
            while (sc.hasNext()) {
              x.add(sc.nextInt());
              y.add(sc.nextInt());
              t.add(sc.nextDouble());
             u.add(sc.nextDouble());
              //sc.nextLine();
          }




       File danePodzielone=new File(filepath+"/Temp_Raman/dane"+a+".txt");
       danePodzielone.createNewFile();
       XX.add(x.get(0));
       YY.add(y.get(0));
       for(int i=0;i<x.size()-1;i++)
       {

           if(x.get(i+1) !=x.get(i)){
               a++;
               danePodzielone=new File(filepath+"/Temp_Raman/dane"+a+".txt");
               danePodzielone.createNewFile();
               XX.add(x.get(i+1));
           }
           else {
           if(y.get(i+1)!=y.get(i)){
               a++;
               danePodzielone=new File(filepath+"/Temp_Raman/dane"+a+".txt");
               danePodzielone.createNewFile();
               YY.add(y.get(i+1));
           }
           else{
              FileWriter fw = new FileWriter(filepath+"/Temp_Raman/dane"+a+".txt",true);


              PrintWriter pw = new PrintWriter(fw);
              pw.write(t.get(i)+" "+u.get(i)+"\n");
              pw.flush();
              pw.close();
           }
           }
        }

      }
public void OpeningFile()抛出FileNotFoundException、IOException{
文件夹;
字符串文件路径;
对象[]选项={“确定”,“取消”};
int n=JOptionPane.showOptionDialog(null,
“选择将在其中创建临时文件的文件夹,\n然后打开带有Raman映射的文本文件”,
“信息”,
JOptionPane.YES\u NO\u CANCEL\u选项,
JOptionPane.DEFAULT_选项,
无效的
选项,
期权[1]);
如果(n==0){
fc.setFileSelectionMode(仅限JFileChooser.DIRECTORIES_);
fc.setApproveButtonText(“Stwórz”);
int returnVal=fc.showOpenDialog(Main.this);
if(returnVal==JFileChooser.APPROVE_选项){
folder=fc.getSelectedFile();
filepath=folder.getAbsolutePath();
File newFolder=新文件(filepath+“/Temp_”);
mkdir();
int returnVal2=fc2.showOpenDialog(Main.this);
if(returnVal2==JFileChooser.APPROVE_选项){
dane=fc2.getSelectedFile();
字符串dane1=dane.getAbsolutePath();
系统输出打印LN(dane1);
System.out.println(文件路径);
divideTxt(丹麦,文件路径);
}
否则{
系统输出打印项次(“取消”);
}
}
否则{
系统输出打印项次(“取消”);
}
//ZAMYKANIE PLIKU:
试一试{
fr.close();
}捕获(IOE异常){
e、 printStackTrace();
} 
}
否则{
系统输出打印项次(“取消”);
}
}
私有静态void divideTxt(File dane,String filepath)抛出FileNotFoundException,IOException{
列表XX=新的ArrayList();
List YY=new ArrayList();
INTA=1;
列表x=新的ArrayList();
列表y=新的ArrayList();
List t=new ArrayList();
列表u=新的ArrayList();
扫描仪sc=新扫描仪(丹麦);
while(sc.hasNext()){
x、 添加(sc.nextInt());
y、 添加(sc.nextInt());
t、 添加(sc.nextDouble());
u、 添加(sc.nextDouble());
//sc.nextLine();
}
File danePodzielone=新文件(filepath+“/Temp_Raman/dane”+a+“.txt”);
danePodzielone.createNewFile();
二十、 加(x.get(0));
YY.add(y.get(0));

对于(inti=0;如果希望更快地获得帮助,请发布一个(最小的完整测试和可读示例)。让代码生成输入文件。1)对代码块使用一致的逻辑缩进。代码缩进旨在帮助人们理解程序流程。2)源代码中的一行空白总是足够的。在
{
之后或
}
之前的空行通常也是多余的。3)“代码>//ZAMYKANIE PLIKU:
”对于不懂该语言的人来说,这些类型的注释是完全无用的。请翻译成英语。4)从命令行执行文件拆分时,文件拆分速度快吗?尝试输入一些时间戳调试输出,以便您可以看到代码在哪里慢。如果您的拆分代码快,而在添加GUI后又慢,为什么要这样做是否发布了拆分代码而不是GUI代码?:-)