这是一个先复制文件再压缩文件的小程序,可是运行是发现不能压缩文件,且无异常抛出。注释掉红色代码,就能压缩了。为什么呢???大家快来看看啊。
public class App { /**
 * @param args
 */
public static void main(String[] args) {
copyFile("c:\\1.txt", "d:\\1.txt");
                         rarFile("d:\\yy.rar","d:\\1.txt");
}
/**
 * 复制单个文件
 * 
 * @param oldPath
 *            String 原文件路径 如:c:/fqf.txt
 * @param newPath
 *            String 复制后路径 如:f:/fqf.txt
 * @return boolean
 */
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { // 文件存在时
InputStream inStream = new FileInputStream(oldPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444]; while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错 ");
e.printStackTrace(); } }
/**
 * @param rarName
 *            压缩后的文件名带路径和后缀
 * @param filename
 *            被压缩的文件名带路径和后缀
 */
public void rarFile(String rarName, String fileName) {
Runtime rt = Runtime.getRuntime();
try {
String cmd = "rar a " + rarName + " " + fileName;
Process p = rt.exec(cmd);
p.waitFor();
} catch (IOException e) {
System.err.println("压缩文件" + fileName + "出错");
System.err.println(e.getMessage());
e.printStackTrace();
} catch (InterruptedException e) {
System.err.println("被中断");
e.printStackTrace();
} }}

解决方案 »

  1.   

    建议你先debug一下,自己先看看哪出问题了。
      

  2.   

    跑了一下copy没有问题啊,
    不过你main方法里不new这个类编译能通过???public static void main(String[] args) { 
      App app = new App();
      app.copyFile("c:\\1.txt", "d:\\1.txt"); 

      

  3.   

    楼主我教你怎么贴代码:
    1、将代码作良好的格式化,以方便阅读。
    2、在发帖文本框的上方单击字体颜色右边的按钮,选择 Java
    3、将代码粘贴到【code=Java】和【/code】之间。就会得到下面的效果:public class Hello {    public static void main(String[] args) {
            System.out.println("Hello!");
        }
    }
      

  4.   

    问题已经解决,copyFile()里输出文件没close。
    再次感谢大家。