Topic: 一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗?

  Print this page

1.一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? Copy to clipboard
Posted by: xiemingyou
Posted on: 2005-08-11 09:50

一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗?还是得分解成几步来完成?

2.Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re: xiemingyou] Copy to clipboard
Posted by: ranchgirl
Posted on: 2005-08-11 11:34

Take a look at ANT Move task, it might help. I use it all the time, but have not looked at the Move source code.

http://ant.apache.org

3.Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re: xiemingyou] Copy to clipboard
Posted by: jacky_zhangyue
Posted on: 2005-08-27 13:50

用FileReader类和FileWriter类来处理,应该就可以了

4.Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re: xiemingyou] Copy to clipboard
Posted by: aleel_008
Posted on: 2005-08-27 15:33

I think the best way is to use the os invoke,such as the bat file.
I could not image some case that we have to use the primitive api to move a file instead of to utilize the os invoke

5.Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re: xiemingyou] Copy to clipboard
Posted by: q_yuan
Posted on: 2005-09-05 16:03

没有,我没有看到!如果想这样的话,使用输出流和输入流应该可以的!

6.Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re: xiemingyou] Copy to clipboard
Posted by: zcjl
Posted on: 2005-09-05 20:19

File a = new File("F:\\a.txt");
File b = new File("F:\\test\\a.txt");
System.out.println(a.renameTo(b));
System.out.println(a.getPath());
System.out.println(a.exists());
System.out.println(b.getPath());
System.out.println(b.exists());

前提:F:\test这个目录存在

7.Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re: xiemingyou] Copy to clipboard
Posted by: q_yuan
Posted on: 2005-09-06 09:28

我这儿有一个程序,你看一下。我这是把一个特定目录下的文件复制到自己选定的目录下,并且可以改文件名。
package test_Class;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class copy_File extends JFrame implements ActionListener{
  private JButton bnt_send = new JButton("发送文件");
  private FileDialog fd = new FileDialog(this,"另存为",FileDialog.SAVE);
  File file = new File("D:\\javaexe\\copy_File.java");
  public copy_File(){
    initCompoents();
    setBounds(100,100,200,200);
    }
  public void initCompoents(){
    setTitle("TEST");
    bnt_send.setBounds(new Rectangle(50,50,100,20));
    bnt_send.addActionListener(this);
    fd.setDirectory("E:\\");
    fd.setFile("copy_File.java");
    getContentPane().setLayout(null);
    getContentPane().add(bnt_send,null);
    addWindowListener(new WindowAdapter(){
      public void windowClosing(WindowEvent evt){
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }
      });
    }
  public void actionPerformed(ActionEvent evt){
    FileInputStream fin = null;
    FileOutputStream fout = null;
    String s ,fileName;
    try{
      fd.setVisible(true);
      fileName = fd.getDirectory()+fd.getFile();
      fin = new FileInputStream(file);
      byte ss[] = new byte[fin.available()];
     fin.read(ss);
     s = new String(ss);
     fout = new FileOutputStream(fileName);
     fout.write(s.getBytes());
        }catch(Exception ex){}
      finally {
        try{
          fout.close();
          fin.close();
          }catch(IOException ex){}
        }
    }
  public static void main(String[] arg){
    new copy_File().show();
    }
  }

8.Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re: gongshi] Copy to clipboard
Posted by: zcjl
Posted on: 2005-09-06 14:17

gongshi wrote:
Take a look at ANT Move task, it might help. I use it all the time, but have not looked at the Move source code.

http://ant.apache.org


ant的move task也是使用的srcFile.renameTo(File destFile)方法
不过对于dest多了一项检查并创建新文件夹的动作:

File parent = destFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}


完整方法摘抄如下:

protected boolean renameFile(File sourceFile, File destFile,
boolean filtering, boolean overwrite)
throws IOException, BuildException {

boolean renamed = true;
if ((getFilterSets() != null && getFilterSets().size() > 0)
|| (getFilterChains() != null && getFilterChains().size() > 0)) {
renamed = false;
} else {
if (!filtering) {
// ensure that parent dir of dest file exists!
File parent = destFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}

if (destFile.exists()) {
if (sourceFile.isDirectory()) {
throw new BuildException(
new StringBuffer("Cannot replace ").append(
((destFile.isFile()) ? "file " : "directory ")).append(
destFile).append(" with directory ").append(
sourceFile).toString());
} else if (destFile.isFile() && !destFile.delete()) {
throw new BuildException("Unable to remove existing "
+ "file " + destFile);
}
}
renamed = sourceFile.renameTo(destFile);
} else {
renamed = false;
}
}
return renamed;
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923