Java JFileChooser提供的字符串文件名无效
在我的应用程序中,我想从给定的userinput压缩一个文件或文件夹。当我尝试从JDialog获取输入时,它工作得很好,但是如果我想让用户从文件选择器中选择,我的程序不会工作——它总是创建一个空的zip文件。你能教我怎么修吗 编辑:当我试图通过JDialog获取filename和outputname时,工作正常,但是当我想通过filechooser选择文件名时,我无法以正确的方式将其传递给我的后续函数。也许是因为目录分隔符?它写入文件名和文件路径,但当我传递它时,它将无法工作Java JFileChooser提供的字符串文件名无效,java,swing,jfilechooser,Java,Swing,Jfilechooser,在我的应用程序中,我想从给定的userinput压缩一个文件或文件夹。当我尝试从JDialog获取输入时,它工作得很好,但是如果我想让用户从文件选择器中选择,我的程序不会工作——它总是创建一个空的zip文件。你能教我怎么修吗 编辑:当我试图通过JDialog获取filename和outputname时,工作正常,但是当我想通过filechooser选择文件名时,我无法以正确的方式将其传递给我的后续函数。也许是因为目录分隔符?它写入文件名和文件路径,但当我传递它时,它将无法工作 import ja
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;
您不检查返回值。 请阅读: JFileChooser为用户提供了一种简单的机制来选择 文件有关使用JFileChooser的信息,请参阅如何使用文件 选择器,Java教程中的一部分。下面的代码弹出一个 只能看到.jpg和.jpg的用户主目录的文件选择器 .gif图像:
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");
}
它应该做什么,它实际上做什么?那么你说的是“它应该工作,但它不工作”,对吧,现在?如果我知道的更多,我就不会问了。。但我试图编辑我的问题。让我们看看你不知道当你运行程序时会发生什么?请再次查看我的问题我现在写的更具体,你甚至可以查看我的完整代码。也许你现在可以看到问题了。谢谢!这一分钟,我尝试了if(result==JFileChooser.APPROVE_OPTION){函数,并看到了它的工作原理。非常感谢!这样对我来说不起作用,但正确的答案是检查返回值。谢谢。但是无论如何,文件名似乎无法正确传递,因为创建的zip文件夹是空的。