2014-02-10 81 views
-4

我试图写一个程序,拷贝一个文件,并复制它的内容到另一个。我必须让用户选择文件。我卡住了,可以帮助我。有什么不对我的JFileChooser程序

import java.io.IOException; 
import java.nio.file.CopyOption; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
import java.nio.file.StandardCopyOption; 

import javax.swing.JFileChooser; 

public class FileCopy { 
    public static void main(String[]Args) throws IOException { 
     JFileChooser chooser = new JFileChooser("/Users/josealvarado/Desktop/"); 

     Path FROM = Paths.get(chooser); 
     Path TO = Paths.get(chooser); 

     //overwrite existing file, if exists 
     CopyOption[] options = new CopyOption[]{ 
      StandardCopyOption.REPLACE_EXISTING, 
      StandardCopyOption.COPY_ATTRIBUTES 
     }; 
     Files.copy(FROM, TO, options); 
    } 
} 
+6

你得到什么错误? – Salah

回答

1

我只能猜测,但我认为你需要一个JFrame其中将包含您的JFileChooser,我犯了一个小例子,没有任何功能,只是为了展示,你怎么可以或许达到自己的目标。

请了解你的下一个问题(S)在这里SO,上传你尝试和POST错误/异常你,否则就很难帮助或解决您的问题!

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package de.professional_webworkx.tutorial.jtable.view; 

import java.awt.BorderLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JFileChooser; 
import javax.swing.JFrame; 

/** 
* 
* @author ottp 
*/ 
public class MainFrame extends JFrame { 

    private JFileChooser chooser; 

    public MainFrame() { 
     initGUI(); 
    } 

    private void initGUI() { 

     chooser = new JFileChooser(); 
     chooser.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println(chooser.getSelectedFile().getName()); 
      } 
     }); 
     this.setTitle("FileChoosing and copy one file to another"); 
     this.setSize(1024, 768); 
     this.getContentPane().add(chooser, BorderLayout.NORTH); 
     this.setVisible(true); 
    } 
} 

的班,开始你的App

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 

package de.professional_webworkx.tutorial.jtable; 

import de.professional_webworkx.tutorial.jtable.view.MainFrame; 
import javax.swing.JFileChooser; 

/** 
* 
* @author ottp 
*/ 
public class JTableDemo { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     new MainFrame(); 

    } 

} 

希望这有助于 帕特里克

+0

感谢您的意见。这是我在这个网站上的第一篇文章,但我会尽量让下一次更清楚。 – user3213435

1
Path FROM = Paths.get(chooser); 
    Path TO = Paths.get(chooser) 

你不能传递一个JFileChooserPaths.get()。以下是过载的静态方法

  • Paths.get(String first, String... more) - 将路径字符串或连接时形成路径字符串的字符串序列转换为路径。
  • Paths.get(URI uri) - 将给定的URI转换为Path对象。

你可能希望传递一个字符串。为此,您需要从JFileChooser获得String文件路径。要做到这一点,首先需要chooser.showOpenDialog()它返回一个int如果OK按钮选择文件(APPROVE_OPTION)后按下,所以,你想要做这样的事情

JFileChooser chooser = new JFileChooser(); 
int result = chooser.showOpenDialog(null); 
String path; 
if (result == JFileChooser.APPROVE_OPTION) { 
    path = (chooser.getSelectedFile()).getAbsolutePath(); 
} 

然后,你可以通过pathPaths.get(path)

你真的应该看看How to Use File Choosers

+0

感谢您的回复我尝试阅读filechooser API,但不太明白它。 – user3213435