//FileReader FileWriter 读写英文
public void FileReaderAndWriter1() throws Exception { File filePath = new File("E:/iotest"); if (!filePath.exists()) { filePath.mkdirs(); } //写文件 File file = new File(filePath.getPath(), "writer.txt"); FileWriter writer = new FileWriter(file); for (int i = 0; i < 100; i++) { writer.write(new char[]{'a','b','c'}); writer.write(new char[]{'\r','\n'}); } writer.close(); //读取文件 FileReader reader = new FileReader(file); char[] chars=new char[100]; int readCount =0; while((readCount=reader.read(chars))>0){ System.out.print(String.valueOf(chars).substring(0, readCount)); } reader.close(); } //FileReader FileWriter 读写中文 public void FileReaderAndWriter2() throws Exception { File filePath = new File("E:/iotest"); if (!filePath.exists()) { filePath.mkdirs(); } //写文件 File file = new File(filePath.getPath(), "writer.txt"); FileWriter writer = new FileWriter(file); BufferedWriter bfWriter = new BufferedWriter(writer); for (int i = 0; i < 10000; i++) { bfWriter.write("当前行号" + i); bfWriter.write("\r\n"); } bfWriter.flush(); writer.close(); bfWriter.close(); //读取文件 FileReader reader = new FileReader(file); BufferedReader bfReader =new BufferedReader(reader); String line=""; while((line=bfReader.readLine())!=null){ System.out.println(line); } } //FileInputStream FileOutputStream public void FileStream() throws Exception{ File file1 =new File("E:/iotest","writer.txt"); FileInputStream input =new FileInputStream(file1); BufferedInputStream bInput =new BufferedInputStream(input); File file2 =new File("E:/iotest","writer1.txt"); FileOutputStream output = new FileOutputStream(file2); BufferedOutputStream bOutput =new BufferedOutputStream(output); byte [] b=new byte[1024]; int bCount; while((bCount=bInput.read(b))>0){ bOutput.write(b, 0, bCount); } bOutput.flush(); bOutput.close(); bInput.close(); } //BitArrayInputStream BitArrayOutputStream public void BitStream()throws Exception{ byte [] binput =new byte[]{12,-1,127,-128,0}; ByteArrayInputStream input = new ByteArrayInputStream(binput); int i; while((i = input.read())!=-1){ System.out.println(i); } ByteArrayOutputStream output = new ByteArrayOutputStream(); output.write(new byte[]{67,97,1}); String strOut = output.toString(); System.out.println(strOut); } //CharArrayReader CharArrayWriter public void CharStream()throws Exception{ CharArrayReader reader =new CharArrayReader(new char[]{'a','b','c'}); int i; while((i=reader.read())!=-1){ System.out.println(i); } CharArrayWriter writer = new CharArrayWriter(); writer.write("上海"); char [] cbuf =new char[]{'a','b','c','A'}; writer.write(cbuf); File file = new File("E:/iotest/char.txt"); FileWriter fw = new FileWriter(file); writer.writeTo(fw); writer.flush(); fw.flush(); } //ObjectStream public void ObjectStream() throws Exception{ FileOutputStream fos = new FileOutputStream("E:/iotest/object.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeDouble(10.2); oos.writeFloat(16.323f); oos.writeChars("中国"); Student stu =new Student(1,"cwg",new Date()); oos.writeObject(stu); oos.flush(); oos.close(); FileInputStream fis =new FileInputStream("E:/iotest/object.txt"); ObjectInputStream ois = new ObjectInputStream(fis); System.out.println( ois.readDouble()); System.out.println( ois.readFloat()); System.out.println( ois.readChar()); System.out.println( ois.readChar()); Object obj = ois.readObject(); if(obj instanceof Student){ Student rstu =(Student)obj; System.out.println(rstu.getId()+"--"+rstu.getUserName()); } } public static void main(String[] args) throws Exception { IODemo demo = new IODemo(); //demo.FileReaderAndWriter1(); //demo.FileReaderAndWriter2(); //demo.FileStream(); //demo.BitStream(); //demo.CharStream(); //demo.ObjectStream(); }