Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区 » 编程/算法/API  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗?
xiemingyou





发贴: 2
积分: 0
于 2005-08-11 09:50 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗?还是得分解成几步来完成?


作者 Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re:xiemingyou]
ranchgirl



版主


发贴: 801
积分: 132
于 2005-08-11 11:34 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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



作者 Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re:xiemingyou]
jacky_zhangyue





发贴: 1
积分: 0
于 2005-08-27 13:50 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用FileReader类和FileWriter类来处理,应该就可以了


作者 Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re:xiemingyou]
aleel_008





发贴: 43
积分: 0
于 2005-08-27 15:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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



作者 Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re:xiemingyou]
q_yuan





发贴: 174
积分: 3
于 2005-09-05 16:03 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
没有,我没有看到!如果想这样的话,使用输出流和输入流应该可以的!


作者 Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re:xiemingyou]
zcjl

涅槃



发贴: 537
积分: 65
于 2005-09-05 20:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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这个目录存在



作者 Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re:xiemingyou]
q_yuan





发贴: 174
积分: 3
于 2005-09-06 09:28 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我这儿有一个程序,你看一下。我这是把一个特定目录下的文件复制到自己选定的目录下,并且可以改文件名。
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();
    }
  }



作者 Re:一个路径下的文件剪切到另一个路径下面,java中有直接的api方法吗? [Re:gongshi]
zcjl

涅槃



发贴: 537
积分: 65
于 2005-09-06 14:17 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
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;
}




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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