从 JFileChooser 给出的作为字符串的文件名将不起作用
Filename as a String given from JFileChooser wont work
在我的应用程序中,我想从给定的用户输入中压缩文件或文件夹。当我尝试从 JDialog 获取输入时,一切正常,但如果我想让用户从 fileChooser 中进行选择,我的程序将无法工作——它总是创建一个空的 zip 文件。请教我如何解决这个问题?
编辑:当我尝试通过 JDialog 获取文件名和输出名时,这工作正常,但是当我想通过文件选择器选择文件名时,我无法以正确的方式将它传递给我的进一步功能。也许是因为目录分隔符?它会写入文件名和文件路径,但是当我传递它时它不会工作。
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;
您没有检查 return 值。
请阅读 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");
}
在我的应用程序中,我想从给定的用户输入中压缩文件或文件夹。当我尝试从 JDialog 获取输入时,一切正常,但如果我想让用户从 fileChooser 中进行选择,我的程序将无法工作——它总是创建一个空的 zip 文件。请教我如何解决这个问题?
编辑:当我尝试通过 JDialog 获取文件名和输出名时,这工作正常,但是当我想通过文件选择器选择文件名时,我无法以正确的方式将它传递给我的进一步功能。也许是因为目录分隔符?它会写入文件名和文件路径,但是当我传递它时它不会工作。
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;
您没有检查 return 值。 请阅读 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");
}