标签:
import java.io.*; public class TestFileInputStream { public static void main(String[] args) { int b = 0; FileInputStream in = null; try { in = new FileInputStream("d:\\share\\java\\io\\TestFileInputStream.java"); } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); System.exit(-1); } try { long num = 0; while((b=in.read())!=-1){ System.out.print((char)b); num++; } in.close(); System.out.println(); System.out.println("共读取了 "+num+" 个字节"); } catch (IOException e1) { System.out.println("文件读取错误"); System.exit(-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("文件已复制"); } }
import java.io.*; public class TestFileReader { public static void main(String[] args) { FileReader fr = null; int c = 0; try { fr = new FileReader("d:\\share\\java\\io\\TestFileReader.java"); int ln = 0; while ((c = fr.read()) != -1) { //char ch = (char) fr.read(); System.out.print((char)c); //if (++ln >= 100) { System.out.println(); ln = 0;} } fr.close(); } catch (FileNotFoundException e) { System.out.println("找不到指定文件"); } catch (IOException e) { System.out.println("文件读取错误"); } } }
import java.io.*; public class TestFileWriter { public static void main(String[] args) { FileWriter fw = null; try { fw = new FileWriter("d:\\bak\\unicode.dat"); for(int c=0;c<=50000;c++){ fw.write(c); } fw.close(); } catch (IOException e1) { e1.printStackTrace(); System.out.println("文件写入错误"); System.exit(-1); } } }
import java.io.*; public class TestBufferStream1 { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("d:\\share\\java\\HelloWorld.java"); BufferedInputStream bis = new BufferedInputStream(fis); int c = 0; System.out.println(bis.read()); System.out.println(bis.read()); bis.mark(100); 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+" "); } bis.close(); } catch (IOException e) {e.printStackTrace();} } }
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();} } }
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"); System.out.println(osw.getEncoding()); osw.close(); osw = new OutputStreamWriter( new FileOutputStream("d:\\bak\\char.txt", true), "ISO8859_1"); // latin-1 osw.write("mircosoftibmsunapplehp"); System.out.println(osw.getEncoding()); osw.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class TestTransForm2 { public static void main(String args[]) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = null; try { s = br.readLine(); while(s!=null){ if(s.equalsIgnoreCase("exit")) break; System.out.println(s.toUpperCase()); s = br.readLine(); } br.close(); } catch (IOException e) { e.printStackTrace(); } } } //阻塞
import java.io.*; public class TestDataStream { public static void main(String[] args) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); try { dos.writeDouble(Math.random()); dos.writeBoolean(true); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); System.out.println(bais.available()); DataInputStream dis = new DataInputStream(bais); System.out.println(dis.readDouble()); System.out.println(dis.readBoolean()); dos.close(); dis.close(); } catch (IOException e) { e.printStackTrace(); } } }
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;} } } }
import java.io.*; public class TestPrintStream2 { public static void main(String[] args) { String filename = args[0]; if(filename!=null){list(filename,System.out);} } public static void list(String f,PrintStream fs){ try { BufferedReader br = new BufferedReader(new FileReader(f)); String s = null; while((s=br.readLine())!=null){ fs.println(s); } br.close(); } catch (IOException e) { fs.println("无法读取文件"); } } }
import java.util.*; import java.io.*; public class TestPrintStream3 { public static void main(String[] args) { String s = null; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); try { FileWriter fw = new FileWriter ("d:\\bak\\logfile.log", true); //Log4J PrintWriter log = new PrintWriter(fw); while ((s = br.readLine())!=null) { if(s.equalsIgnoreCase("exit")) break; System.out.println(s.toUpperCase()); log.println("-----"); log.println(s.toUpperCase()); log.flush(); } log.println("==="+new Date()+"==="); log.flush(); log.close(); } catch (IOException e) { e.printStackTrace(); } } }
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; //透明的,序列化时不考虑 }
J2SE之流_InputStream_OutputStream_ Reader_Write
标签:
原文地址:http://www.cnblogs.com/gimin/p/4782385.html