JBUILDER development skills

xiaoxiao2021-03-06  117

Synchronous output directory

During the Java development process, sometimes we have to create some simple classes to test certain features, delete the source file 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:

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.

Output debugging information

Many times, we have to output some simple variables to standard output devices to understand the operation of the program. For example, add the following code to the program:

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

When the work of writing and debugging the code is over, the last compile and package will be made, then remove all 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. Here are three ways to use to output information in the Diagnostic class:

Print (String Message): Output Information

Println: Output information and adds a line 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 (New

Java.io.FileOutputStream ("log.txt")))); Finally, during development, we only need to call a method to close all output:

Diagnostic.enable (FALSE);

Resource bundle

In Java, it is easy to use a string that directly embedded to the program 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.

Abnormal tracking

In Java, it is very convenient to capture and process exceptions using try..catch (.. finally). 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 getstacktracesstring () {

// StringWriter will contain stack information

StringWriter StringWriter = new stringwriter ();

/ / Must encapsulate StringWriter into a 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 string

StringBuffer 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 profment ();

File File = New File ("Test.properties");

FileInputStream FIS = New FileInputStream (file);

p.Load (FI);

} catch (ioexception oEx) {

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 Value

MultiLine = this Example Shows HOW WE

Can Write a Property

Value over Multiple Lines in A

Properties 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: call:

System.out.println ("SINGLINE?" P.GetProperty ("SingLINE"));

System.out.println ("Multiline?" P.GetProperty ("Multiline"));

result:

SINGLINE? SINGLE LINE VALUE

Multiline? 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: SingleLine = Single Line Value

Multiline = this Example Shows How WE /

Can Write a Property /

Value over Multiple Lines in A /

Properties file

Read the value of the properties file again, Multiline's attribute value can read it completely

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

New Post(0)