java基础类

完整代码文档下载:http://download.csdn.net/detail/hui12343211/6553553

public class 字符串{

public static void main(String args[]){
String strEng = "I Like Java";
System.out.println("1.charAt方法:"+strEng.charAt(8));
System.out.println("2.charAt方法:"+(int)strEng.charAt(8));
System.out.println("3.codePointAt方法:"+strEng.codePointAt(8));

String strChi = "我喜欢Java";
System.out.println("1(中文).chartAt方法:"+strChi.charAt(0));
System.out.println("2(中文).chartAt方法:"+(int)strChi.charAt(0));
System.out.println("3(中文).codePointAt方法:"+strChi.codePointAt(0));

System.out.println("4.codePointBefore方法:"+strEng.codePointBefore(9));
System.out.println("5.codePointCount方法:"+strEng.codePointCount(1,2));
System.out.println("6.compareTo方法:"+strEng.compareTo("I Like java"));
System.out.println("7.compareTo方法:"+strEng.compareTo("I Like Java"));
System.out.println("8.compareToIgnoreCase方法:"+strEng.compareToIgnoreCase("i like java"));
System.out.println("9.concat方法:"+strEng.concat(" ha ha!"));
System.out.println("10.contains方法:"+strEng.contains(" Java"));

char[] array = {'明','日','科','技'};
System.out.println("11.copyValueOf方法:"+String.copyValueOf(array));
System.out.println("12.copyValueOf方法:"+String.copyValueOf(array, 0, 2));

String fileName = "mydatabase.txt";
System.out.println("13.endsWith方法:"+fileName.endsWith(".txt"));
System.out.println("14.equals方法:"+fileName.equals("mydatabase.txt"));
System.out.println("15.equalsIgnoreCase方法:"+fileName.equalsIgnoreCase("MYDATABASE.TXT"));

System.out.println("16.format方法:"+String.format("%e", 15.000));
System.out.println("17.format方法:"+String.format("%x", 15));

System.out.println("今年是:"+String.format(java.util.Locale.CHINA, "%tY", new java.util.Date())+"年");
System.out.println("现在是:"+String.format(java.util.Locale.CHINA, "%tB", new java.util.Date()));
System.out.println("今天是:"+String.format(java.util.Locale.CHINA, "%td", new java.util.Date())+"日");

System.out.println("18.getBytes方法:");
String strCom = "i like java";
byte[] str1 = strCom.getBytes();
for(int i = 0 ; i < str1.length ; i++){
System.out.print(str1[i]+";");
}
System.out.println();

byte[] str2 = strCom.getBytes(Charset.defaultCharset());
for(int i = 0 ; i < str2.length ; i++){
System.out.print(str2[i]+";");
}
System.out.println();

byte[] str3;
try {
str3 = strCom.getBytes("GBK");
for(int i = 0 ; i < str3.length ; i++){
System.out.print(str3[i]+";");
}
System.out.println();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

System.out.println("19.getChars方法:");
char[] dst = new char[12];
strCom.getChars(0, 11, dst, 1);
for(int i = 0 ; i< dst.length ; i++){
System.out.print(dst[i]+";");
}
System.out.println();
System.out.println("20.hasCode方法:"+strCom.hashCode());

System.out.println("21.indexOf方法:"+strCom.indexOf(97));
System.out.println("22.indexOf方法:"+strCom.indexOf(97, 9)); 
System.out.println("23.indexOf方法:"+strCom.indexOf("j"));
System.out.println("24.indexOf方法:"+strCom.indexOf("j", 9));

String myName = new String("xiao ming");
String yourName = new String("xiao ming");
System.out.println("25.intern方法:"+ (myName.intern() == yourName.intern()));

System.out.println("26.isEmpty方法:"+strCom.isEmpty());
System.out.println("27.indexOf方法:"+strCom.lastIndexOf(97));
System.out.println("28.indexOf方法:"+strCom.lastIndexOf(97, 9)); 
System.out.println("29.indexOf方法:"+strCom.lastIndexOf("j"));
System.out.println("30.indexOf方法:"+strCom.lastIndexOf("j", 9));
System.out.println("31.length方法:"+strCom.length());

String regex = "1234";
System.out.println("32.matches方法:"+regex.matches("\\d{4}"));

System.out.println("33.offsetByCodePoints方法:"+strCom.offsetByCodePoints(2, 8));
System.out.println("34.regionMatches方法:"+strCom.regionMatches(true, 2, "  LIKE", 2, 4));
System.out.println("35.regionMatches方法:"+strCom.regionMatches(2, "  like", 2, 4));
System.out.println("36.replace方法:"+strCom.replace("like", "love"));
System.out.println("37.replaceAll方法:"+strCom.replaceAll("a", "A"));
System.out.println("38.replaceFirst方法:"+strCom.replaceFirst("a", "A"));
System.out.println("39.split方法:"+strCom.split(" ", 3)[0]+";"+strCom.split(" ", 3)[1]+";"+strCom.split(" ", 3)[2]);
System.out.println("40.startsWith方法:"+strCom.startsWith("i like j", 0));
System.out.println("41.subSequence方法:"+strCom.subSequence(0, 6));
System.out.println("42.substring方法:"+strCom.substring(0, strCom.length()));
System.out.println("43.toCharArray方法:"+strCom.toCharArray()[2]);
System.out.println("44.toLowerCase方法:"+strCom.toLowerCase()+";"+strCom.toLowerCase(Locale.UK));
System.out.println("45.toUpperCase方法:"+strCom.toUpperCase()+";"+strCom.toUpperCase(Locale.UK));
System.out.println("46.trim方法:"+strCom.trim());
System.out.println("47.valueOf方法:"+String.valueOf(true)+";"+String.valueOf('哈')+";"+String.valueOf(2.12f)+";"+String.valueOf(new Integer(123)));
}

}


public class 包装类 {
public static void main(String[] args) {
System.out.println("=====================");
System.out.println("Integer类--整数类");
System.out.println("构造方法Integer(int value)和Integer(String value):"+new Integer(1)+";"+new Integer("1"));
System.out.println("bitCount方法:"+"(0和9二进制补码):"+Integer.bitCount(0)+";"+Integer.bitCount(9));
System.out.println("Integer最大值:"+Integer.MAX_VALUE);
System.out.println("Integer最小值:"+Integer.MIN_VALUE);
System.out.println("比特位:"+Integer.SIZE+";类型:"+Integer.TYPE);
System.out.println("byteValue方法:"+new Integer(1).byteValue());
System.out.println("compareTo方法:"+new Integer(123).compareTo(new Integer(123)));
System.out.println("decode方法:"+Integer.decode("123"));
System.out.println("doubleValue方法:"+new Integer(12).doubleValue());
System.out.println("equals方法:"+new Integer(1).equals(new Integer(1)));
System.out.println("floatValue方法:"+new Integer(12).floatValue());
System.out.println("getInteger方法:"+Integer.getInteger("sun.arch.data.model"));
System.out.println("getInteger方法:"+Integer.getInteger("java.version", 10));
System.out.println("getInteger方法:"+Integer.getInteger("java.version", new Integer(10)));
System.out.println("hashCode方法:"+new Integer(123).hashCode());
System.out.println("highestOneBit方法:"+Integer.highestOneBit(9));
System.out.println("intValue方法:"+new Integer(12).intValue());
System.out.println("longValue方法:"+new Integer(12).longValue());
System.out.println("lowestOneBit方法:"+Integer.lowestOneBit(9));
System.out.println("parseInt方法:"+Integer.parseInt("123"));
System.out.println("parseInt方法:"+Integer.parseInt("FF", 16));
System.out.println("reverse方法:"+Integer.reverse(10));
System.out.println("reverseBytes方法:"+Integer.reverseBytes(10));
System.out.println("signum方法:"+Integer.signum(-123));
System.out.println("toBinaryString方法:"+Integer.toBinaryString(123));
System.out.println("toHexString方法:"+Integer.toHexString(123));
System.out.println("toOctalString方法:"+Integer.toOctalString(123));
System.out.println("toString方法:"+Integer.toString(123, 10));
System.out.println("valueOf方法:"+Integer.valueOf("123", 8));
System.out.println("valueOf方法:"+Integer.valueOf(234));
System.out.println("=====================");
System.out.println("Long类--长整型类");
System.out.println("方法基本近似Integer类");
System.out.println("=====================");
System.out.println("Short类--短整型类");
System.out.println("方法基本近似Integer类");
System.out.println("=====================");
System.out.println("Boolean类--布尔类");
System.out.println("方法基本近似Integer类");
System.out.println("=====================");
System.out.println("Byte类--字节对象");
System.out.println("方法基本近似Integer类");
System.out.println("=====================");
System.out.println("Character类--字符类");
System.out.println("charCount方法:"+Character.charCount(0x10000));
System.out.println("getNumericValue方法:"+Character.getNumericValue('\u216C'));
System.out.println("getType方法:"+Character.getType('!'));
System.out.println("isDefined方法:"+Character.isDefined('a'));
System.out.println("isDigit方法:"+Character.isDigit('哈'));
System.out.println("isLetter方法:"+Character.isLetter('哈'));
System.out.println("isLetter方法:"+Character.isLetter(26126));
System.out.println("isLowerCase方法:"+Character.isLowerCase('a'));
System.out.println("toLowerCase方法:"+Character.toLowerCase('A'));
System.out.println("=====================");
System.out.println("Double--双精度数字类");
System.out.println("doubleToLongBits方法:"+Double.doubleToLongBits(123.456d));
System.out.println("doubleToRawLongBits方法:"+Double.doubleToRawLongBits(Double.NaN));
System.out.println("isInfinite方法:"+Double.isInfinite(12.345d));
System.out.println("isInfinite方法:"+new Double(12.345d).isInfinite());
System.out.println("isNaN方法:"+Double.isNaN(1.23d));
System.out.println("longBitsToDouble方法:"+Double.longBitsToDouble(123));
System.out.println("====================="); 
System.out.println("Float--浮点类");
System.out.println("方法基本近似Double类");
System.out.println("====================="); 
}
}



public class 输入输出 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
System.out.println("======================");
System.out.println("BufferedInputStream类--缓冲字节输入流");
availableMethod();
markMethod();
markSupportedMethod();
skipMethod();
System.out.println("======================");
System.out.println("BufferedOutputStream类--缓冲字节输出流");
flushMethod();
System.out.println("======================");
System.out.println("BufferedReader类--缓冲字符输入流");
readMethod();
readLineMethod();
readyMethod();
System.out.println("======================");
System.out.println("BufferedWriter类--缓冲字符输出流");
newLineMethod();
System.out.println("======================");
System.out.println("DataInputStream类--数据输入流");
System.out.println("DataOutputStream类--数据输出流");
DataInputOrOutputStream_readFloatMethod();
DataInputOrOutputStream_readUTFMethod();
System.out.println("======================");
System.out.println("File类--文件类");
canExecuteMethod();
canReadMethod();
canWriteMethod();
compareToMethod();
createNewFileMethod();
createTempFileMethod();
deleteOnExitMethod();
getAbsoluteFileMethod();
getAbsolutePathMethod();
getCanonicalFileMethod();
getCanonicalPathMethod();
getFreeSpaceMethod();
getParentMethod();
getParentFileMethod();
getPathMethod();
getTotalSpaceMethod();
getUsableSpaceMethod();
hashCodeMethod();
isAbsoluteMethod();
isDirectoryMethod();
isFileMethod();
isHiddenMethod();
lastModifiedMethod();
lengthMethod();
listMethod();
listRootsMethod();
mkdirMethod();
mkdirsMethod();
toXXXMethod();
System.out.println("======================");
System.out.println("FileInputStream类--文件字节输入流");
getChannelMethod();
getFDMethod();
System.out.println("======================");
System.out.println("FileOutputStream类--文件字节输出流");
System.out.println("======================");
System.out.println("ObjectOutputStream类--对象输出流");
System.out.println("ObjectInputStream类--对象输入流");
writeObjectMethod();
System.out.println("======================");
System.out.println("RandomAccessFile类--随机文件访问类");
RandomAccessFile_readLineMethod();
System.out.println("======================");
System.out.println("Reader类--读取字符流的抽象类");
System.out.println("Writer类--写入字符流的抽象类");
System.out.println("======================");
System.out.println("Scanner类--简单文本扫描器");
delimiterMethod();
nextMethod();
useLocaleMethod();
hasNextFloatMethod();
System.out.println("======================");
}

