import java.io.*; class ByteArrayStreamDemo { public static void main(String[] args) { //数据源 ByteArrayInputStream bis = new ByteArrayInputStream("ABCDEF".getBytes());//getBytes()是将一个字符串转化为一个字节数组 //数据目的 ByteArrayOutputStream bos = new ByteArrayOutputStream(); int by = 0; while((by = bis.read())!=-1) { bos.write(by); } System.out.println(bos.size()); System.out.println(bos.toString()); //bos.writeTo(new FileOutputStream("ByteArray.txt"));//只有此处需要对异常进行处理。 } }
import java.io.*; class CharArrayStreamDemo { public static void main(String[] args)throws IOException { //数据源 CharArrayReader car = new CharArrayReader("世界第一等".toCharArray());//toCharArray()是将一个字符串转化为一个字符数组 //数据目的 CharArrayWriter caw = new CharArrayWriter(); int by = 0; while((by = car.read())!=-1) { caw.write(by); } System.out.println(caw.size()); System.out.println(caw.toString()); //caw.writeTo(new FileOutputStream("CharArray.txt"));//需要对异常进行处理。 } }
import java.io.*; class DataStreamDemo { public static void main(String[] args)throws IOException { //WriteData(); //ReadData(); WriteUTFDemo(); ReadUTFDemo(); } public static void WriteData()throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("Data.txt")); dos.writeInt(10); dos.writeChar(97); dos.writeDouble(3.1415926); dos.writeBoolean(true); dos.close(); } public static void ReadData()throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("Data.txt")); System.out.println(dis.readInt()); System.out.println(dis.readChar()); System.out.println(dis.readDouble()); System.out.println(dis.readBoolean()); dis.close(); } public static void WriteUTFDemo()throws IOException { DataOutputStream dos = new DataOutputStream(new FileOutputStream("Data-UTF.txt")); dos.writeUTF("你好"); dos.close(); } public static void ReadUTFDemo()throws IOException { DataInputStream dis = new DataInputStream(new FileInputStream("Data-UTF.txt")); System.out.println(dis.readUTF()); dis.close(); } }
import java.io.*; class ReadPiped implements Runnable { private PipedInputStream in; ReadPiped(PipedInputStream in) { this.in = in; } public void run() { try { byte[] buf = new byte[1024]; System.out.println("读取前,没有数据,管道阻塞开始"); int len = in.read(buf); System.out.println("读取数据,管道阻塞结束"); String s = new String(buf,0,len); System.out.println(s); } catch(IOException e) { throw new RuntimeException("管道读取流失败!"); } finally { try { in.close(); } catch(IOException e) { throw new RuntimeException("流关闭失败!"); } } } } class WritePiped implements Runnable { private PipedOutputStream out; WritePiped(PipedOutputStream out) { this.out = out; } public void run() { try { System.out.println("开始写入数据,等待5秒"); Thread.sleep(5000); out.write("guandao lai le".getBytes()); out.close(); } catch(Exception e) { throw new RuntimeException("管道输出流失败!"); } } } class PipedStreamDemo { public static void main(String[] args)throws IOException { PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream(); in.connect(out);//使此管道输入流连接到管道输出流 ReadPiped r = new ReadPiped(in); WritePiped w = new WritePiped(out); new Thread(r).start(); new Thread(w).start(); } }
//例子5:
import java.io.*; class PrintStreamDemo { public static void main(String[] args)throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in)); PrintWriter ps = new PrintWriter(System.out,true);//(new BufferedWriter(new FileWriter("F:\\myfile\\print.txt")),true) String line = null; while((line = bufr.readLine())!=null) { if("over".equals(line)) break; ps.println(line.toUpperCase()); //ps.flush(); } ps.close(); bufr.close(); } }
import java.io.*; import java.util.*; class PropertiesDemo { public static void main(String[] args)throws IOException { //setAndget(); //method_1(); method_2(); } //演示如何将流中的数据添加放到集合中 //想要将key-value.txt数据存到集合进行操作 /* 步骤:1.用一个流和key-value相关联 2.读一行数据,将该行数据用"="进行切割 3.等号左边作为键,右边作为值,存入到Properties集合即可。 */ //方法1(load方法的原理) public static void method_1()throws IOException { Properties p = new Properties(); //FileInputStream fis = new FileInputStream("F:\\myfile\\key-value.txt"); BufferedReader bufr = new BufferedReader(new FileReader("F:\\myfile\\key-value.txt")); String str=null; while((str=bufr.readLine())!=null) { String[] s = str.split("="); System.out.println(s[0]+"...."+s[1]); p.setProperty(s[0],s[1]); } //System.out.println(p); p.list(System.out); bufr.close(); } //方法2(直接调用load方法) public static void method_2()throws IOException { Properties p = new Properties(); p.load(new FileReader("F:\\myfile\\key-value.txt"));//p.load(new FileInputStream("F:\\myfile\\key-value.txt")) p.setProperty("zhaoliu","100");//修改已有的姓名的年龄并保存在文件中 p.store(new FileWriter("F:\\myfile\\key-value.txt"), p.toString()); System.out.println(p); } //设置和获取元素 public static void setAndget() { Properties p = new Properties(); p.setProperty("zhangsan","30"); p.setProperty("lisi","40"); System.out.println(p); String value = p.getProperty("lisi"); System.out.println("value="+value); //p.setProperty("lisi",89+"");//修改lisi年龄 Set<String> name = p.stringPropertyNames(); Iterator<String> it = name.iterator(); while(it.hasNext()) { String key = it.next(); System.out.println("key:"+key+" "+"value"+p.getProperty(key)); } /* for(String key: name) { System.out.println("key:"+key+" "+"value"+p.getProperty(key)); } */ } }
import java.io.*; class RandomAccessFileDemo { public static void main(String[] args)throws IOException { //WriteFile(); //WriteFile2(); ReadFile(); } public static void WriteFile()throws IOException { RandomAccessFile raf = new RandomAccessFile("Random.txt","rw"); raf.write("李四".getBytes()); raf.writeInt(65); raf.write("王武".getBytes()); raf.writeInt(67); raf.close(); } public static void WriteFile2()throws IOException { //改变指针,随机位置写入数据,还可以修改之前位置所写的数据(不会重新覆盖创建的文件) RandomAccessFile raf = new RandomAccessFile("Random.txt","rw"); raf.seek(8*3); raf.write("周七".getBytes()); raf.writeInt(69); raf.seek(8*2); raf.write("赵六".getBytes()); raf.writeInt(68); raf.close(); } public static void ReadFile()throws IOException { RandomAccessFile raf = new RandomAccessFile("Random.txt","r"); PrintStream ps = System.out; System.setOut(ps); byte[] buf = new byte[4]; //ps.println(raf.getFilePointer());//获取当前指针位置 //raf.seek(8*1);//改变当前指针的位置(可以前后移动) //raf.skipBytes(8);//跳过8个字节(只能往后跳) while(raf.read(buf)!=-1) { String name = new String(buf); ps.println("name="+name); int age= raf.readInt(); ps.println("age="+age); } raf.close(); } }
import java.io.*; import java.util.*; class SequenceStreamDemo { public static void main(String[] args)throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>(); v.add(new FileInputStream("F:\\myfile\\1.txt")); v.add(new FileInputStream("F:\\myfile\\2.txt")); v.add(new FileInputStream("F:\\myfile\\3.txt")); Enumeration<FileInputStream> enu = v.elements(); SequenceInputStream sis = new SequenceInputStream(enu);//将多个流整合为一个流对象 BufferedWriter bufw = new BufferedWriter(new FileWriter("F:\\myfile\\123.txt")); int num = 0; while((num=sis.read())!=-1) { bufw.write(num); bufw.flush(); } sis.close(); bufw.close(); } }
import java.io.*; class StringReaderWriterDemo { public static void main(String[] args)throws IOException { StringReader sr = new StringReader("Thank you very much"); StringWriter sw = new StringWriter(); //StringWriter s = sw.append(‘A‘); int by = 0; while((by=sr.read())!=-1) { sw.write(by); } System.out.println(sw.toString()); } }
import java.io.*; class ObjectStreamDemo { public static void main(String[] args)throws Exception { //writeObj(); readObj(); } public static void writeObj()throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Object.txt")); oos.writeObject(new Person("zhangsan",25)); oos.close(); } public static void readObj()throws Exception { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Object.txt")); Person p =(Person)ois.readObject(); System.out.println(p); ois.close(); } }
Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
原文:http://www.cnblogs.com/XYQ-208910/p/4918714.html