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 blocke.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 http.FileReaderAndBufferedReaderDemo;import java.io.IOException;import java.io.PrintWriter;/* * 對應於PrintStream * 用于格式化輸出到檔案 */public class PrintWriterDemo {public static void main(String[] args) throws IOException {// 簡化的建立方式PrintWriter out = new PrintWriter("file/test4.txt");// 也可以這樣建立: out=new Printer(new BufferedWriter(new// FileWriter("file/test4.txt")));// 格式化輸出到文本out.println('a');out.println(3);out.println(3.5);out.print("我愛你!i love you");out.close();// 從文本讀取剛才的寫入System.out.println(FileReaderAndBufferedReaderDemo.read("file/test4.txt"));}}
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);}}