static void availableMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
InputStream in = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
System.out.println("文件大小为:"+file.length()+"Bytes");
System.out.println("估计文件还有:"+bis.available()+"Bytes");
byte[] by = new byte[(int)file.length()];
while(bis.read(by)!=-1)
System.out.print(new String(by));
bis.close();
in.close();
System.out.println();
}

static void markMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
InputStream in = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(in,2);
System.out.println((char)bis.read());
bis.mark(1);
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.reset();
System.out.println((char)bis.read());
bis.reset();
System.out.println((char)bis.read());
System.out.println((char)bis.read());
System.out.println((char)bis.read());
System.out.println((char)bis.read());
bis.close();
in.close();
}

static void markSupportedMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
InputStream in = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
if(bis.markSupported())
System.out.println("BufferedInputStream类支持mark()与reset()方法");
bis.close();
in.close();
}

static void skipMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
InputStream in = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(in);
bis.skip(5);
System.out.println((char)bis.read());
bis.close();
in.close();
}

static void flushMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
OutputStream fos = new FileOutputStream(file);
BufferedOutputStream bus = new BufferedOutputStream(fos);
byte[] by = "A little girl was talking to her teacher about whales.".getBytes();
bus.write(by);
bus.flush();
bus.close();
}

static void readMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
FileReader fin = new FileReader(file);
BufferedReader bip = new BufferedReader(fin);
int read;
while((read = bip.read()) != -1){
System.out.print((char)read);
}
bip.close();
System.out.println();
}

