问题
I Am using This Method to Read From a text file in JAR And Work Correctly.
BufferedReader Bbr=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream("AllBookRecords.txt")));
Now, I want to Write To a file (Delete a line from file, By writing from first file to second file without specific line).
I use This code for do this:
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();
}
But java.lang.IllegalArgumentException has ocure:
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)
My File is in Classpath.
Thank You.
回答1:
You can't do that.
A jar is a file itself, not a directory. It contains resources that java knows how to read. A resource in a jar, however, is not a file, and so can't be used as one. The only way to write new data into a jar, to my knowledge, is to get the contents, make your changes, and create a whole new jar.
来源:https://stackoverflow.com/questions/14403745/write-to-file-method-in-jar