java 基础之IO流 其他IO类 --08

来源:互联网 发布:口风琴多少钱一个淘宝 编辑:程序博客网 时间:2024/04/29 08:09


18 File 文件类

18.1 概念

1.用来将文件或者文件夹封装成对象
2.方便对文件与文件夹的属性信息进行操作。
3.File对象可以作为参数传递给流的构造函数。
4.了解File类中的常用方法。

18.2 构造方法及摘要

File(File parent, String child) 
          根据 parent 抽象路径名和 child 路径名字符串创建一个新 File 实例。 
File(String pathname) 
          通过将给定路径名字符串转换为抽象路径名来创建一个新 File 实例。 
File(String parent, String child) 
          根据 parent 路径名字符串和 child 路径名字符串创建一个新 File 实例。 
File(URI uri) 
          通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例。 

18.3 常用方法

boolean delete() 
          删除此抽象路径名表示的文件或目录。 
boolean equals(Object obj) 
          测试此抽象路径名与给定对象是否相等。 
 boolean exists() 
          测试此抽象路径名表示的文件或目录是否存在。 
 String getName() 
          返回由此抽象路径名表示的文件或目录的名称。 
 String getParent() 
          返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回 null。 
 File getParentFile() 
          返回此抽象路径名父目录的抽象路径名;如果此路径名没有指定父目录,则返回 null。 
 String getPath() 
          将此抽象路径名转换为一个路径名字符串。 
 boolean isAbsolute() 
          测试此抽象路径名是否为绝对路径名。 
 boolean isDirectory() 
          测试此抽象路径名表示的文件是否是一个目录。 
 boolean isFile() 
          测试此抽象路径名表示的文件是否是一个标准文件。 
 long length() 
          返回由此抽象路径名表示的文件的长度。 
 String[] list() 
          返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。 
 File[] listFiles() 
          返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。 
 boolean mkdir() 
          创建此抽象路径名指定的目录。 
 boolean mkdirs() 
          创建此抽象路径名指定的目录,包括所有必需但不存在的父目录。 
 String toString() 
          返回此抽象路径名的路径名字符串。 

18.4 示例

1.列出一个文件夹下所有的子文件夹以及子文件

