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

來源:互聯網
上載者:User
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);}}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.