对IO学习的总结
来源:互联网 发布:网络李逵劈鱼技巧 编辑:程序博客网 时间:2024/05/22 00:43
IO是用来干什么的?
主要涉及文件、网络数据流、内存缓冲等的输入和输出。
经常搞混的概念:
字节:byte 占八位 1byte=8bit
字符:charset 占两个字节 1char=2byte; http://blog.csdn.net/ocean20/article/details/6743385
文件的写入和读出,字节:
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Scanner;public class FileDemo {public static void main(String[] args) {// TODO Auto-generated method stub//创建文件File file=new File("d:"+File.separator+"test.txt");if(file.exists()){file.delete();}else {try{file.createNewFile();}catch(IOException e){e.printStackTrace();}}//输出,写入文件OutputStream out=null;try{out=new FileOutputStream(file);}catch(FileNotFoundException e){e.printStackTrace();}String info="hello";byte[] b0=info.getBytes();try{out.write(b0);}catch(IOException e){e.printStackTrace();}try{out.close();}catch(IOException e){e.printStackTrace();}Scanner sin=new Scanner(System.in);sin.nextLine();//写入,从文件里面得到内容InputStream in=null;//输入流try{in=new FileInputStream(file);//接受file里面的信息,获取字节流输入对象}catch(FileNotFoundException e){e.printStackTrace();}int len=0;//输入数组的长度,也是读取的长度byte[] b=new byte[1024];//1024b=1ktry{len=in.read(b);}catch(IOException e){e.printStackTrace();}try{in.close();}catch(IOException e){e.printStackTrace();}//将字节变成字符串然后输出System.out.println(new String(b,0,len));}}文件读取,字符:
import java.io.*;public class FileReaders {public static void main(String[] args) {// TODO Auto-generated method stubFile file=new File("D:"+File.separator+"test.txt");FileReader read=null;//字符输入流try{read=new FileReader(file);}catch(IOException e){e.printStackTrace();}String len=null;BufferedReader bufferReader=new BufferedReader(read);try{len=bufferReader.readLine();}catch(IOException e){e.printStackTrace();}finally{try{read.close();}catch(IOException e){e.printStackTrace();}}System.out.println(len);}}对象的序列化:
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable;class Person implements Serializable{String name;int age;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}}public class SerializePerson {public static void main(String[] args) {// TODO Auto-generated method stubSerialize();Deserialize();}public static void Deserialize(){ObjectInputStream ois=null;try{ois=new ObjectInputStream(new FileInputStream(new File("e:/person.txt")));}catch(IOException e){e.printStackTrace();}Person p=null;try{p=(Person)ois.readObject();}catch(ClassNotFoundException e){e.printStackTrace();}catch(IOException e){e.printStackTrace();}System.out.println(p.name+" "+p.age);}public static void Serialize(){Person p =new Person();p.setAge(22);p.setName("Dustin");ObjectOutputStream oo=null;try{oo=new ObjectOutputStream(new FileOutputStream(new File("e:/person.txt")));}catch(IOException e){e.printStackTrace();}try{oo.writeObject(p);}catch(IOException e){e.printStackTrace();}System.out.println("Person对象序列化成功");try{oo.close();}catch(IOException e){e.printStackTrace();}}}内存流:
内存操作流现在在Java SE阶段是感觉不出有什么作用,但是在学习Java WEB 中的AJAX技术的时候,会结合XML解析和JavaScript、AJAX完成一些动态效果的使用
管道流:
用于线程之间的通信
import java.io.*;class Send implements Runnable{private PipedOutputStream out=null;public Send(){this.out=new PipedOutputStream();}public PipedOutputStream getPipedOutputStream(){return this.out;}@Overridepublic void run() {// TODO Auto-generated method stubString str="hello world";try{this.out.write(str.getBytes());}catch(IOException e){e.printStackTrace();}try{out.close();}catch(IOException e){e.printStackTrace();}}}class Receive implements Runnable{private PipedInputStream in=null;public Receive(){this.in=new PipedInputStream();}public PipedInputStream getPipedInputStream(){return in;}@Overridepublic void run() {// TODO Auto-generated method stubbyte b[]=new byte[1024];int len=0;//BufferedReader read=new BufferedReader(new InputStreamReader(in));//try{//System.out.println(read.readLine());//}catch(IOException e){//e.printStackTrace();//}//try{//read.close();//}//catch(IOException e){//e.printStackTrace();//}//读出数据try{len=in.read(b);}catch(IOException e){e.printStackTrace();}try{in.close();}catch(IOException e){e.printStackTrace();}System.out.println(new String(b,0,len));}}public class PipedInputDemo{public static void main(String[] args){Send send=new Send();Receive receive=new Receive();try{send.getPipedOutputStream().connect(receive.getPipedInputStream());}catch(IOException e){e.printStackTrace();}new Thread(send).start();new Thread(receive).start();}}
0 0
- 对IO学习的总结
- 对io流学习的简单总结
- 大神对IO的总结
- 黑马程序员-Java IO的学习总结
- IO流的学习总结2
- Java中IO流的学习总结
- java IO体系的学习总结
- 对进程的学习总结
- 对Hibernate的学习总结
- 对Windows学习的总结
- 对c++的学习总结
- 黑马程序员——对IO流的总结
- 对与java io知识的部分总结
- Java io学习总结
- IO流学习总结
- java.io学习总结
- JAVA IO 学习总结
- io学习小总结
- CSerialPort多线程串口编程工具详解
- MySQL 视图操作
- 【Day35】浅谈PHP拦截器之__set()与__get()的理解与使用方法
- NAT简介及穿透
- Console命令详解
- 对IO学习的总结
- IOS手势处理的那些坑
- MQTT的通配符
- 学校在线二手交易平台-服务器模块(本科生毕业设计)
- java中的IO操作总结(一
- usaco Palindromic Squares
- mysqli_result 类中的成员函数和属性
- win64 python2.7 numpy 安装
- ext4 学习笔记一,[自定义类](白鹤翔第一季)