A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lanzy1989 中级黑马   /  2014-9-17 20:40  /  1968 人查看  /  11 人回复  /   2 人收藏 转载请遵从CC协议 禁止商业使用本文

以下是本人搜集的一些方法。送给大家:
字符串
1、获取字符串的长度
length()
2 、判断字符串的前缀或后缀与已知字符串是否相同
前缀 startsWith(String s)
后缀 endsWith(String s)
3、比较两个字符串
equals(String s)
4、把字符串转化为相应的数值
int型 Integer.parseInt(字符串)
long型 Long.parseLong(字符串)
float型 Folat.valueOf(字符串).floatValue()
double型 Double.valueOf(字符串).doubleValue()
4、将数值转化为字符串
valueOf(数值)
5、字符串检索
indexOf(Srting s) 从头开始检索
indexOf(String s ,int startpoint) 从startpoint处开始检索
如果没有检索到,将返回-1
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) 构造一个分析器,使用默认分隔字符(空格,换行,回车,Tab,进纸符)
StringTokenizer(String s,String delim) delim是自己定义的分隔符
nextToken() 逐个获取字符串中的语言符号
boolean hasMoreTokens() 只要字符串还有语言符号将返回true,否则返回false
countTokens() 得到一共有多少个语言符号


输入输出流
1、FileInputStream类
FileInputStream(String name) 使用给定的文件名name创建一个FileInputStream对象
FileInputStream(File file) 使用File对象创建FileInpuStream对象
File类有两个常用方法:
File(String s) s确定文件名字
File(String directory,String s) directory是文件目录
例如:
File f=new File("Myfile.dat");
FileInputStream istream=new FileInputStream(f);
处理I/O异常
当出现I/O错误的时候,Java生成一个IOException(I/O异常)对象来表示这个错误的信号。
程序必须使用一个catch检测这个异常
例如:
try{
FileInputStream ins= new FileInputStream("Myfile.dat");
}
catch(IOException e){
System.out.println("File read Error:"+e);
}
从输入流中读取字节
int read() 返回0~255之间一个整数,如果到输入流末尾,则返回-1
int read(byte b[]) 读取字节数组
int read(byte b[],int off,int len) off指定把数据存放在b中什么地方,len指定读取的最大字节数
关闭流
close()
2、FileOutputStream类
FileOutputStream(String name) 使用指定的文件名name创建FileOutputStream对象
FileOutputStream(File file) 使用file对象创建FileOutputStream对象
FileOutputStream(FileDescriptor fdobj) 使用FileDescriptor对象创建FileOutputStream对象
3、FileReader类和FileWriter类
FileReader(String filename)
FileWriter(String filename)
处理时需要FileNotFoundException异常
4、RandomAccessFile类
RandomAccessFile不同于FileInputStream和FileOutputStream,不是他们的子类
当我们想对一个文件进行读写操作的时候,创建一个指向该文件的RandomAccessFile流就可以了
RandomAccessFile类有两个构造方法:
RandomAccessFile(String name, String mode) name是文件名,mode取r(只读)或rw(读写)
RandomAccessFile(File file,String mode) file给出创建流的源
seek(long a) 移动RandomAccessFile流指向文件的指针,a确定指针距文件开头的位置
getFilePointer() 获取当前文件的指针位置
close() 关闭文件
getFD() 获取文件的FileDescriptor
length() 获取文件长度
read() 读取一个字节数据
readBoolean() 读取一个布尔值
readByte() 读取一个字节
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 读取一个UTF字符串
setLength(long newLength) 设置文件长度
skipByte(int n) 在文件中跳过给定数量的字节
write(byte b[]) 写b.length个字节到文件
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)


DataInputStream类(数据输入流)
DataInputStream(InputStream in) 将数据输入流指向一个由in指定的输入流
DataOutputStream类(数据输出流)
DataOutputStream(OutputStream out) 将数据输出流指向一个由out指定的输出流
主要方法:
close()
read() 读取一个字节数据
readBoolean() 读取一个布尔值
readByte() 读取一个字节
readChar()
readFloat()
readFully(byte b[])
readInt()
readLine()
readLong()
readUnsignedShort()
readUTF() 读取一个UTF字符串
skipByte(int n) 在文件中跳过给定数量的字节
write(byte b[]) 写b.length个字节到文件
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、对象流
ObjectInputStream类和ObjectOutputStream类分别是DataInputStream类和DataOutputStream类的子类
8、回压输入流
PushbackInputStream类
PushbackInputStream(InputStream in)
PushbackReader类
PushbackReader(Reader in)
unread(char c) 回压一个字符
unread(char c[]) 回压数组c中全部字符
unread(char c[],offset,int n) 回压c中从offset开始的n个字符



目前就学到这些,下次再添加吧。。。

11 个回复

倒序浏览
这个必须赞,楼主继续加油。说不定能成为神贴
回复 使用道具 举报
感谢分享,我也有总结,不过都比较零散
回复 使用道具 举报
谢谢楼主,很有用处哦
回复 使用道具 举报
好东西啊 ,收藏啦
回复 使用道具 举报
正在学习方法中,
回复 使用道具 举报
Seven` 发表于 2014-9-18 02:06
感谢分享,我也有总结,不过都比较零散

哈哈,你要可以直接找我要啊,15000一份:lol
回复 使用道具 举报
zz_job 中级黑马 2014-9-19 22:40:25
8#
这个。。不是API里的么。。。看API就好了。。
回复 使用道具 举报
加油。。。。。。。。。。。
回复 使用道具 举报
我们每个人都应该学你, 自己归纳一套才学得扎实
回复 使用道具 举报
学习总结是好的。
回复 使用道具 举报
好分享,就必须赞起来啊。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马