File是IO操作中,唯一可以代表磁盘本身类,File定义了一些与平台无关的操作方法来操作文件,比如说可以创建和删除文件等等。常用操作方法如下:
·public File(String pathname):构造方法,构造一个有路径的文件(文件夹)目录 ·public boolean createNewFile() throws IOException: 创建文件 ·public boolean delete(): 删除文件 ·public boolean exists(): 判断文件是否存在 ·public boolean isDirectory(): 判断文件是否是文件夹 ·public long length(): 返回文件的字节长度 ·public String[] list(): 返回文件夹中所有文件 ·public boolean mkdir(): 创建文件夹 ·public boolean mkdirs(): 创建全路径文件夹(推荐使用)例如:我们需要取得某磁盘某文件下的所有文件名:
public class IOTest { public static void main(String[] args) { loop("D:" + File.separator + "workspace"); } public static void loop(String path){ File file = new File (path); String s[] = null; if(file.isDirectory()){ //是文件夹 s = file.list(); //取得所有的文件名称 for (int i = 0; i < s.length; i++) { loop(path+File.separator+s[i]); //递归,方法自己调用自己 } }else{ System.out.println(path); } } } 字节输出流OutputStream 在IO操作包中,OutputStream是字节输出流的最大的父类OutputStream类的定义:
public abstract class OutputStream extends Object implements Closeable, Flushable 通过类的定义发现,此类是一个抽象类,所以使用的时候必须要依靠子类,如果我们现在要完成的是对文件进行操作,那么需要使用FileOutputStream为OutputStream实例化。FileOutputStream常用构造方法:
·public FileOutputStream(File file) throws FileNotFoundException:创建一个文件字节输出流,连接到一个具体的文件,文件的内容是以覆盖的形式进行操作·public FileOutputStream(File file, boolean append) throws FileNotFoundException:创建一个字节文件输出流,连接到一个文件,如果第二个参数的值为true话,表示的内容以追加的形式进行操作OutputStream类常用方法:·public void write(byte[] b) throws IOException: 将整个字节数组写入到流中·public void write(int b) throws IOException:将一个字节写入到流中·public void write(byte[] b, int off, int len) throws IOException:将字节数组的一部分写入到流中 举例:将“回首向来萧瑟处,归去,也无风雨也无晴”写入到 test.txt文件中。
public class IOTest{ public static void main(String[] args) throws Exception{ File file = new File("D:" + File.separator + "wanczy.txt"); OutputStream out = new FileOutputStream(file); String s = "回首向来萧瑟处,归去,也无风雨也无晴"; byte b[] = s.getBytes();//将字符串转换成字节数组 out.write(b);//将字节数组写入到流中 out.close();//关闭流 } } 字节输入流InputStream 使用InputStream可以读取流中的内容 InputStream类定义如下: public abstract class InputStream extends Object implements Closeable 发现此类依然是一个抽象类,如果要使用的话,必须依靠子类,现在我们需要从文件中读取内容,那么肯定需要用FileInputStream,FileInputStream的构造方法:·public FileInputStream(File file) throws FileNotFoundException:构造FileInputStream对象,连接到一个文件如果要读取内容的话,我们必须要对InputStream读取的方法有所了解:·public abstract int read() throws IOException:读取一个字节·public int read(byte[] b) throws IOException: 将内容读取到一个字节数组中:
public class IOTest { public static void main(String[] args) throws Exception{ File file = new File("D:" + File.separator + "test.txt"); InputStream in = new FileInputStream(file); int length = (int)file.length();//取得文件的字节长度 byte b[] = new byte[length];//字节初始化时用到了获取的文件长度 in.read(b);//读取内容到字节数组 String s = new String(b);//将字节数组转换为字符串 System.out.println(s); in.close(); } } 字符输出流 Writer Writer类是IO操作包中字符输出的最高的父类,主要功能是完成字符流的输出 Writer类定义如下: public abstract class Writer extends Object implements Appendable, Closeable, Flushable 与字节操作的OutputStream和InputStream一样,都是抽象类,如果要进行文件操作的话,则必须使用子类FileWriter,来看到此类的构造方法: ·public FileWriter(File file) throws IOException: 构造文件字符输出流对象,链接到一个文件,表示内容覆盖 ·public FileWriter(File file,boolean append) throws IOException: 构造文件字符输出流,连接到一个文件,如果第二个参数的值为true的话,那么表示追加 我们还需要知道Writer常用的写入的方法: ·public void write(char[] cbuf) throws IOException:将整个字符数组写入流中 ·public void write(String str) throws IOException:将字符串写入到流 ·public void write(int c) throws IOException:将一个字符写入到流中 将字符串写入到流中: public class IOTest { public static void main(String[] args) throws Exception{ File file = new File("D:" + File.separator + "test.txt"); Writer out = new FileWriter(file,true); String s = "一行白鹭上青天"; out.write(s); //将字符串直接写入到流中 out.close(); } } 将字符数组写入到流中: public class IOTest{ public static void main(String[] args) throws Exception{ File file = new File("D:" + File.separator + "test.txt"); Writer out = new FileWriter(file); String s = "hello world"; char c[] = s.toCharArray();//将字符串转换成字符数组 out.write(c);//将字符数组写入到流中 out.close(); } } 字符输入流 Reader 字符输入流Reader是字符输入操作的最高父类 Reader类的定义如下: public abstract class Readerextends Objectimplements Readable, Closeable 发现此类也是一个抽象类,现在要从文件流中读取内容,那么肯定是需要子类FileReader,观察FileReader的构造方法: ·public FileReader(File file) throws FileNotFoundException: 创建文件字符输入流,并且链接到文件 Reader里面有相应的读取的方法: ·public int read() throws IOException: 读取单个字符 ·public int read(char[] cbuf) throws IOException: 读取到一个字符数组 读取文件内容: public class IOTest { public static void main(String[] args) throws Exception{ File file = new File("D:" + File.separator + "test.txt"); Reader in = new FileReader(file); char c[] = new char[1024]; int length = in.read(c);//将内容读取到字符数组中 String s = new String(c,0,length); System.out.println(s); in.close(); } } 以上的程序虽然可以完成文件的内容读取,但是受到字符长度的限制。 我们用到了BufferedReader 来解决这个问题 public String readLine() throws IOException一次性读取一行 此方法表示静态方法,所以调用的时候必须使用BufferedReader类的对象,那么我们必须要使用此类的构造方法。 public BufferedReader(Reader in):构造方法,需要字符输入流对象作为参数,但System.in是字节输入流对象,实际上JDK中专门提供了两个类,专门用于字节到字符的转换: InputStreamReader:字节输入流——字符输入流 OutputStreamWriter:字节输出流——字符输出流 我们现在想要从System.in转换成Reader ,必须使用InputStreamReader完成。构造方法如下: public InputStreamReader(InputStream in) 读取文件内容: public class DuQu { public static void main(String[] args) throws Exception{ BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:" + File.separator + "test.txt")))); StringBuffer sb=new StringBuffer(); String s=""; while((s=br.readLine())!=null){ sb.append(s); } System.out.println(sb); } } 既然字节流,字符流都提供了各自的输入输出流,那么他们的区别是什么呢?在开发中使用哪一组更好呢? 字节流操作的基本单元为字节;字符流操作的基本单元为Unicode码元。 字节流默认不使用缓冲区;字符流使用缓冲区。 字节流通常用于处理二进制数据,实际上它可以处理任意类型的数据,但它不支持直接写入或读取Unicode码元;字符流通常处理文本数据,它支持写入及读取Unicode码元。 在读写文件需要对内容按行处理,比如比较特定字符,处理某一行数据的时候一般会选择字符流。 只是读写文件,和文件内容无关的,一般选择字节流。 PrintStream打印流 public PrintStream(OutputStream out) 构造方法是父类的对象,这种开发模式我们叫做装饰模式,可以根据实例化PrintStream对象的不同,完成向不同的目标输出。 观察PrintStream常用的方法: ·public void print(常见类型对象或者基本数据类型) 向文件中输出: public class Lx6 { public static void main(String[] args) throws Exception{ PrintStream ps=new PrintStream(new FileOutputStream(new File("D:"+File.separator+"test.txt"))); ps.print("你好啊"); ps.close(); } }现在可以得出一个结论,使用打印流是最方便的输出,以后在开发中,对文件进行写入操作,可以尽量的使用打印流进行操作;对文件进行读取操作,可以使用BufferedReader。 输入输出重定向对于我们System.out和System.err都是有固定的输出的目标,就是屏幕,System.in也是有固定的输入目标的,就是键盘,但是在System类中也提 供了重定向的方法,也就是说可以改变输入输出的目标。重定向方法如下:·public static void setIn(InputStream in):对输入进行重定向·public static void setOut(PrintStream out):对输出进行重定向输出重定向举例:
public class IODemo3 { public static void main(String[] args) throws Exception{ PrintStream ps=new PrintStream(new FileOutputStream(new File("D:" + File.separator + "test.txt"))); System.setOut(ps);//输出重定向,将原本输出控制台的东西输出到d盘 System.out.println("三十功名尘与土,八千里路云和月"); } } 运行结果:控制台没有任何输出,原本控制台要输出的语句出现在test.txt文件中,实现了输出的重定向。输入重定向我们一般使用键盘输入,默认system.in也是键盘为输入目标,很少进行重定向,这里不做举例。 对象序列化问题 IO流中还有一个重要的内容,就是对象序列化问题。 大家都知道我们所有定义的对象,都是在内存中,那就是说这个对象没有持久化,那么对象序列化,就是将对象写入到流中,连接到其他的流,就可以将对象写入到硬盘的文件中。 对象的反序列化:从流中读取对象 ·对象序列化操作:ObjectOutputStream ·构造方法:public ObjectOutputStream(OutputStream out) throws IOException ·对象反序列化操作:ObjectInputStream ·构造方法:public ObjectInputStream(InputStream in) throws IOException 序列化操作方法:public final void writeObject(Object obj) throws IOException 反序列化操作方法:public final Object readObject() throws IOException ,ClassNotFoundException 对象序列化的条件: 需要序列化的对象的类必须实现序列化接口 举例:进行对象序列化和反序列化操作,将对象写入doc文件中进行持久化。 public class Person implements Serializable{ private static final long serialVersionUID = -7521916564459248343L; private String name; private String password; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Person(String name,String password) { this.name=name; this.password=password; } } 序列化: public class IODemo1 { public static void main(String[] args) throws Exception{ Person p=new Person("熊九天","xiong"); ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("D:"+File.separator+"test.doc"))); oos.writeObject(p); oos.close(); } }反序列化:
public class IODemo2 { public static void main(String[] args) throws Exception{ ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("D:"+File.separator+"test.doc"))); Object obj=ois.readObject(); Person p=(Person)obj; System.out.println("账号:"+p.getName()+"密码:"+p.getPassword()); ois.close(); } } IO流常用的知识总结大概就这么多了。