JavaIO 总结

时间:2024-07-17 19:05:38

另外参考文章:http://www.ibm.com/developerworks/cn/java/j-lo-javaio/

一. File类

JavaIO 总结

file.createNewFile();file.delete();file.list();file.listFiles();file.isFile();file.isDirectory();file.mkdirs();

删除目录下所有文件:

public void deleteAllFiles(File file) {

  if (file.isFile() || file.list().length == 0) {

    file.delete();

  } else {

    File[] delFiles = file.listFiles();

    for (File delFile : delFiles) {

      deleteAllFiles(delFile);

      delFile.delete();

    }

  }

}

二. 流

流从结构上分为字节流(以字节为处理单位,可以处理二进制数据)和字符流(以字符为处理单位,无法处理二进制数据),它们的底层都是以字节流的方式来实现的

字节流的输入输出流基础是InputStream和OutputStream

字符流的输入输出流基础是Reader和Writer

InputStreamReader是字节流到字符流的桥梁。用于读文件,把文件中的字节读成字符。

OutputStreamWriter是字符流到字节流的桥梁。用于写文件,把字符以字节的形式写入文件。

Java.io体系结构:http://353588249-qq-com.iteye.com/blog/780343

JavaIO 总结

JavaIO 总结

JavaIO 总结

读数据的流程:
open a stream
while more information
read information
close the stream

try {
  InputStream is = new FileInputStream(filePath + "//abc.txt");
  byte[] buff = new byte[200];

  try {
    // 标识每次实际读了多少字节
    int length;
    while (-1 != (length = is.read(buff, 0, 200))) {
      String str = new String(buff, 0, length);
      System.out.println(str);
    }
    is.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
} catch (FileNotFoundException e) {
  e.printStackTrace();
}

写数据的流程
open a stream
while more information
write information
close the stream

String content = "hello world";
// flag用于标识是否在文件内容后再添加内容还是覆盖原有内容,flag=true不覆盖,否则覆盖
OutputStream os = new FileOutputStream(filePath + "//abc.txt", boolean flag);
os.write(content.getBytes());
os.close();

OutputStream os = new FileOutputStream("file.txt");

Writer writer = new OutputStreamWriter(os);

BufferedWriter bw = new BufferedWriter(writer);

bw.write("www.baidu.com");

bw.write("\n");

bw.write("www.qq.com");

bw.close();

InputStream is = new FileInputStream("file.txt");

Reader r = new InputStreamReader(is);

BufferedReader br = new BufferedReader(r);

String str = br.readLine();

while (null != str) {

  System.out.println(str);

  str = br.readLine();

}

br.close();

FileReader = new InputStreamReader(FileInputStream);

FileWriter = new OutputStreamWriter(FileOutputStream);

Serializable

package io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class SerializableTest {

  public static void main(String[] args) throws Exception {
    Person p1 = new Person(10, "person1", 1.5);
    Person p2 = new Person(20, "person2", 1.6);
    Person p3 = new Person(30, "person3", 1.7);

    OutputStream os = new FileOutputStream("person.dat");
    ObjectOutputStream oos = new ObjectOutputStream(os);
    oos.writeObject(p1);
    oos.writeObject(p2);
    oos.writeObject(p3);

    oos.close();

    InputStream is = new FileInputStream("person.dat");
    ObjectInputStream ois = new ObjectInputStream(is);
    Person p;
    for (int i = 0; i < 3; i++) {
      p = (Person) ois.readObject();
      System.out.println(p.name + ", " + p.age + ", " + p.height);
    }

    ois.close();
  }
}

class Person implements Serializable {
  private static final long serialVersionUID = -6460642520310614043L;

  int age;
  String name;
  double height;

  public Person(int age, String name, double height) {
    this.age = age;
    this.name = name;
    this.height = height;
  }
}

Java NIO:参考:http://tutorials.jenkov.com/java-nio/index.html

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class CopyFile {
  public static void main(String[] args) throws Exception {
    String infile = "CopyFile.java";
    String outfile = "CopyFile.txt";
    FileInputStream fin = new FileInputStream(infile);
    FileOutputStream fout = new FileOutputStream(outfile);

    // read data from a channel into a buffer
    FileChannel inChannel = fin.getChannel();
    // write data from a buffer into a channel(Read data out of the Buffer)
    FileChannel outChannel = fout.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(1024);
    while (true) {
      buffer.clear();
      // read data into buffer
      int r = inChannel.read(buffer);
      if (-1 == r) {
        break;
      }
      buffer.flip();
      // write data from buffer
      outChannel.write(buffer);
    }
    inChannel.close();
    outChannel.close();
    fin.close();
    fout.close();
  }
}