Recent Notes
Displaying keyword search results 31 - 40
Created by alfa on April 06, 2011 13:23:40
Last update: April 06, 2011 13:23:40
A file can be read as URL with the java.net package. You must provide absolute path after the URI scheme name file:// .
package com.demo.io;
import java.io.*;
i...
Created by alfa on April 06, 2011 12:48:44
Last update: April 06, 2011 12:48:44
package com.demo.io;
import java.io.*;
...
Created by alfa on April 06, 2011 12:25:29
Last update: April 06, 2011 12:27:00
Specify "UTF-8" as charsetName to the constructor of InputStreamReader . From JavaDoc:
An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and decodes them into characters using a specified charset. The charset that it uses may be specified by name or may be given explicitly, or the platform's default charset may be accepted.
BufferedReader r = new BufferedReader(
new I...
Created by alfa on April 06, 2011 12:07:12
Last update: April 06, 2011 12:08:01
You can use Class.getresourceAsStream or ClassLoader.getresourceAsStream to read a file from classpath. Class.getresourceAsStream delegates to ClassLoader.getresourceAsStream , but it does some preprocessing before delegation:
If the file name begins with a '/', then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is: package_name_with_dot_replaced_by_slash/file_name
package com.demo.io;
import java.io.*;
...
Created by Dr. Xi on April 05, 2011 08:04:37
Last update: April 05, 2011 08:11:37
There's no difference between a Java HTTP client and a Java HTTPS client. Ignore JavaWorld Java Tip 96 , it's way too old. The following code gets an HTTP page as well as an HTTPS page.
import java.io.*;
import java.net.*;
pub...
There's one catch . If you are using the code on a test server with a self-signed certificate, it fails. In that case, I would suggest that you download the certificate from the server and import it to your keystore as a trusted key. You may also need to add a subject alternative name to the certificate if the host name does not match the certificate.
You may also choose to use a custom TrustManager and HostnameVerifier to ignore the certificate verification errors.
Created by Dr. Xi on March 29, 2011 16:06:57
Last update: April 01, 2011 12:33:52
This utility class retrieves SSL certificates from the server and print them out to the stdout. The output can be saved to a file and imported to a Java keystore. This is useful in your test environment where the SSL certificate is self-signed.
import java.io.InputStream;
import java.io.Outp...
Retrieve and import the a certificate:
E:\test>java RetrieveSSLCert 192.168.69.144 8081 >...
Created by Dr. Xi on March 28, 2011 20:51:30
Last update: March 28, 2011 20:53:46
HTTP basic authentication is just Base64 encoded user name and password passed in as the Authorization header. So the following code works:
// encode user name and password
String credent...
However, since JDK 1.2, there's a more Java friendly way:
Authenticator.setDefault(new Authenticator() {
...
Test code:
import java.net.*;
import java.io.*;
pub...
Created by Dr. Xi on October 16, 2008 20:45:40
Last update: March 28, 2011 20:23:22
Java's built-in classes are way too complex/flexible for a simple protocol like HTTP. This is a wrapper to simplify HTTP GET and POST.
import java.io.*;
import java.net.*;
imp...
A simple test:
import java.io.*;
import java.util.*;
...
Created by Dr. Xi on March 02, 2011 11:39:18
Last update: March 09, 2011 12:19:30
Some peculiarities about Java PrintWriter: PrintWriter never throws any exceptions. From JavaDoc : Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError(). When error occurs, you'll never know anything more than that it occured, because checkError returns boolean. When a character is out of the range of the character encoding of the PrintWriter, it prints a question mark (?). But this is not an error. Test code:
import java.io.*; public class TestPrintWri... Latin1 test result: java TestPrintWriter iso-8859-1 | od -bc 000000... UTF-8 test result: java TestPrintWriter utf-8 | od -bc 0000000 141... Also, the constructor throws a FileNotFoundException when you try to write to a...
Created by Dr. Xi on March 01, 2011 16:11:50
Last update: March 01, 2011 16:11:50
Java PrintWriter is buffered. You can turn on autoFlush, but it does'nt work for all methods.
Test code:
import java.io.*;
public class TestPrintWri...
Output:
[START] Test PrintWriter
Auto-flush only work...