在JAR中写入文件方法 [英] Write To File Method In JAR

查看:124
本文介绍了在JAR中写入文件方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用此方法从JAR中的文本文件中正确读取并正常工作。

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();
        }

但是java.lang.IllegalArgumentException有ocure:

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)

我的文件在Classpath中。

My File is in Classpath.

谢谢。

推荐答案

你做不到。

jar是一个文件本身,而不是一个目录。它包含java知道如何阅读的资源。但是,jar中的资源不是文件,因此不能用作文件。据我所知,将新数据写入jar的唯一方法是获取内容,进行更改,并创建一个全新的jar。

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.

这篇关于在JAR中写入文件方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