java常用类解析四:I/O流典型使用方式

阅读更多
package http;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * Read和Write分别对应InputStream和OutputStream
 * 前者用于字符流,后者用于字节流
 * FileReader和FileWrite分别对应与FileInputStream和FileOutputStream
 * BufferedReader和BufferedWrite分别对应与BufferedInputStream和
 * BufferedOutputStream
 * 此示例实现文本文件的字符读写
 */
public class FileReaderAndBufferedReaderDemo {
	public static String read(String fileName) throws IOException {
		StringBuilder str = new StringBuilder();
		BufferedReader in = new BufferedReader(new FileReader(fileName));
		String s;
		while ((s = in.readLine()) != null)
			str.append(s + '\n');
		in.close();
		return str.toString();
	}

	public static void write(String fileName, boolean append)
			throws IOException {
		BufferedWriter out = new BufferedWriter(
				new FileWriter(fileName, append));
		out.write("我是dahai!java hello!");
		out.close();
	}

	public static void main(String[] args) {
		try {
			write("file/test3.txt", false);
			System.out.println(read("file/test3.txt"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
 
package http;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;


/*
 * DataInputStream用于读取格式化的数据
 */
public class DataInputStreamAndByteArrayInputStreamDemo {
	public static void main(String[] args) throws IOException {
		DataInputStream in = new DataInputStream(new ByteArrayInputStream(
				FileReaderAndBufferedReaderDemo.read("file/test3.txt")
						.getBytes()));
		while (in.available() != 0)
			System.out.print((char) in.readByte());

	}
}
 
package test;

import http.FileReaderAndBufferedReaderDemo;

import java.io.IOException;
import java.io.StringReader;
/*
 * StringReader操作的是字符串
 */
public class StringReaderDemo {
	public static void main(String[] args) throws IOException {
		StringReader in = new StringReader(FileReaderAndBufferedReaderDemo
				.read("file/test3.txt"));
		int c;
		while ((c = in.read()) != -1)
			System.out.print((char) c);
	}
}
 
package test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/*
 * RandomAccessFile直接继承Object,可以进行随机输入和输出,类似于c语言的文件操作
 * 要指明以什么方式打开文件,用这个类时要知道文件的排版,该类有读写基本类型和UTF-8字符串
 * 的各种方法,可以定位到文件的某一位置进行读写
 */
public class RandomAccessFileDemo {
	public static void main(String[] args) throws FileNotFoundException {
		RandomAccessFile out = new RandomAccessFile("file/test5", "rw");
		try {
			out.writeInt(1);
			out.writeDouble(3.3);
			out.writeChar('a');
			out.writeUTF("hello,java!");
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		RandomAccessFile in = new RandomAccessFile("file/test5", "r");
		try {
			in.seek(4);
			System.out.println(in.readDouble());
			in.seek(4+8+2);
			System.out.println(in.readUTF());
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
 
package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/*
 * ObjectInputStream(ObjectOutputStream)用于对象的序列化
 * 直接对一个对象进行读写,该对象须实现Serializable
 */
public class ObjectInputStreamDemo {
	public static void writeObject(String fileName, Object o, boolean isAppend)
			throws FileNotFoundException, IOException {
		ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
				fileName, true));
		out.writeObject(o);
		out.close();
	}

	public static Object readObject(String fileName)
			throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(
				fileName));
		Object o = in.readObject();
		in.close();
		return o;
	}

	public static void main(String[] args) {
		try {
			ObjectInputStreamDemo.writeObject("file/object", new Person(),
					false);
			((Person) ObjectInputStreamDemo.readObject("file/object"))
					.display();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

class Person implements Serializable {
	private String name = "刘海房liuhaifang";
	private int sex = 0;

	Person(String name, int sex) {
		this.name = name;
		this.sex = sex;
	}

	Person() {
	}

	void display() {
		System.out.println("my name is :" + name);
		String s = (sex == 0) ? "男" : "女";
		System.out.println(s);
	}
}
 

你可能感兴趣的:(JNI,nbsp,Borland,freemarker,VBScript)