您的位置:首页 > 编程语言 > Java开发

JAVA020--IO流

2016-06-19 13:28 447 查看

IO流

按照方向分为输出输入流

按照处理数据的最小单位分为字符流和字节流

字节流

package com.lovo.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class TestByteStream {

public static void main(String[] args) {
// TODO Auto-generated method stub
//1、字节流
//声明流
InputStream in = null;
OutputStream out = null;
try {
//创建流
in = new FileInputStream("D:" + System.getProperty("file.separator") + "lileihanmeimei.mp3");
out = new FileOutputStream("E:" + System.getProperty("file.separator") + "j124.mp3");

//操作流
//          int b = 0;
//          while((b = in.read()) != -1){
//              out.write(b);
//          }

byte[] b = new byte[1024];
int length = 0;
while((length = in.read(b)) != -1){
out.write(b,0,length);
out.flush();//冲刷
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
//关闭流
if(in != null){
try {
in.close();
} catch (IOException e) {
//
4000
TODO Auto-generated catch block
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

}


字符流

package com.lovo.test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

import javax.swing.JOptionPane;

public class TestCharStream {

public static void main(String[] args) {
// TODO Auto-generated method stub
//字符流
//      String msg = JOptionPane.showInputDialog("请输入‘白日依山尽’的下一句:");
//
//      Writer w = null;
//
//      try {
//          w = new FileWriter("poem.txt");
//          char[] b = msg.toCharArray();
//          w.write(b);
//          w.flush();
//      } catch (IOException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      } finally{
//          if(w != null){
//              try {
//                  w.close();
//              } catch (IOException e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//              }
//          }
//      }

Reader r = null;
try {
String result = "";
r = new FileReader("poem.txt");
char[] c = new char[10];
int length = 0;
while((length = r.read(c)) != -1){
String str = new String(c, 0, length);
result += str;
}
System.out.println(result);

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(r != null){
try {
r.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}


文件流

package com.lovo.test;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TestFile {

public static void main(String[] args) {
// TODO Auto-generated method stub
//File是JDK中专门用来表示文件或文件夹的类
File f1 = new File("D:" + File.separator + "HelloWorld.class");
File f2 = new File("D:" + File.separator + "test14");
System.out.println(f2.isFile());//判断File对象是否是文件
System.out.println(f2.isDirectory());//判断File对象是否是文件夹

File f3 = new File("D:" + File.separator + "HelloWorld.txt");
System.out.println(f3.exists());//判断File对象代表的文件或文件夹是否存在
if(!f3.exists()){
try {
f3.createNewFile();//创建新文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
f3.delete();//删除文件或文件夹
}

File f4 = new File("D:" + File.separator + "J124");
if(!f4.exists()){
f4.mkdir();//创建新文件夹
}else{
f4.delete();//删除文件或文件夹
}

//探究文件
System.out.println(f1.getName());//得到文件名
System.out.println(f1.getAbsolutePath());//得到绝对路径
System.out.println(f1.isHidden());//判断是否是隐藏文件
System.out.println(f1.canExecute());//判断是否是可执行文件
System.out.println(f1.canRead());//判断是否是可读文件
System.out.println(f1.canWrite());//判断是否是可写文件
System.out.println(f1.length());//得到文件大小,单位字节
Date lm = new Date(f1.lastModified());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
System.out.println(sdf.format(lm));//得到上次修改时间

//探究文件夹
if(f2.isDirectory()){
//          String[] subFileNames = f2.list();//得到文件夹下面所有子文件和子文件夹的名字
//          for(String subFileName : subFileNames){
//              System.out.println(subFileName);
//          }

File[] subFiels = f2.listFiles();//得到文件夹下面所有子文件和子文件夹的File对象
for(File subFile : subFiels){
System.out.println(subFile.getAbsolutePath());
}
}

}

}


对象流

package com.lovo.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.lovo.bean.Student;

public class TestObject {

public static void main(String[] args) {
// TODO Auto-generated method stub
//对象的序列化--将对象以二进制的形式输出(没有确定输出到哪儿去)
//      ArrayList<Student> al = new ArrayList<Student>();
//      Student stu0 = new Student("HJ", 25,"四川","成都","九眼桥第三桥洞");
//      Student stu1 = new Student("YP", 27,"四川","成都","九眼桥第2桥洞");
//      al.add(stu0);
//      al.add(stu1);
//
//      ObjectOutputStream oos = null;
//
//      try {
//          oos = new ObjectOutputStream(new FileOutputStream("student.data"));
//          oos.writeObject(al);
//      } catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      } catch (IOException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }finally{
//          if(oos != null){
//              try {
//                  oos.close();
//              } catch (IOException e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//              }
//          }
//      }

//反序列化--将输入的二进制流转换为对象(同样不确定二进制流的数据源)
ArrayList<Student> al = null;
ObjectInputStream ois = null;

try {
//使用了装饰器模式
ois = new ObjectInputStream(new FileInputStream("student.data"));
al = (ArrayList<Student>)ois.readObject();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(ois != null){
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

System.out.println(al.get(0));
System.out.println(al.get(1));
}

}

a414

student代码部分

package com.lovo.bean;

import java.io.Serializable;

//Serializable接口是一个标示接口,相当于给类打上一个标志允许参与序列化和反序列化
public class Student implements Serializable{

private String name;

private int age;

private Address address;

public Student(){

}

public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}

public Student(String name, int age, String state,String city,String street) {
super();
this.name = name;
this.age = age;
this.address = new Address(state,city,street);
}

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;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return this.name + ":" + this.age + ":" + this.address;
}
}


address代码部分

package com.lovo.bean;

import java.io.Serializable;

public class Address implements Serializable{

private String state;

private String city;

private String street;

public Address(){

}

public Address(String state, String city, String street) {
super();
this.state = state;
this.city = city;
this.street = street;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public String getCity() {
return city;
}

public void setCity(String city) {
this.city = city;
}

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return this.state + "-" + this.city + "-" + this.street;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: