0

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.

Sajad
  • 2,273
  • 11
  • 49
  • 92
  • 1
    Short answer, you can't. Longer answer, use the Search feature and read one of the [many existing questions](http://stackoverflow.com/search?q=write+file+in+jar) for this same topic. – parsifal Jan 18 '13 at 17:07
  • Can you recommend me a special way? – Sajad Jan 18 '13 at 17:49
  • Edit your question to describe what you're *trying to do*, not how you think you want to do it. There are many alternatives to rewriting your application's JAR. – parsifal Jan 18 '13 at 17:53
  • am totally confused Some say it's not possible, but some solutions are suggested! What is it correct? Finally, how to write a JAR file? – Sajad Jan 22 '13 at 15:03

1 Answers1

3

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.

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
  • About what? If you aren't really familiar with what a jar is, you can take a look at [Oracle's docs](http://docs.oracle.com/javase/tutorial/deployment/jar/), or, of course, [Wikipedia](http://en.wikipedia.org/wiki/JAR_(file_format)). – femtoRgon Jan 18 '13 at 19:20
  • am totally confused Some say it's not possible, but some solutions are suggested! What is it correct? Finally, how to write a JAR file? – Sajad Jan 22 '13 at 15:04
  • You can't just write to a file within an existing jar. It's an archive, very similar to a zip file. For how to unpack, write or otherwise handle jars, you could take a look at [java.util.jar](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/jar/package-summary.html) – femtoRgon Jan 22 '13 at 16:32