JBuilder 9.0 development practical skills seven strokes

xiaoxiao2021-03-06  103

Synchronous Output Directory In the Java development process, sometimes we have to create some simple classes to test certain features, delete the source files from the disk after the test is complete, because we don't want this class to appear in the final product. However, although the source file has been deleted, compiled Class files may still be left on disk, and when there are other classes depend on this class, it may result in instability of the development environment. In JBuilder, we can set up a compilation option to synchronize (Synchronize) Java source files and corresponding classes, so once the Java source file is deleted, the corresponding Class file will also be deleted. Follow the steps below to set the synchronization option: Open Project | Project Properties ... Select the Compiler tab to see the following dialog: Output debugging information Many times, we have to output some simple variables to the standard output device to understand the program Operation. For example, adding the following code in the program: System.out.println ("Debugging information: The value of the variable is" value); when the writing and debugging code is over, the next step is to make the final compilation and package, At this time, you will be deleted all of these System.Out.println code. In order to avoid these troubles, we can introduce a logical variable DebugMode, set it to true, and check the value of this variable before outputting the debug information:

IF (debugmode) system.out.println ("debugging information: The value of the variable is" value);

After all debugging work, set the DebugMode variable to false so that the debug information will not be output when the program is running. However, this is not the best way to deal with similar debugging information, we can use the Diagnostic class to improve. The Diagnostic class has three methods related to the output stream. In addition to the standard System.err output stream, other different output streams (such as files or URLs) can also be given. Most importantly, when writing and debugging the work, we can rule out the Diagnostic class in formally compiling, eliminating all calls to the Diagnostic class method. Below is three ways to output information in the Diagnostic class: Print (String Message): Output Information Println: Output information, and append the end of the end. PrintLnc: The output with a row end value, the counter (its value is progressing) and TAB characters. Therefore, we can replace the above system.out.println:

Diagnostic.printlnc ("debugging information: The value of the variable is:" value);

The standard output devices of these output methods are System.err, but we can change it with the setLogStream (PrintStream log) method. For example, suppose to send the output to the log.txt file, simply insert the following code:

Diagnostic.SetLogStream (New Java.io.printStream (Newjava.IO.FileoutputStream ("Log.txt")));

Finally, during development, we only need to call a method to turn off all output:

Diagnostic.enable (false); Resource Bundle in Java, it is easy to use a string of direct embedded procedures as a resource. For example, as long as you save a string to a separate file, we can easily implement support for multiple languages ​​by creating different files. To save the "hard-coded" string to resource bundling files, standard Java API provides two ways: • Save the key-value pair to text files in the form of the property file. • Put the key-value to the String [] [] array object saved to the Java source file, the Java source file extension from Java.util.ListResourceBundle. The first method has better flexibility, and can modify string values ​​without recoiling. The second method has good performance, but each time you modify the string value, the Java source code file containing the key - value must recompile. JBuilder provides a third approach to saving a hard-coded string value, namely com.borland.jb.util.ArrayResourceBundle. The ArrayResourceBundle class saves the value of the key through a string [] array object, but does not contain the key, we can access the value by index, that is, the index of the first value is 0, the second value index is 1, and so on. Since the value can be accessed directly, the performance is quite good. But there are also shortcomings - the files that maintain the save value are difficult because there is no direct (intuitive) connection between the values ​​and keys. Exception Tracking in Java, using try..catch (. Finally) statement can capture and process exceptions, it is very convenient. The common abnormality processing of the CatCH block is to output an exception, such as output an exception to the log file. The Exception class has a printStackTrace () method, which is capable of outputting stack information from an abnormal method, and the default output location is System.err. However, sometimes we want to output the stack information elsewhere outside of System.err, such as outputting the stack information to email when an exception occurs, or is displayed in a dialog. The printstackTrace () method has several different types: · PrintStackTrace (), output to standard error flow. · PrintStractRace (PrintStream PS) output to PrintStream named PS. · PrintStackTrace (PrintWriter PW) output to PrintWriter named PW. We can save the stack information to the String object with the last PrintStackTrace () method. As long as you capture stack information in the String object, we can easily use this information anywhere in your application. The following code snippet demonstrates the specific implementation steps:

private String getStackTraceAsString () {// StringWriter stack information including StringWriter stringWriter = new StringWriter (); // must be packaged as StringWriter PrintWriter object, // to meet the requirements of printStackTrace PrintWriter printWriter = new PrintWriter (stringWriter); // Get Stack Information E.PrintStackTrace (PrintWriter); // Convert to String, and return to this stringStringBuffer error = StringWriter.getBuffer (); return error.toString ();}

Quick View JavaBean Basic Information

In JBuilder, it is very convenient to see the internal information of JavaBean. We can drag the JavaBean into the Design window and view the JavaBean properties and events via the Object Inspector. However, even if you don't pass the designer, we can also view the information of JavaBean, which is using BeanInsight (menu: Tools | BeanInSight). Click the Examine Bean button to start analysting the specified JavaBean, you can see the results of the analysis JavaBean in the BeanInsight Result area. If you want to know more detailed information, click on the "View Details ..." button.

Multiple line attribute values

The attribute file is a good way to save the application configuration information. With the property file, simply modify the simple properties text file when modifying the application parameters, without recompiling the entire application.

The property file saves data in the form of "key-value" pair. Each row starts with a key, plus one equal to the symbol, plus the value corresponding to the key. The key and value must be on the same line, this is important. However, sometimes, the value is very much, and if the value can be divided into multi-line storage, it will bring great convenience. Let's take a look at how to use multi-line properties.

First, the code segment below reads the test.properties property file:

try {Properties p = new Properties (); File file = new File ( "test.properties"); FileInputStream fis = new FileInputStream (file); p.load (fis);} catch (IOException ioex) {ioex.printStackTrace ( }

The LOAD () method of the Properties class requires an input stream as a parameter, where we have passed an FileInputStream object.

Next, we can get the property value from the Properties object by calling the getProperty () method. Let's take a look at the contents of the Test.Properties property file:

SINGLINE = Single Line Valuemultiline = this Example Shows How Wecan Write a PropertyValue over Multiple Lines in Aproperties File

This attribute file saves two properties, but the keys are SingleLine and Multiline. The value of SingLine is only one line, and the value of Multiline has multiple lines.

If we call the getProperty () method of the Properties object, and output the attribute value, the result will be:

transfer:

System.out.println ("SINGLINE?" P.GetProperty ("SingleLine"); System.out.Println ("Multiline?" P.GetProperty ("multiline"));

result:

SINGLINE? SINGLELINE VALUEMULTILINE? This Example Shows How We

The Multiline property contains only the first row value. So how do I solve this problem? In fact, it is very simple, just add the escape character "/" at the end of each line. The modified attribute file is as follows:

SingLINE = SINGLE LINE VALUEMUEMUEMULTILINE = this Example Shows How We / CAN WRITE A Property / Value over Multiple Lines In A / Properties File Review the value of the property file again, MultiLine's attribute value can be read out

Note the Synchronize Output DIR option. Select this option to implement the output directory synchronization, and JBuilder will ensure synchronization of the Java Class file each time JBuilder will guarantee the Java source file.

转载请注明原文地址:https://www.9cbs.com/read-105097.html

New Post(0)