星期六, 八月 21, 2010

Understanding of Java standard IO streams

Java byte stream and character stream including the stream, byte stream through the IO device to read bytes of data the way, while the character stream is read into the data through the byte stream into a character "flow" in the form of user-driven.

Flow through the expansion of packaging functions, such as BufferedStream, can buffer streams, improve reading performance, in fact, are based in Java, this operation IO's.

But we can not BufferedStream is actually available as a stream, in fact, they must be packing a representative of the real IO stream users to operate equipment, such as System.in and System.out, or arrays and strings (another package's stream ).

InputStream can be used for example:
InputStream in = new BufferedInputStream (new BufferedInputStream (System.in));

The character stream it?
In fact, no real character in the form of flow only through the packaging of some special characters in the input stream to carry out operations, such as packing a byte stream InputStream:
Reader reader = new InputStreamReader (System.in);
Suppose, for example packaging the character array:
char [] in = new char [1024];
Reader reader = new CharArrayReader (in);

Understand, the law can understand the flow of operation.

Oracle Java processing in the user-defined types

Java for PL / SQL in the package does not define the type of support (not found, throw exception invalid name pattern information), need to define schema-level type.

Oracle's API:

Code:
Schema Level Type:
create or replace type Guy as object
(
name varchar2 (50),
age number
)
create or replace type GUYS as Table of GUY
PL / SQL:
create or replace package ArrayNesta is
PROCEDURE insert_guys (
guys_in IN guys
);
end ArrayNesta;
create or replace package body ArrayNesta is
PROCEDURE insert_guys (
guys_in IN guys
)
IS
BEGIN
for i in guys_in.first .. guys_in.last
LOOP
insert into ASSOCIATEARRAYNESTA (name, age) values (guys_in (i). name, guys_in (i). age);
END LOOP;

END insert_guys;

end ArrayNesta;
Database Table:
create table ASSOCIATEARRAYNESTA (
name varchar2 (50),
age number
)
Java:
/ **
*
* /
package array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import oracle.jdbc.pool.OracleDataSource;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
/ **
* @ Author nfeng
*
* /
public class ArrayTest (
/ **
* @ Param args
* /
public static void main (String [] args) throws Exception (
OracleDataSource ods = new OracleDataSource ();
ods.setURL ("jdbc: oracle: thin: username / password @ host: port: database");
Connection conn = ods.getConnection ();
System.out.println (conn);
StructDescriptor sd = StructDescriptor.createDescriptor ("GUY", conn);
Object [] nesta = new Object [] ("nesta", 29);
STRUCT snesta = new STRUCT (sd, conn, nesta);

Object [] kisey = new Object [] ("kisey", 30);
STRUCT skisey = new STRUCT (sd, conn, kisey);

ArrayDescriptor ad = ArrayDescriptor.createDescriptor ("GUYS", conn);
ARRAY guysin = new ARRAY (ad, conn, new STRUCT [] (snesta, skisey));

CallableStatement cs = conn.prepareCall ("(call ArrayNesta.insert_guys (?))");
cs.setArray (1, guysin);
cs.execute ();

conn.close ();
)
)

During the running of Java

[Other Languages]

1. Source code into binary code after compile

2. Implementation of the binary code: OS load the file into memory and execution

[Java]

1. Source code made by the javac compile into a byte code

2. Implementation of the java ClassName:

a. OS first java.exe load into memory and perform, create JVM

b. The java.exe into the parameters dealing with the implementation of the times, so JVM interpretation processing

ClassName content and present them.

JAVA properties file to read one of

JAVA third method to read the configuration file is:
The first reading of XML
The second reading of INI
Now consider reading this document
package temp;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Test (
public static void main (String [] args) (
String config = "dbconfig.properties"; / / specify the configuration file to read
Properties prop = new Properties ();
InputStream is;
try (
is = new FileInputStream (config);
prop.load (is);
) Catch (FileNotFoundException e) (
System.out.println ("can not find the specified configuration file.");
e.printStackTrace ();
) Catch (IOException e) (
e.printStackTrace ();
)
/ / Here is the specific information read the configuration file
String driver = prop.getProperty ("driver");
System.out.print (driver);
)
)
Properties of our file on this: F: \ Symposium \ java and the configuration file summary \ ConfigSecond directory.

星期六, 八月 14, 2010

python quick sort Code:

def quick_sort(ls):
return [] if ls == [] else quick_sort([y for y in ls[1:] if y < ls[0]]) + [ls[0]] + quick_sort([y for y in ls[1:] if y >= ls[0]])

if __name__ == '__main__':
l1 = [3,56,8,1,34,56,89,234,56,231,45,90,33,66,88,11,22]
l2 = quick_sort(l1)
print l1
print l2

python3.0 dictionary key order

In fact, the dictionary object that is key - the value of the following is a dictionary object to add, modify, delete (modify and add the same way, when the key does not exist when the value added)

IDLE 3.0
>>> Dic = ("aa": 1, "bb": 2, "ab": 3)
>>> Dic
('Aa': 1, 'ab': 3, 'bb': 2)
>>> For k in sorted (dic.keys ()):
print (k)

aa
ab


-----------------------------------------------
In fact, the dictionary object that is key - value pairs
The following is a dictionary object to add, modify, delete
(Modify and add the same way, when the key does not exist when the value added)
>>> Dic ["cc"] = 4
>>> Dic
('Aa': 1, 'cc': 4, 'ab': 3, 'bb': 2)
>>> Dic ["bc"] = 5
>>> Dic
('Aa': 1, 'cc': 4, 'ab': 3, 'bb': 2, 'bc': 5)
>>> Dic ["cc"] = 6
>>> Dic
('Aa': 1, 'cc': 6, 'ab': 3, 'bb': 2, 'bc': 5)
>>> Del dic ["cc"]
>>> Dic
('Aa': 1, 'ab': 3, 'bb': 2, 'bc': 5)

Python Developer Activex component method

Python strong feature is that it omnipotent.

Use the win32com module development window ActiveX example: (If you have not installed win32com module, then go to http://python.net/crew/skippy/win32/Downloads.html download).

# SimpleCOMServer.py

class PythonUtilities:
_public_methods_ = ['SplitString']
_reg_progid_ = "Python.Utilities"
_reg_clsid_ = "(A6688635-62F5-41cb-AF54-CBA84C2F0F86)"

def SplitString (self, val):
return "Hello world", val

if __name__ == '__main__':
print "Registering COM server ..."
import win32com.server.register
win32com.server.register.UseCommandLine (PythonUtilities)

In the console run: python SimpleCOMServer.py

In the HTML page to call the Activex component:

window.onload = function () (
var obj = new ActiveXObject ("Python.Utilities");

alert (obj.SplitString ("Hel"));
)