The origin of a problem
Sometimes when we do some projects, we need to write some configuration information to the configuration file for the call. Let's say this:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/79/34/wKiom1aLbvLQhC4eAAAVhpYSRkA317.png "title=" 20160104220214_93791.png "alt=" Wkiom1albvlqhc4eaaavhpysrka317.png "/>
If we need to read the configuration information in the Config.txt file in Demo.java, it can be read using file in this project directory, but after packaging it into a jar, using file will not be able to read to Config.txt unless the configuration information is placed outside the jar package.
Let's say we use file to read files:
BufferedReader reader = new BufferedReader (new FileReader (New File ("Res/config.txt"));
There is no problem in the project state, but the package is packaged as a jar to perform an error:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/79/34/wKiom1aLby7hqUPdAABAzNiL0Uc895.png "title=" error "alt = "Wkiom1alby7hqupdaabaznil0uc895.png"/>
It is obvious that the specified file could not be found, why does this problem occur? Because this time the entire JAR file is a file, of course, cannot find the configuration file in the jar package
Let's continue testing, create a new res folder in the "Test 0.jar" sibling directory, put a config.txt in it, and continue running:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/79/33/wKioL1aLb4Kxt_oaAAEEpSLIQBI941.png "style=" float: none; "title=" 20160104221449_53393.png "alt=" Wkiol1alb4kxt_oaaaeepsliqbi941.png "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/79/34/wKiom1aLb13BJ1TrAACr7MeOdgw752.png "style=" float: none; "title=" 20160104221506_44420.png "alt=" Wkiom1alb13bj1traacr7meodgw752.png "/>
Now we can see that the program executes without error, it can be run normally
Two problems extension
Someone might want to ask, if I have to package the configuration file into the jar, is there any way to access it? The answer is yes. Use this.getclass (). getResourceAsStream ("/config.txt"); it's OK. The complete fetch input stream is this:
InputStream InputStream = This.getclass (). getResourceAsStream ("/config.txt"); BufferedReader reader = new BufferedReader (new InputStreamReader (InputStream, "UTF-8"));
Got BufferedReader, and then it's normal to read the file.
import java.awt.dimension;import java.awt.font;import java.awt.gridbagconstraints;import java.awt.gridbaglayout;import java.awt.toolkit;import java.io.bufferedreader;import java.io.filenotfoundexception;import java.io.ioexception;import java.io.inputstream;import java.io.inputstreamreader;import javax.swing.jframe;import javax.swing.jlabel;import javax.swing.jpanel;import javax.swing.jscrollpane;import javax.swing.jtextarea;import Javax.swing.swingutilities;public class demo extends jframe{private gridbaglayout gridbag;private gridbagconstraints constraints;private jpanel mainjpanel;private jlabel tip;private jscrollpane jscrollpane;private jtextarea content;private font contentfont = new font ("Song Body", font.layout_no_limit_context, 16); //Body Font Public demo () {super ("Reference Jar package config file Demo");Dimension screensize = toolkit.getdefaulttoolkit (). Getscreensize ();screenSize = Toolkit.getdefaulttoolkit (). Getscreensize (); //screen Size setpreferredsize (new dimension (700, ), Int framewidth = this.getpreferredsize (). width; //Interface Width int frameheight = this.getpreferredsize (). height; //Interface Height setsize (framewidth,frameheight); SetLocation (( Screensize.width - framewidth) / 2, (screensize.height - frameheight) /  2);//Initialize Mainjpanel = new jpanel (); Tip = new jlabel ("Hint:"); JScrollPane = new jscrollpane (); Content = new jtextarea (5,10);//Layout gridbag = new gridbaglayout (); Constraints = new gridbagconstraints ();constraints.fill = Gridbagconstraints.both;mainjpanel.setlayout (gridbag);constraints.gridwidth = 0; Constraints.gridheight = 1;constraints.Weightx = 1; constraints.weighty = 0; gridbag.setconstraints (Tip, constraints); Tip.setfont (Contentfont); Mainjpanel.add (Tip);constraints.gridwidth = 0; constraints.gridheight = 1;constraints.weightx = 1; constraints.weighty = 1; gridbag.setconstraints (jscrollpane, constraints); JScrollPane.setFont ( Contentfont); Mainjpanel.add (JScrollPane); Content.setfont (Contentfont); Jscrollpane.setviewportview (content); Content.setlinewrap (True); Content.setwrapstyleword (True);try {inputstream inputstream = This.getclass (). getResourceAsStream ("/config.txt"); Bufferedreader reader = new bufferedreader (New inputstreamreader (InputStream, "UTF-8") ); string line = ""; while ((Line = reader.readline ()) != null) {Content.append ( line + "\ n");} Reader.close ();} catch (filenotfoundexception e) &nbsP {E.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} Add (Mainjpanel); setvisible (true); Setdefaultcloseoperation (jframe.exit_on_close);} Public static void main (String[] args) {swingutilities.invokelater (new Runnable ( ) {public void run () {new demo ();}});}}
Res/config.txt:
Test text
Note:
Although this approach implements the reading of files in the jar package, it is still a disadvantage that it is not possible to dynamically modify the values in the configuration file during program execution.
This article is from "Zifangsky's personal blog" blog, make sure to keep this source http://983836259.blog.51cto.com/7311475/1731724
Refer to the configuration file in the jar package after packaging into an executable jar package