Friday, January 18, 2008

Java Coding Question And Answers

Reading UTF-8 Encoded Data

try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("infilename"), "UTF8"));
String str = in.readLine();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
Writing UTF-8 Encoded Data

try {
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename"), "UTF8"));
out.write(aString);
out.close();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
Reading ISO Latin-1 Encoded Data

try {
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("infilename"), ,”8859_1"));
String str = in.readLine();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}
Writing ISO Latin-1 Encoded Data

try {
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("outfilename"), "8859_1"));
out.write(aString);
out.close();
} catch (UnsupportedEncodingException e) {
} catch (IOException e) {
}

Networking (java.net)

Creating a URL
try {
// With components.
URL url = new URL("http","hostname", 80, "index.html");
// With a single string.
url = new URL("http://hostname:80/index.html");
} catch (MalformedURLException e) { }

No comments:

 
Google