我正在使用此方法从JAR中的文本文件读取并正确工作。
BufferedReader Bbr=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("AllBookRecords.txt")));
现在,我要写入文件(从文件删除一行,通过从第一个文件写入第二个文件而没有特定的行)。
我使用此代码执行此操作:
String Bookid=tf1.getText(); File Tf=new File("Boutput.txt"); URL myUrl=getClass().getResource("AllBookRecords.txt"); File file=new File(myUrl.toURI()); OutputStream myoutput=new FileOutputStream(file); PrintWriter pw2=new PrintWriter(myoutput); String Bs; while( (Bs=Bbr.readLine()) != null ){ String[] Ust=Bs.split(" "); String Bname=Ust[0]; String Bdate=Ust[1]; String id=Ust[2]; if(!id.equals(Bookid.trim())){ pw2.println(Bs); } } pw2.close(); Bbr.close(); file.delete(); Tf.renameTo(file); } catch(FileNotFoundException fnfe){ foundlable.setText("File Not Found"); } catch(IOException ioe){ foundlable.setText("IO Error"); ioe.printStackTrace(); } catch(URISyntaxException ue){ ue.printStackTrace(); }
但是java.lang.IllegalArgumentException发生了:
C:\Windows\System32>java -jar "C:\Users\khoy\Documents\NetBeansProjects\JavaApplication6\dist\JavaApplication6.jar" Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: URI is not hierarchical at java.io.File.<init>(File.java:363) at Library.ReadBookFileToListM$3.actionPerformed(ReadBookFileToListM.java:67) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236) at java.awt.Component.processMouseEvent(Component.java:6038) at javax.swing.JComponent.processMouseEvent(JComponent.java:3265) at java.awt.Component.processEvent(Component.java:5803) at java.awt.Container.processEvent(Container.java:2058) at java.awt.Component.dispatchEventImpl(Component.java:4410) at java.awt.Container.dispatchEventImpl(Container.java:2116) at java.awt.Component.dispatchEvent(Component.java:4240) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916) at java.awt.Container.dispatchEventImpl(Container.java:2102) at java.awt.Window.dispatchEventImpl(Window.java:2429) at java.awt.Component.dispatchEvent(Component.java:4240) at java.awt.EventQueue.dispatchEvent(EventQueue.java:599) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160) at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
我的文件在Classpath中。
谢谢。
你不能那样做。
jar本身就是文件,而不是目录。它包含Java知道如何读取的资源。但是,jar中的资源不是文件,因此不能用作文件。据我所知,将新数据写入jar的唯一方法是获取内容,进行更改并创建一个全新的jar。