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=1k
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));
}
}
文件读取,字符:
import java.io.*;
public class FileReaders {
public static void main(String[] args) {
// TODO Auto-generated method stub
File 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 stub
Serialize();
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;
}
@Override
public void run() {
// TODO Auto-generated method stub
String 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;
}
@Override
public void run() {
// TODO Auto-generated method stub
byte 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();
}
}