static void readLineMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
FileReader fin = new FileReader(file);
BufferedReader bip = new BufferedReader(fin);
System.out.println(bip.readLine());
bip.close();
}

static void readyMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
FileReader fin = new FileReader(file);
BufferedReader bip = new BufferedReader(fin);
if(bip.ready())
System.out.println(bip.readLine());
bip.close();
}

static void newLineMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.xls");
if(!file.exists())
file.createNewFile();
FileWriter fw = new FileWriter(file);
BufferedWriter bufw = new BufferedWriter(fw);
bufw.write("A little girl was talking to her teacher about whales.");
bufw.newLine();
bufw.write("小明:你好");
bufw.newLine();
bufw.write("Jack:Hello");
bufw.flush();
bufw.close();
bufw.close();
}

static void DataInputOrOutputStream_readFloatMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出2.txt");
if(!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeFloat(123.456f);
dos.writeFloat(234.456f);
dos.flush();
dos.close();
fos.close();

FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readFloat());
System.out.println(dis.readFloat());
dis.close();
fis.close();
}

static void DataInputOrOutputStream_readUTFMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出2.txt");
if(!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF("你好");
dos.flush();
dos.close();
fos.close();

FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
System.out.println(dis.readUTF());
dis.close();
fis.close();
}

static void canExecuteMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出2.txt");
if(file.canExecute()){
System.out.println("应用程序可以执行此抽象路径名表示的文件");
}
}

