有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java文件名作为JFileChooser提供的字符串不起作用

在我的应用程序中,我想从给定的userinput压缩一个文件或文件夹。当我尝试从JDialog获取输入时,它工作得很好,但是如果我想让用户从文件选择器中选择,我的程序不会工作——它总是创建一个空的zip文件。你能教我怎么修吗

编辑:当我试图通过JDialog获取filename和outputname时,工作正常,但是当我想通过filechooser选择文件名时,我无法以正确的方式将其传递给我的后续函数。也许是因为目录分隔符?它写入文件名和文件路径,但当我传递它时,它将无法工作

import java.io.*;
import java.util.zip.*;
import javax.swing.*;
import javax.swing.JFileChooser;

public class Zipper { 

int prefixLength;
  ZipOutputStream zipOut;
  byte[] ioBuffer = new byte[4096];

  public Zipper(String dirFileName, String dirFileOutput) throws Exception
  { prefixLength = dirFileName.lastIndexOf("/") + 1;
    zipOut = new ZipOutputStream(new FileOutputStream("./" + dirFileOutput + ".zip"));
    createZipFrom(new File(dirFileName));
    zipOut.close();
  }

  void createZipFrom(File dir) throws Exception
  { if (dir.exists() && dir.canRead() && dir.isDirectory())
    { File[] files = dir.listFiles();
      if (files != null)
      { for (File file: files)
        { if (file.isDirectory()) 
          { createZipFrom(file);
          }
          else
          { String filePath = file.getPath();//.replace('\\', '/');
            FileInputStream in = new FileInputStream(filePath);
            zipOut.putNextEntry(new ZipEntry(filePath.substring(prefixLength)));
            int bytesRead;
            while ((bytesRead = in.read(ioBuffer)) > 0) 
            { zipOut.write(ioBuffer, 0, bytesRead);
            }
            System.out.println(filePath + " added\n");
            zipOut.closeEntry();
            in.close();
          }
        }
      }
    }
  }

  public static void main(String[] args) throws Exception {

    JFileChooser fileChooser = new JFileChooser();
    fileChooser.showOpenDialog(null);
    File selectedFile = fileChooser.getSelectedFile();
    System.out.println(selectedFile.getPath());

    String dirFileName = selectedFile.getPath(); // should come from the fileChooser but isnt working

    String dirFileOutput = JOptionPane.showInputDialog(null, "packetname"); // thats working..

    System.out.println(dirFileName);
    System.out.println(dirFileOutput);

    new Zipper(dirFileName, dirFileOutput);

    System.out.println("package " + dirFileOutput + "." + ".zip created\n");

  }

}

编辑:我让它运行的变化

prefixLength = dirFileName.lastIndexOf("/") + 1;

对此

prefixLength = dirFileName.lastIndexOf("\\") + 1;

共 (1) 个答案

  1. # 1 楼答案

    不检查返回值。 请阅读JFileChooserDialog javadoc

    JFileChooser provides a simple mechanism for the user to choose a file. For information about using JFileChooser, see How to Use File Choosers, a section in The Java Tutorial. The following code pops up a file chooser for the user's home directory that sees only .jpg and .gif images:

    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "JPG & GIF Images", "jpg", "gif");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
    }
    

    以下代码适用于我:

            // ...
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showOpenDialog(appFrame);
            if (result == JFileChooser.APPROVE_OPTION) {
                File selectedFile = fileChooser.getSelectedFile();
                System.out.println(selectedFile.getPath());
    
                String dirFileName = selectedFile.getPath(); // should come from the fileChooser but isnt working
    
                String dirFileOutput = JOptionPane.showInputDialog(null, "packetname"); // thats working..
    
                System.out.println(dirFileName);
                System.out.println(dirFileOutput);
    
                System.out.println("package " + dirFileOutput + "." + ".zip created\n");
            }