07 JAVA IO流 练习

来源:互联网 发布:乐清知临公立宿舍学校 编辑:程序博客网 时间:2024/05/16 07:01

1. OutputStream

import java.io.*;public class TestOutputStream {public static void main(String[] args) {OutputStreamWriter osw;try {osw = new OutputStreamWriter(new FileOutputStream("/Users/apple/Desktop/io/char.txt"));osw.write("abcdefghijklmn我是几个汉字");System.out.println(osw.getEncoding()); //获取当前系统的默认编码osw.close();osw = new OutputStreamWriter(new FileOutputStream("/Users/apple/Desktop/io/char.txt", true), "ISO8859-1"); //true表示append,而不是删除重建osw.write("opqrstuvwxyz我是几个汉字");System.out.println(osw.getEncoding());osw.close();} catch (FileNotFoundException e) {System.out.println("找不到相关的文件!");} catch (IOException e) {e.printStackTrace();}}}


2. 复制文件

/** * 使用FileReader复制文件,但是我们却不可以使用FileReader复制图片,因为字符流就是字节流+编码表, * 使用字符流去复制图片,字符流会默认将图片的字节码格式进行编码,导致与原图片不一致 */import java.io.*;public class CopyFile {public static void main(String[] args) {FileReader fr = null;FileWriter fw = null;String dir = "/Users/apple/Desktop/io/";int c = -1;//char[] cbuf = new char[60];try {fr = new FileReader(dir+"CopyFile.java");//如果文件不存在,会创建文件,但是不能创建子目录fw = new FileWriter(dir+"copiedFile.java");/*一次性装入60个字符到cbuf里面,如果不够会填充再返回-1 * while(fr.read(cbuf) != -1)System.out.println(cbuf); */while((c = fr.read()) != -1) {fw.write(c);}fw.close(); fr.close();} catch(FileNotFoundException fnfe) {System.out.println("找不到指定文件!");fnfe.printStackTrace();System.exit(-1);} catch(IOException ioe) {System.out.println("文件复制错误!");ioe.printStackTrace();System.exit(-1);}System.out.println("复制成功~");}}

3. Buffer
import java.io.*;public class TestBufferStream {public static void main(String[] args) {FileInputStream fis =null; BufferedInputStream bis = null;BufferedReader br = null;BufferedWriter bw = null;try{fis = new FileInputStream("/Users/apple/Desktop/io/testBuffer.txt");bis = new BufferedInputStream(fis,15); //buffer的大小为10字节int c = 0;System.out.println((char)bis.read());System.out.println(bis.read());bis.mark(1);//标记当前位置,readlimit只是说用户读取多少个字节后,mark可能会失效(“可能”,而不是一定,因为这取决于buffer的大小)for(int i = 0; i<=10 && (c = bis.read())!=-1; i++) {System.out.print((char)c+" ");}System.out.println();bis.reset();//回到刚才标记的位置for(int i = 0; i<=10 && (c = bis.read())!=-1; i++) {System.out.print((char)c+" ");}System.out.println(); bis.close();bw = new BufferedWriter(new FileWriter("/Users/apple/Desktop/io/testBuffer1.txt"));br = new BufferedReader(new FileReader("/Users/apple/Desktop/io/testBuffer1.txt"));String s = null;for(int i = 0; i < 50; i++) {s = String.valueOf(Math.random());bw.write(s);bw.newLine();//添加分隔符}bw.flush();while((s = br.readLine()) != null) {System.out.println(s);}bw.close();br.close();} catch(FileNotFoundException fe) {fe.getMessage();} catch(IOException e) {e.getMessage();}}}


4. DataStream

import java.io.*;public class TestDataStream {public static void main(String[] args) {//在内存中创建了一个byte类型数组空间,并且创建了一个输出流管道指向它ByteArrayOutputStream baos = new ByteArrayOutputStream();DataOutputStream dos = new DataOutputStream(baos);//套接try {dos.writeInt(100);dos.writeBoolean(true); //布尔型占一个字节ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());System.out.println(bais.available());//有几个有效字节DataInputStream dis =  new DataInputStream(bais);//先进先出System.out.println(dis.readInt());System.out.println(dis.readBoolean());dos.close();dis.close();} catch(IOException e) {e.printStackTrace();}}}

5. Object

import java.io.*;class T implements Serializable {int x = 1;int y = 2;double z = 3.0;transient int k = 8;}public class TestObject {public static void main(String args[]) {T t = new T();t.k = 2;System.out.println(t.k);try {FileOutputStream fos = new FileOutputStream("/Users/apple/Desktop/io/t.bak");ObjectOutputStream oos = new ObjectOutputStream(fos);oos.writeObject(t);oos.flush();oos.close();FileInputStream fis = new FileInputStream("/Users/apple/Desktop/io/t.bak");ObjectInputStream ois = new ObjectInputStream(fis);T tr =  (T)ois.readObject();System.out.println("t.x:"+tr.x+" t.y:"+tr.y+" t.z:"+tr.z+" t.k:"+tr.k);} catch (FileNotFoundException e) {System.out.println("没有相关文件!");} catch(IOException e1) {System.out.println("对象写入失败!");} catch(ClassNotFoundException e2) {System.out.println("找不到对象类型!");}}}

6. print

import java.io.*;public class TestPrint {static void print(PrintStream ps) {ps.println("print~");}public static void main(String[] args) {PrintStream ps = null;try {FileOutputStream fos = new FileOutputStream("/Users/apple/Desktop/io/print1.dat");ps = new PrintStream(fos);} catch(FileNotFoundException e) {System.out.println("找不到相关文件!");}if(ps != null) {System.setOut(ps);//我们重置了输出位置}int ln = 0;for(char c = 0; c <= 100; c++) {System.out.print(c + " ");if(ln++ >= 25) {System.out.println();ln = 0;}}System.out.println();print(ps);}}

7. System.in

import java.io.*;import java.util.Date;public class TestSystemIn {public static void main(String[] args) {String s = null;BufferedReader br = new BufferedReader(new InputStreamReader(System.in));try {FileWriter fw = new FileWriter("/Users/apple/Desktop/io/logFile.log",true);PrintWriter pw = new PrintWriter(fw);while((s = br.readLine()) != null) {if(s.equalsIgnoreCase("exit"))break;System.out.println(s.toUpperCase());pw.println("-----------------");pw.println(s.toUpperCase());pw.flush();}pw.println("======="+new Date()+"=======");pw.flush();br.close();} catch (IOException e) {e.printStackTrace();}}}



0 0
原创粉丝点击