static void canReadMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出2.txt");
if(file.canRead()){
System.out.println("应用程序可以读取此抽象路径名表示的文件");
}
}

static void canWriteMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出2.txt");
if(file.canWrite()){
System.out.println("应用程序可以向指定的文件中写入信息");
}
}

static void compareToMethod() throws IOException{
File fileOne = new File("E:\\a.java");
File fileTwo = new File("H:\\a.java");
System.out.println(fileOne.compareTo(fileTwo));
}

static void createNewFileMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出3.txt");
if(!file.exists())
file.createNewFile();
}

static void createTempFileMethod() throws IOException{
File file = File.createTempFile("abc", ".txt");
System.out.println(file.exists());

}

static void deleteOnExitMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出3.txt");
if(file.exists())
file.deleteOnExit();
System.out.println(file.exists());
}

static void getAbsoluteFileMethod() throws IOException{
File file = new File("test.java");
System.out.println(file.getAbsoluteFile());
}

static void getAbsolutePathMethod() throws IOException{
File file = new File("test.java");
System.out.println(file.getAbsolutePath());
}

static void getCanonicalFileMethod() throws IOException{
File file = new File("test.java");
System.out.println(file.getCanonicalFile());
}


static void getCanonicalPathMethod() throws IOException{
File file = new File("test.java");
System.out.println(file.getCanonicalPath());
}

static void getFreeSpaceMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.getFreeSpace());
}

static void getParentMethod() throws IOException{
File file = new File("E:\\a.java");
System.out.println(file.getParent());
}

