[JAVA]《Java 核心技术》(五)流、文件、正则

1. 读写字节
 
avaiable方法
 
流打开要记得用close()关闭
 
2. 字节流和字符流
 
字节流, XXIn/OutStream
字符流,抽象类 Reader/Writer的子类
 
3.
FileInputStream
FileOutputStream
System.getPropert("user.dir")
 
组合流。
目的:利用有些流的缓冲、数字特性等。
 
 
常用:
FileInput/OutputStream
BufferedInput/OutputStream
 
 
4 文件输入输出
 
字符流有读写 编码设定
字节流没有
 
5 UTF8编码
0~7F.     0a6...a0
80~7FF.  110a10..a06 10 a5 ..a0
800...FFFF, 1110a15..a12                    10a11..a6                       10a5..a0
10000...10FFFF, 11110                    10                10                     10
 
6. ZIP
ZipInputStream
getNextEntry
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname))
ZipEntry entry;
while((entry = zis.getNextEntry()) != null){
     zin.closeEntry();
}
 
zin.close();


ZipOutputStream
 
FileOutputStream fout = new FileOutputStream("test.zip");
ZipOutputStream zout = new ZipOutputStream(fout);
 
for allfiles{
     ZipEntry ze = new ZipEntry(filename);
     zout.putNextEntry(ze);
     
     zout.closeEntry();
 
}
 
zout.close();
 
jar文件可以用
JarInputStream()和JarOutputStream()来读写清单项
 
5.对象序列化
序列号的作用
 
6.
流关心的是文件的内容;File关心的是文件在磁盘上的文件的存储。
File.separator
 
7.
内存映射文件
FileInputStream fin = new FileInputStream();
FileChannel channel = in.getChannel();
MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY,0,length);
...
FileLock lock = channel.lock()/ tryLock();
 
8.
正则表达式
 
\d  一个数字 [0-9]
\D 一个非数字 [^0-9]
\s 一个空白字符
\S 一个非空白字符
\w 一个字符 [a-zA-Z0-9]
\W
^ $开头和结尾
X? 可选的X 0次或1次
X* 0或多次
X+1或多次
X{n},X{n,},X{n,m}: n次,至少n次,n~m次
()群组
 
?将贪婪匹配转为勉强匹配,最小匹配
+贪婪匹配为占有匹配,最大匹配
eg:
[a-z]*ab      ------c ab ,其中c是最小匹配,[a-z]*
[a-z]*+ab    ------cab ,其中cab是最大匹配,[a-z]*+
 
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(input);
if(matcher.matches())...
 
 
find()
 
replaceAll
split()
 
 
正则 \\|
原因 | 在正则中有特殊含义,于是需要转义, \|,然而\也需要转义,于是就成了\\|
 
附加
关于JAVA IO机制的一篇文章
http://crazyblog.sinaapp.com/48607.html
posted @ 2013-12-01 16:54  akingseu  阅读(263)  评论(0编辑  收藏  举报