如果用户未能选择单选按钮,我想从我的 JAR 中复制一个 PDF 文件。我遇到的问题是控制台报告 java.io.FileNotFoundException: file:/home/geeky/workspace/TabSync/bin/ndg/co/uk/TabSync/nullpdf.pdf (No such file or directory) 即使文件在那里。如果我将声明为 No such file... 的路径复制到 Thunar 中,则文件会立即打开,因此路径是正确的。
代码如下:
URL nopathbrain = getClass().getResource("/ndg/co/uk/TabSync/nullpdf.pdf");
////////////////////////////////////////////////////////////////////////////////////////
public void actionPerformed(ActionEvent arg0) {
try{
//set the busy cursor while building the sync files
setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
if (radioButton1.isSelected()) {
filepathbrain1 = txtPath1.getText();
System.out.println(filepathbrain1);
} else {
System.out.println(nopathbrain);
filepathbrain1 = String.valueOf(nopathbrain);
}
copysource();
copysource() 看起来像这样
public void copysource() {
try{
File afile =new File(filepathbrain1);
File bfile =new File(buildpath +"/documents/1.pdf");
FileInputStream inStream = new FileInputStream(afile);
FileOutputStream outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
} //end while
inStream.close();
outStream.close();
jar -tvf 的输出是:
0 Sun Mar 31 09:51:56 BST 2013 ngd/
0 Sun Mar 31 09:51:56 BST 2013 ngd/co/
0 Sun Mar 31 09:51:56 BST 2013 ngd/co/uk/
0 Sun Mar 31 09:51:56 BST 2013 ngd/co/uk/TabSync/
806 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About$1.class
556 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About$2.class
4762 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/About.class
1802 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/Help.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$1.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$10.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$11.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$12.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$13.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$14.class
1117 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$15.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$16.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$17.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$18.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$19.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$2.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$20.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$21.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$22.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$23.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$24.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$25.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$26.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$27.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$28.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$29.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$3.class
735 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$30.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$31.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$32.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$33.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$34.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$35.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$36.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$37.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$38.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$39.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$4.class
1120 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$40.class
4765 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$41.class
723 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$42.class
1185 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$43.class
697 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$44.class
1220 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$45.class
995 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$46.class
781 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$47.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$5.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$6.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$7.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$8.class
733 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$9.class
2097 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main$Field1Listener.class
47217 Sun Mar 31 09:52:54 BST 2013 ngd/co/uk/TabSync/Main.class
1260 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/FileFiltererpdf.class
1260 Sun Mar 31 08:53:20 BST 2013 ngd/co/uk/TabSync/FileFiltererjpg.class
991552 Wed Jul 27 19:13:00 BST 2011 ngd/co/uk/TabSync/Coalition_v2.ttf
991552 Wed Jul 27 19:13:00 BST 2011 Coalition_v2.ttf
409717 Thu Mar 21 14:15:32 GMT 2013 nullpdf.pdf
我遇到的问题是,当未选择单选按钮时,JAR 中的 PDF 副本不起作用。如果我在桌面上提供文件的手动路径,那么副本工作正常(所以我很高兴 copysource() 工作正常,它只是为了完整性而包含在内)。
在这里编辑:
我现在正在尝试通过临时文件访问 pdf,因为似乎我应该使用 Stream 来获取资源,但我仍然收到文件未找到异常。代码:
//write the null file to tmp for copy
try {
java.io.InputStream iss = getClass().getResourceAsStream("/nullpdf.pdf");
byte[] data = new byte[iss.available()];
iss.read(data);
iss.close();
String tempFile = "file";
File temp = File.createTempFile(tempFile, ".pdf");
FileOutputStream fos = new FileOutputStream(temp);
fos.write(data);
fos.flush();
fos.close();
nopathbrain = tempFile +".pdf";
} catch (IOException ex) {
ex.printStackTrace();
System.out.println("TEMP FILE NOT CREATED - ERROR in brainradiocheck");
}
我认为临时文件创建正常,但是如何将其文件路径作为字符串返回?我想将临时文件的文件路径设置为 nopathbrain。
安迪