static void getParentFileMethod() throws IOException{
File file = new File("E:\\a.java");
System.out.println(file.getParentFile());
}

static void getPathMethod() throws IOException{
File file = new File("E:\\a.java");
System.out.println(file.getPath());
}

static void getTotalSpaceMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.getTotalSpace());
}

static void getUsableSpaceMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.getUsableSpace());
}

static void hashCodeMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.hashCode());
}

static void isAbsoluteMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.isAbsolute());
}

static void isDirectoryMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\");
System.out.println(file.isDirectory());
}

static void isFileMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.isFile());
}

static void isHiddenMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.isHidden());
}

static void lastModifiedMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(new java.util.Date(file.lastModified()));
}

static void lengthMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println(file.length());
}

static void listMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\");
System.out.println("未过滤:");
for(int i = 0 ; i < file.list().length ; i++)
System.out.print(i+"["+file.list()[i]+"]");
System.out.println("\n已过滤:");
for(int i = 0 ; i < file.list(new MyFilter()).length ; i++)
System.out.print(i+"["+file.list(new MyFilter())[i]+"]");
System.out.println();
}

static class MyFilter implements FilenameFilter{
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}

}

static void listRootsMethod() throws IOException{
File[] fs = File.listRoots();
for(int i = 0 ; i < fs.length ; i++){
System.out.print(fs[i]+"\t");
}
System.out.println();
}

static void mkdirMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\mydir");
if(file.exists())
file.delete();
if(file.mkdir())
System.out.println("目录创建成功1");
}

static void mkdirsMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\mydir\\mydir2");
if(file.exists())
file.delete();
if(file.mkdir())
System.out.println("目录创建成功2");
}

@SuppressWarnings("deprecation")
static void toXXXMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
System.out.println("toURI:"+file.toURI());
System.out.println("toString:"+file.toString());
System.out.println("toPath:"+file.toPath());
System.out.println("toURL:"+file.toURL());
}

static void getChannelMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
FileInputStream fi = new FileInputStream(file);
FileChannel c = fi.getChannel();
c.close();
fi.close();
}

static void getFDMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
FileInputStream fi = new FileInputStream(file);
FileDescriptor fd = fi.getFD();
System.out.println(fd.valid());
fi.close();
}

static void writeObjectMethod() throws IOException, ClassNotFoundException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出3.txt");
FileOutputStream fop = new FileOutputStream(file);
ObjectOutputStream ops = new ObjectOutputStream(fop);
Student stu = new 输入输出.Student("李四" , "男");
ops.writeObject(stu);
ops.close();
fop.close();

FileInputStream fip = new FileInputStream(file);
ObjectInputStream ips = new ObjectInputStream(fip);
Student sturead = (Student)ips.readObject();
System.out.println(sturead.getName()+";"+sturead.getSex());
ips.close();
fip.close();
}

public static class Student implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String sex;
Student(String name , String sex){
this.name = name;
this.sex = sex;
}
public String getName(){
return name;
}
public String getSex(){
return sex;
}
}

static void RandomAccessFile_readLineMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter3\\输入输出.txt");
RandomAccessFile r = new RandomAccessFile(file , "rw");
System.out.println(r.readLine());
r.close();

}

static void delimiterMethod(){
String input = "1 and 2 and yellow and blue";
@SuppressWarnings("resource")
Scanner s = new Scanner(input).useDelimiter("\\sfish\\s");
System.out.println(s.delimiter());
s.close();
}

static void nextMethod(){
String input = "1 and 2 and yellow and blue";
Scanner sc = new Scanner(input);
while(sc.hasNext()){
System.out.print(sc.next());
}
sc.close();
System.out.println();
}

static void useLocaleMethod(){
String input = "1 and 2 and yellow and blue";
Scanner sc = new Scanner(input);
sc.useLocale(new Locale("en","US"));
System.out.println(sc.locale());
sc.close();
}

static void hasNextFloatMethod(){
String input = "1 and 2 and yellow and blue";
Scanner sc = new Scanner(input);
while(sc.hasNextFloat()){
System.out.print(sc.nextFloat());
}
System.out.println();
sc.close();
}
}



