在JAR中写入文件方法



我正在使用这种方法从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:WindowsSystem32>java -jar "C:UserskhoyDocumentsNetBeansProjectsJavaApplication6distJavaApplication6.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.

谢谢。

你不能那样做。

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

相关内容

  • 没有找到相关文章

最新更新