javaIO流

来源:互联网 发布:淘宝购物业务流程图 编辑:程序博客网 时间:2024/05/23 15:38
 
 

写入文件的两种方式

<1>

import java.io.*;public class TestFileOutputStream {  public static void main(String[] args) {   int b = 0;   FileInputStream in = null;   FileOutputStream out = null;   try {     in = new FileInputStream("d:/share/java/HelloWorld.java");     out = new FileOutputStream("d:/share/java/io/HW.java");     while((b=in.read())!=-1){       out.write(b);     }////////////////////////////////重点     in.close();      out.close();   } catch (FileNotFoundException e2) {     System.out.println("找不到指定文件"); System.exit(-1);   } catch (IOException e1) {     System.out.println("文件复制错误"); System.exit(-1);   }   System.out.println("文件已复制");  }}


<2>

import java.io.*;public class TestBufferStream2 {  public static void main(String[] args) {    try {      BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\share\\java\\dat2.txt"));      BufferedReader br = new BufferedReader(             new FileReader("d:\\share\\java\\dat2.txt"));      String s = null;、      for(int i=1;i<=100;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 (IOException e) { e.printStackTrace();}  }} 
三种功能实现:
标准控制台输入,回车大写
标准控制台输入,回车写入文件
输出在文件中
<3>
标准控制台输入,回车大写
import java.io.*;public class Test {  public static void main(String args[]) {   BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // 控制台输入流,提高效率          String s = null;    try {     while((s = br.readLine())!=null){        if(s.equalsIgnoreCase("exit")) break;        System.out.println(s.toUpperCase());      }      br.close();    } catch (IOException e) {      e.printStackTrace();    }  }} /阻塞

<4>
标准控制台输入,回车写入文件
有两种方法:
一种边读边写,BufferedReade, BufferedWriter
import java.io.*;public class Test { public static void main(String args[]) {  BufferedReader br = null;  BufferedWriter bw = null;  try {   br = new BufferedReader(new InputStreamReader(System.in)); // 控制台输入流,提高效率   bw = new BufferedWriter(new FileWriter("d:\\fangfa1.txt"));  } catch (IOException e1) {   e1.printStackTrace();  }  String s = null;  try {   while ((s = br.readLine()) != null) {    if (s.equalsIgnoreCase("exit"))     break;    bw.write(s.toUpperCase());   }   br.close();   bw.flush();   bw.close();  } catch (IOException e) {   e.printStackTrace();  } }} }

一种打印流,PrintStream , System.setOut(ps);
import java.io.*;public class Test { public static void main(String args[]) {  BufferedReader br = null;  PrintStream ps = null;  try {   br = new BufferedReader(new InputStreamReader(System.in)); // 控制台输入流,提高效率   ps = new PrintStream(new FileOutputStream("d:\\fangafa2.txt"));  } catch (FileNotFoundException e1) {   e1.printStackTrace();  }  String s = null;  if (ps != null) {   System.setOut(ps);  }  try {   while ((s = br.readLine()) != null) {    if (s.equalsIgnoreCase("exit"))     break;    System.out.println(s.toUpperCase());   }   br.close();  } catch (IOException e) {   e.printStackTrace();  } }}

<3>
import java.io.*;public class TestPrintStream1 {   public static void main(String[] args) {    PrintStream ps = null;    try {      FileOutputStream fos =               new FileOutputStream("d:\\bak\\log.dat");      ps = new PrintStream(fos);    } catch (IOException e) {      e.printStackTrace();    }    if(ps != null){      System.setOut(ps);    }    int ln = 0;    for(char c = 0; c <= 60000; c++){      System.out.print(c+ " ");      if(ln++ >=100){ System.out.println(); ln = 0;}    }  }}} 

<ObjectStream>
ObjectInputStream  ObjectOutputStream 接口:Serializable//序列化  修饰:transient//透明的import java.io.*;public class TestObjectIO { public static void main(String args[]) throws Exception {  T t = new T();  t.k = 8;  FileOutputStream fos = new FileOutputStream("d:/share/java/io/testobjectio.dat");  ObjectOutputStream oos = new ObjectOutputStream(fos);  oos.writeObject(t);  oos.flush();  oos.close();    FileInputStream fis = new FileInputStream("d:/share/java/io/testobjectio.dat");  ObjectInputStream ois = new ObjectInputStream(fis);  T tReaded = (T)ois.readObject();  System.out.println(tReaded.i + " " + tReaded.j + " " + tReaded.d + " " + tReaded.k);   }}class T  implements Serializable{ int i = 10; int j = 9; double d = 2.3; transient int k = 15;}

<2>
Writer比OutputStream牛逼,字符流比字节流好用,所以只有OutputStreamWriter。write(new String);
import java.io.*;public class TestTransForm1 { public static void main(String[] args) { try { OutputStreamWriter osw = new OutputStreamWriter( new FileOutputStream("d:\\bak\\char.txt")); osw.write("mircosoftibmsunapplehp");///////////字符流有write(new String); System.out.println(osw.getEncoding()); osw.close(); } catch (IOException e) { e.printStackTrace(); } }

<总结>
FileInputSream FileOutputStream
FileWriter FileReader
InputSreamReader OutputStreamWriter:转换流
BufferedInputStream BufferedOutputStream:缓冲流;BufferedInputStream readLine(),BufferedOutputStream newLine()
BufferedWriter BufferedReader
DataInputStream DataOutputStream :数据流;DataInputStream readUTF()readLong(),DataOutputStream writeUTF(),writeLong()
ByteArrayInputStream ByteArrayOutputStream:字节数组流; ByteArrayInputStream 参数字节数组,ByteArrayOutputStream toByteArray()
PrintSream PrintWriter:打印流;print(),println()
ObjectInputStream ObkjectInputSream: 接口:Serializable//序列化 修饰:transient//透明的