public class 集合类 {
public static void main(String[] args) {
System.out.println("======================");
System.out.println("List接口--有序集合类");
addMethod();
setMethod();
listIteratorMethod();
subListMethod();
toArrayMethod();
System.out.println("======================");
System.out.println("Map接口--映射集合类");
keySetMethod();
System.out.println("======================");
System.out.println("Set接口--无重复元素集合类");
addAllMethod();
System.out.println("======================");
}

private static void addMethod() {
List<String> list = new ArrayList<String>();
list.add(0, "哈哈");
list.add("哦哦");
list.add(1, "啊啊");
for(int i=0; i<list.size() ; i++){
System.out.print(list.get(i)+";");
}
System.out.println();
Iterator<String> iter = (Iterator<String>) list.iterator();
while(iter.hasNext()){
System.out.print(iter.next()+";");
}
System.out.println();
}

private static void setMethod() {
List<String> list = new ArrayList<String>();
list.add(0, "哈哈");
list.add("哦哦");
list.add(1, "啊啊");
list.set(0, "嘿嘿");
for(int i=0; i<list.size() ; i++){
System.out.print(list.get(i)+";");
}
System.out.println();
}

private static void listIteratorMethod(){
List<String> list = new ArrayList<String>();
list.add(0, "哈哈");
list.add("哦哦");
list.add(1, "啊啊");
ListIterator<String> iter = (ListIterator<String>) list.listIterator();
while(iter.hasNext()){
System.out.print(iter.next()+";");
}
System.out.println();
}

private static void subListMethod() {
List<String> list = new ArrayList<String>();
list.add(0, "哈哈");
list.add("哦哦");
list.add(1, "啊啊");
list.set(0, "嘿嘿");
List<String> sublist = list.subList(1, 3);
for(int i=0; i<sublist.size() ; i++){
System.out.print(sublist.get(i)+";");
}
System.out.println();
}

private static void toArrayMethod() {
List<String> list = new ArrayList<String>();
list.add(0, "哈哈");
list.add("哦哦");
list.add("哦哦");
list.add("哦哦");
list.add(1, "啊啊");
list.set(0, "嘿嘿");
String[] a = new String[5];
Object[] arrlist = list.toArray(a);
for(int i=0; i<arrlist.length ; i++){
System.out.print(arrlist[i]+";");
}
System.out.println();
}

private static void keySetMethod(){
Map<String , String> map = new HashMap<String , String>();
map.put("apple", "新鲜的苹果");
map.put("computer", "配置优良的计算机");
map.put("book", "堆积如山的图书");
Set<String> keySet = map.keySet();
for(Object keyName : keySet)
System.out.print(keyName+";");
System.out.println();
}

private static void addAllMethod(){
Set<String> set = new HashSet<String>();
List<String> list = new ArrayList<String>();
list.add(0, "哈哈");
list.add("哦哦");
list.add("哦哦");
list.add(1, "啊啊");
set.addAll(list);
Iterator<String> iter = (Iterator<String>) set.iterator();
while(iter.hasNext()){
System.out.print(iter.next()+";");
}
System.out.println();
}
}


/*关于Statement与PreparedStatement接口的比较
 *1.PreparedStatement是预编译的,对于批量处理可以大大提高效率. 也叫JDBC存储过程
 *2.使用 Statement 对象。在对数据库只执行一次性存取的时侯,用 Statement 对象进行处理。PreparedStatement 对象的开销比Statement大,对于一次性操作并不会带来额外的好处。
 *3.statement每次执行sql语句,相关数据库都要执行sql语句的编译,preparedstatement是预编译得,   preparedstatement支持批处理
 *4.PreparedStatement对象不仅包含了SQL语句,而且大多数情况下这个语句已经被预编译过,因而当其执行时,
 *只需DBMS运行SQL语句,而不必先编译。当你需要执行Statement对象多次的时候,PreparedStatement对象将会大大降低运行时间,当然也加快了访问数据库的速度。
 *这种转换也给你带来很大的便利,不必重复SQL语句的句法,而只需更改其中变量的值,便可重新执行SQL语句。
 *选择PreparedStatement对象与否,在于相同句法的SQL语句是否执行了多次,而且两次之间的差别仅仅是变量的不同。
 *如果仅仅执行了一次的话,它应该和普通的对象毫无差异,体现不出它预编译的优越性。
 *5.执行许多SQL语句的JDBC程序产生大量的Statement和PreparedStatement对象。通常认为PreparedStatement对象比Statement对象更有效,特别是如果带有不同参数的同一SQL语句被多次执行的时候。
 *PreparedStatement对象允许数据库预编译SQL语句,这样在随后的运行中可以节省时间并增加代码的可读性。 
 */


/* ResultSet类的getDate getTime 和getTimestamp方法的区别:
 * getDate()                 只取到天
 * getTime()                 只取时分秒
 * getTimestamp()    精确到毫秒 
 */


public class 数据库编程 {
public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
System.out.println("======================");
System.out.println("DriverManager类--驱动程序管理类");
deregisterDriverMethod();
getConnectionMethod();
getLoginTimeoutMethod();
setLogWriterMethod();
printlnMethod();
System.out.println("======================");
System.out.println("Connection接口--数据库连接接口");
commitMethod();
clearWarningsMethod();//清除警告信息
isReadOnlyMethod();//是否是只读模式
isValidMethod();//连接是否有效
rollbackMethod();
setSavepointMethod();//创建一个保存点
setTransactionIsolationMethod();
System.out.println("======================");
System.out.println("Statement接口--执行SQL语句接口");
addBatchMethod();//添加批处理语句
setPoolableMethod();
getResultSetMethod();
getResultSetConcurrencyMethod();
getResultSetHoldabilityMethod();
getResultSetTypeMethod();
getUpdateCountMethod();
System.out.println("======================");
System.out.println("PreparedStatement接口--预编译SQL语句接口");
clearParametersMethod();
System.out.println("======================");
System.out.println("ResultSet接口--结果集接口");
absoluteMethod();
afterLastMethod();
deleteRowMethod();
getTimestampMethod();
moveToInsertRowMethod();
System.out.println("======================");
}

private static void deregisterDriverMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Driver driver = DriverManager.getDriver(url);
DriverManager.deregisterDriver(driver);//一旦删除一个驱动程序,那将不能再使用这个驱动程序
DriverManager.registerDriver(driver);//使用这个方法重新注册
}

private static void getConnectionMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "");
Connection con = DriverManager.getConnection(url, info);
System.out.println(con);
}

private static void getLoginTimeoutMethod(){
System.out.println("获取连接数据库的等待时间:"+DriverManager.getLoginTimeout());
}

private static void setLogWriterMethod() throws IOException{
File file = new File("E:\\Eclipse\\wordspace3\\Java全能速查宝典\\src\\chapter5\\DriverManager.log");
if(!file.exists())
file.createNewFile();
FileWriter fileWriter = new FileWriter(file,true);//此处的true参数代表将内容【追加】写入文件
PrintWriter out = new PrintWriter(fileWriter, true);
DriverManager.setLogWriter(out);//设置日志打印输出流
}

private static void printlnMethod(){
DriverManager.println("测试DriverManager类输出日志方法");
}

private static void commitMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
con.setAutoCommit(false);
Statement st = con.createStatement();
st.execute("insert into jxl(name,grade) values('A','88')");
con.commit();//手动提交事务
}

private static void clearWarningsMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
con.clearWarnings();
}

private static void isReadOnlyMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
System.out.println(con.isReadOnly());
}

private static void isValidMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
System.out.println(con.isValid(0));
}

private static void rollbackMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
con.setAutoCommit(false);
Statement st = con.createStatement();
st.execute("insert into jxl(name,grade) values('A','10')");
con.rollback();
con.commit();
}

private static void setSavepointMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
con.setAutoCommit(false);
Statement st = con.createStatement();
st.execute("insert into jxl(name,grade) values('A','100')");
Savepoint savepoint = con.setSavepoint();
st.execute("insert into jxl(name,grade) values('A','200')");
con.rollback(savepoint);//回滚到savepoint保存点,当手动提交事务时只提交保存点之前的数据
con.commit();
}

private static void setTransactionIsolationMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
/* Connection.TRANSACTION_READ_UNCOMMITTED  指示不可以发生脏读;不可重复读和虚读可以发生
* Connection.TRANSACTION_SERIALIZABLE      指示不可以发生脏读,不可重复读和虚读
* Connection.TRANSACTION_REPEATABLE_READ   指示不可以发生脏读和不可重复读;虚读可以发生
* Connection.TRANSACTION_READ_COMMITTED    指示可以发生脏读,不可重复读和虚读
*/
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
System.out.println("事务隔离级别:"+con.getTransactionIsolation());
}

