Java 常用方法
java 圖片上傳from表單必加屬性:enctype="multipart/form-data"
1、獲取字符串的長(zhǎng)度 :?length()
2?、判斷字符串的前綴或后綴與已知字符串是否相同
前綴 :startsWith(String?s) ?
后綴 :endsWith(String?s) ?
3、比較兩個(gè)字符串 :equals(String?s) ?
4、把字符串轉(zhuǎn)化為相應(yīng)的數(shù)值
int?型 :Integer.parseInt(字符串) ?
long?型 :Long.parseLong(字符串) ?
float?型 : Folat.valueOf(字符串).floatValue() ?
double?型 :Double.valueOf(字符串).doubleValue() ?
4、將數(shù)值轉(zhuǎn)化為字符串 :valueOf(數(shù)值) ?
5、字符串檢索 :indexOf(Srting?s)?
從頭開始檢索 :indexOf(String?s?,int?startpoint)?從?startpoint?處開始檢索,如果沒有檢索到,將返回?-1 ?(startpoint---- 下標(biāo)位置)
6、得到字符串的子字符串 :
substring(int?startpoint)?從?startpoint?處開始獲取?
substring(int?start,int?end)?從?start?到end?中間的字符
7、替換字符串中的字符,去掉字符串前后空格?
replace(char?old,char?new)?用?new?替換?old ?
trim()??
8、分析字符串?StringTokenizer(String?s)?
構(gòu)造一個(gè)分析器,使用默認(rèn)分隔字符(空格,換行,回車,Tab,進(jìn)紙符)
StringTokenizer(String?s,String?delim)?delim?是自己定義的分隔符
nextToken()?逐個(gè)獲取字符串中的語(yǔ)言符號(hào)
boolean?hasMoreTokens()?只要字符串還有語(yǔ)言符號(hào)將返回?true,否則返回?false ?
countTokens()?得到一共有多少個(gè)語(yǔ)言符號(hào)
《輸入輸出流》
1、FileInputStream類
FileInputStream(String name) 使用給定的文件名name創(chuàng)建一個(gè)FileInputStream對(duì)象
FileInputStream(File file) 使用File對(duì)象創(chuàng)建FileInpuStream對(duì)象
File類有兩個(gè)常用方法:
File(String s) s確定文件名字
File(String directory,String s) directory是文件目錄
例如:
File f=new File("Myfile.dat");
FileInputStream istream=new FileInputStream(f);
處理I/O異常
當(dāng)出現(xiàn)I/O錯(cuò)誤的時(shí)候,Java生成一個(gè)IOException(I/O異常)對(duì)象來表示這個(gè)錯(cuò)誤的信號(hào)。
程序必須使用一個(gè)catch檢測(cè)這個(gè)異常
例如:
try{
FileInputStream ins= new FileInputStream("Myfile.dat");
}
catch(IOException e){
System.out.println("File read Error:"+e);
}
從輸入流中讀取字節(jié)
int read() 返回0~255之間一個(gè)整數(shù),如果到輸入流末尾,則返回-1
int read(byte b[]) 讀取字節(jié)數(shù)組
int read(byte b[],int off,int len) off指定把數(shù)據(jù)存放在b中什么地方,len指定讀取的最大字節(jié)數(shù)
關(guān)閉流
close()
2、FileOutputStream類
FileOutputStream(String name) 使用指定的文件名name創(chuàng)建FileOutputStream對(duì)象
FileOutputStream(File file) 使用file對(duì)象創(chuàng)建FileOutputStream對(duì)象
FileOutputStream(FileDescriptor fdobj) 使用FileDescriptor對(duì)象創(chuàng)建FileOutputStream對(duì)象
3、FileReader類和FileWriter類
FileReader(String filename)
FileWriter(String filename)
處理時(shí)需要FileNotFoundException異常
4、RandomAccessFile類
RandomAccessFile不同于FileInputStream和FileOutputStream,不是他們的子類
當(dāng)我們想對(duì)一個(gè)文件進(jìn)行讀寫操作的時(shí)候,創(chuàng)建一個(gè)指向該文件的RandomAccessFile流就可以了
RandomAccessFile類有兩個(gè)構(gòu)造方法:
RandomAccessFile(String name, String mode) name是文件名,mode取r(只讀)或rw(讀寫)
RandomAccessFile(File file,String mode) file給出創(chuàng)建流的源
seek(long a) 移動(dòng)RandomAccessFile流指向文件的指針,a確定指針距文件開頭的位置
getFilePointer() 獲取當(dāng)前文件的指針位置
close() 關(guān)閉文件
getFD() 獲取文件的FileDescriptor
length() 獲取文件長(zhǎng)度
read() 讀取一個(gè)字節(jié)數(shù)據(jù)
readBoolean() 讀取一個(gè)布爾值
readByte() 讀取一個(gè)字節(jié)
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 讀取一個(gè)UTF字符串
setLength(long newLength) 設(shè)置文件長(zhǎng)度
skipByte(int n) 在文件中跳過給定數(shù)量的字節(jié)
write(byte b[]) 寫b.length個(gè)字節(jié)到文件
writeBoolean(bolean b)
writeByte(int v)
writeChar(char c)
writeChars(String s)
writeDouble(double d)
writeFloat(float v)
writeInt(int i)
writeLong(long l)
writeShort(int i)
writeUTF(String s)
5、管道流
PipedInputStream類
PipedInputStream() 創(chuàng)建一個(gè)管道輸入流
PipedInputStream(PipedOutputStream a) 連接到輸出流a的輸入流
read() 從輸入流中讀取一個(gè)字節(jié)
read(byte b[],int off,int len) off是在b中的開始位置,len是字節(jié)長(zhǎng)度
PipedOutputStream類
PipedOutputStream() 創(chuàng)建一個(gè)輸出流
PipedOutputStream(PipedInputStream a) 連接到輸入流a的輸出流
write(int b)
write(byte b[],int off,int len)
counnect() 連接輸入輸出流
close() 關(guān)閉流
在使用的時(shí)候要捕獲IOException異常。
6、數(shù)據(jù)流
DataInputStream類(數(shù)據(jù)輸入流)
DataInputStream(InputStream in) 將數(shù)據(jù)輸入流指向一個(gè)由in指定的輸入流
DataOutputStream類(數(shù)據(jù)輸出流)
DataOutputStream(OutputStream out) 將數(shù)據(jù)輸出流指向一個(gè)由out指定的輸出流
主要方法:
close()
read() 讀取一個(gè)字節(jié)數(shù)據(jù)
readBoolean() 讀取一個(gè)布爾值
readByte() 讀取一個(gè)字節(jié)
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 讀取一個(gè)UTF字符串
skipByte(int n) 在文件中跳過給定數(shù)量的字節(jié)
write(byte b[]) 寫b.length個(gè)字節(jié)到文件
writeBoolean(bolean b)
writeByte(int v)
writeChar(char c)
writeChars(String s)
writeDouble(double d)
writeFloat(float v)
writeInt(int i)
writeLong(long l)
writeShort(int i)
writeUTF(String s)
7、對(duì)象流
ObjectInputStream類和ObjectOutputStream類分別是DataInputStream類和DataOutputStream類的子類
8、回壓輸入流
PushbackInputStream類
PushbackInputStream(InputStream in)
PushbackReader類
PushbackReader(Reader in)
unread(char c) 回壓一個(gè)字符
unread(char c[]) 回壓數(shù)組c中全部字符
unread(char c[],offset,int n) 回壓c中從offset開始的n個(gè)字符
轉(zhuǎn)載于:https://www.cnblogs.com/xjbBill/p/6015666.html
總結(jié)
- 上一篇: 一. DotNet MVC4.0+Eas
- 下一篇: Javascript数组操作(转)