package test2;import java.io.File;public class FileTest {public static void printDir(File f){//s+="---";if(f.isDirectory()){System.out.println("目录:"+ f.getName());File[] fs = f.listFiles();for(File ff : fs){printDir(ff);}}else{System.out.println("文件:"+ f.getName());}}public static void main(String[] args) {printDir(new File("src"));}}


2.列出目录下所有内容---带层次:
package test2;import java.io.File;public class FileTest {public static void printDir(File f,String s){s+="---";if(f.isDirectory()){System.out.println("目录:"+s+ f.getName());File[] fs = f.listFiles();for(File ff : fs){printDir(ff,s);}}else{System.out.println("文件:"+s + f.getName());}}public static void main(String[] args) {printDir(new File("src"),"|-");}}

3.删除目录
public static void delDir(File f){if(f.isDirectory()){File[] fs = f.listFiles();for(File ff : fs){delDir(ff);}f.delete();}else{f.delete();}}


19 io其他类

19.1RandomAccessFile

随机访问文件,自身具备读写的方法。
通过skipBytes(int x),seek(int x)来达到随机访问。
RandomAccessFile(File file, String mode) 
          创建从中读取和向其中写入(可选)的随机访问文件流,该文件由 File 参数指定。 
RandomAccessFile(String name, String mode) 
          创建从中读取和向其中写入(可选)的随机访问文件流,该文件具有指定名称。 

19.2管道流 

PipedInputStream和PipedOutputStream
PipedInputStream() 
          创建尚未连接的 PipedInputStream。
PipedInputStream(PipedOutputStream src) 
          创建 PipedInputStream,使其连接到管道输出流 src。


19.3打印流 PrintWriter与PrintStream

特点:1.可以直接操作输入输出流
2.此2个流均不会抛出IO异常,而是,异常情况仅设置可通过 checkError 方法测试的内部标志。
3.可以在构造方法中自动刷新该流
4.所使用的编码均是平台默认的编码

19.4 序列流

1.概念

SequenceInputStream 对多个流进行合并。
SequenceInputStream(Enumeration<? extends InputStream> e) 
          通过记住参数来初始化新创建的 SequenceInputStream,该参数必须是生成运行时类型为
 InputStream 对象的 Enumeration 型参数。 
SequenceInputStream(InputStream s1, InputStream s2) 
          通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,
 先读取 s1,然后读取 s2), 以提供从此 SequenceInputStream 读取的字节。 


2.一个简单的合并文件示例

package test2;import java.io.BufferedOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.SequenceInputStream;import java.util.Enumeration;import java.util.Vector;public class SequenceInputStreamTest {public static void mergeFile(Enumeration<? extends InputStream> es,String newFilepath) {SequenceInputStream ss = new SequenceInputStream(es);BufferedOutputStream br = null;try {br = new BufferedOutputStream(new FileOutputStream(newFilepath));byte[] bs = new byte[1024*5];int i = -1;while((i = ss.read(bs, 0, bs.length))!= -1){br.write(bs, 0, i);br.flush();};} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {try {if(ss!= null){ss.close();}if(br!= null){br.close();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}public static void main(String[] args) {Vector<FileInputStream> v = new Vector<FileInputStream>();try {v.add(new FileInputStream("1.txt"));v.add(new FileInputStream("abc.txt"));v.add(new FileInputStream("b.txt"));mergeFile(v.elements(),"idcastlib/abc.txt");} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

3.切割文件示例

/** * 切割文件 * @param f 切割的文件 * @throws Exception  */public static void cutFile(File f) throws Exception{BufferedInputStream bin = null;BufferedOutputStream bos = null;File fto =null;StringBuilder s = null;try {bin  = new BufferedInputStream(new FileInputStream(f));byte[] b = new byte[1024*1024];int i = -1;int count = 1;while((i = bin.read(b))!= -1){if(fto == null){s = new StringBuilder(f.getPath());s.insert(s.lastIndexOf("."), "_"+count++);fto = new File(s.toString());if(fto.createNewFile()) bos = new BufferedOutputStream(new FileOutputStream(fto));else throw new Exception("create file error : filepath :" +s.toString()) ;}if(fto.length()>f.length()/2){ //将文件 切割成2分s = new StringBuilder(f.getPath());s.insert(s.lastIndexOf("."), "_"+count++);fto = new File(s.toString());if(bos != null){bos.close();}if(fto.createNewFile()) bos = new BufferedOutputStream(new FileOutputStream(fto));else throw new Exception("create file error : filepath :" +s.toString()) ;}bos.write(b, 0, i);bos.flush();}} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{if(bos != null){try {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if(bin != null){try {bin.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

19.5 对象流

ObjectInputStream与ObjectOutputStream
被操作的对象需要实现Serializable (标记接口);
对象流示例
package test2;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.ArrayList;import java.util.List;import cn.A;/** * 对象流测试运用 * @author 李昂志 */public class ObjectIOTest {public static void writeObject(){ObjectOutputStream out = null;A a = new A("liyi","123");A a2 = new A("liyi2","1234");A a3 = new A("liyi3","12356");List<A> s = new ArrayList<A>();s.add(a);s.add(a2);s.add(a3);try {out = new ObjectOutputStream(new FileOutputStream("1.txt"));out.writeObject(s);out.flush();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if(out!= null){try {out.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}public static void readeObject(){ObjectInputStream in = null;try {in = new ObjectInputStream(new FileInputStream("1.txt"));Object o = null;List<A> ls =  (List<A>)in.readObject();for(A a: ls){System.out.println(a);}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally {if(in!= null){try {in.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}public static void main(String[] args) {readeObject();writeObject();}}class A implements Serializable{private static final long serialVersionUID = 1L;String name;String pass;public A(){}public A(String name,String pass){this.name = name;this.pass = pass;}@Overridepublic String toString() {return "A [name=" + name + ", pass=" + pass + "]";}}


19.6 其他io对象类

操作基本数据类型
DataInputStream与DataOutputStream
操作字节数组
ByteArrayInputStream与ByteArrayOutputStream
操作字符数组
CharArrayReader与CharArrayWrite
操作字符串
StringReader 与 StringWriter


19.7 字符编码

1.常用编码

1)ASCII:美国标准信息交换码。用一个字节的7位可以表示。
2)ISO8859-1:拉丁码表。欧洲码表。(拉丁语系,意大利语,法语)
用一个字节的8位表示。最高位置1.
3)GB2312:中国的中文编码表。两个字节一个汉字。兼容ASCII,为了和ASCII不冲突,两个字节最高位都置1。汉字都是负数。
4)GBK:中国的中文编码表升级,融合了更多的中文文字符号。(②要么这种)
5)Unicode:国际标准码,融合了多种文字。
所有文字都用两个字节来表示,Java语言使用的就是unicode,char型

UTF-8:最多用三个字节来表示一个字符。

2.转换流的编码运用

1)可以将字符以指定编码格式存储。
2)可以对文本数据指定编码格式来解读。
3)指定编码表的动作由构造函数完成。
0 0