private static void addBatchMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement stmt = con.createStatement();
stmt.addBatch("insert into jxl(name,grade) values('O','0')");
stmt.addBatch("insert into jxl(name,grade) values('O','0')");
stmt.addBatch("insert into jxl(name,grade) values('O','0')");
stmt.executeBatch();
stmt.close();
}


private static void setPoolableMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement stmt = con.createStatement();
stmt.setPoolable(true);//请求将语句可池化(默认情况下Statement在创建时不是可池化的,而PreparedStatement和CallableStatement是可池化的)
stmt.close();
}

private static void getResultSetMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement stmt = con.createStatement();
stmt.executeQuery("select * from jxl");
ResultSet rs = stmt.getResultSet();
while(rs.next())
System.out.print(rs.getInt("id"));
System.out.println();
stmt.close();
}

private static void getResultSetConcurrencyMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement stmt = con.createStatement();
/*  ResultSet.CONCUR_READ_ONLY   不可以更新的ResultSet对象的并发模式
*  ResultSet.CONCUR_UPDATABLE   可以更新的ResultSet对象的并发模式
*/
System.out.println(stmt.getResultSetConcurrency());
stmt.close();
}

private static void getResultSetHoldabilityMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement stmt = con.createStatement();
/* ResultSet.HOLD_CURSORS_OVER_COMMIT 提交当前事务时,具有此可保存性的打开的ResultSet对象将保持开放
* ResultSet.CLOSE_CURSORS_AT_COMMIT  提交当前事务时,具有此可保存性的打开的ResultSet对象将被关闭
*/
System.out.println(stmt.getResultSetHoldability());
stmt.close();
}

private static void getResultSetTypeMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement stmt = con.createStatement();
/* ResultSet.TYPE_FORWARD_ONLY       该常量指示光标只能向前移动的ResultSet对象的类型
* ResultSet.TYPE_SCROLL_SENSITIVE   该常量指示可滚动并且通常受ResultSet底层数据更改影响的ResultSet对象的类型
* ResultSet.TYPE_SCROLL_INSENSITIVE 该常量指示可滚动但通常不受ResultSet底层数据更改影响的ResultSet对象的类型
*/
System.out.println(stmt.getResultSetType());
stmt.close();
}

private static void getUpdateCountMethod() throws SQLException, ClassNotFoundException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement stmt = con.createStatement();
stmt.executeUpdate("update jxl set name = 'AA' where name = 'A'");
System.out.println("获取更新的记录数:"+stmt.getUpdateCount());
stmt.close();
}

private static void clearParametersMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
PreparedStatement ps = con.prepareStatement("update jxl set name = ? where name = ?");
ps.setString(1, "OOO");
ps.setString(2, "O");
ps.clearParameters();//立即清除以上参数值
ps.setString(1, "O");
ps.setString(2, "OOO");
ps.executeUpdate();
}

private static void absoluteMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from jxl");
if(rs.absolute(-3)){
while(rs.next())
System.out.print(rs.getString("name"));
}
System.out.println();
}

private static void afterLastMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from jxl");
rs.afterLast();
while(rs.next())
System.out.print(rs.getString("name"));
System.out.println();
}

private static void deleteRowMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
PreparedStatement st = con.prepareStatement("select * from jxl",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery();
if(rs.absolute(5)){
while(rs.next()){
rs.deleteRow();//将数据库中的当前行数据删除
System.out.print(rs.getString("name"));
}
}
System.out.println();
}

private static void getTimestampMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
PreparedStatement st = con.prepareStatement("select * from test",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery();
if(rs.next()){
System.out.println(rs.getTimestamp(2));
System.out.println(rs.getTimestamp("time"));
}
System.out.println();
}

private static void moveToInsertRowMethod() throws ClassNotFoundException, SQLException{
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
Connection con = DriverManager.getConnection(url, "root", "");
PreparedStatement st = con.prepareStatement("select * from test",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = st.executeQuery();
rs.moveToInsertRow();//将光标移动到插入行
rs.updateTime("time", new Time(0));
rs.insertRow();//插入数据
rs.close();
st.close();
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值