1 @Test 2 public void testInputStream2(){ 3 File file = new File("helloworld.txt"); 4 5 FileInputStream fis= null; 6 try { 7 fis = new FileInputStream(file); 8 9 byte[] b = new byte[5];//读取的数据要写入的数组 10 int len;//每次读取到byte中的字节的长度 11 while((len = fis.read(b))!=-1){ 12 for(int i = 0; i < len; i++){ 13 System.out.print((char)b[i]); 14 } 15 } 16 17 } catch (IOException e) { 18 // TODO Auto-generated catch block 19 e.printStackTrace(); 20 }finally{ 21 if(fis!=null){ 22 try { 23 fis.close(); 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 } 29 } 30 }
public void testOutputStream(){ //1 创建一个File对象,表明要写入的文件的位置 File file = new File("hello.txt"); //2 创建FileOutputStream对象 将file传入其构造器中 FileOutputStream fos=null; try { fos = new FileOutputStream(file); //3 写入操作 fos.write(new String("A smile is a curve that sets everything straight.").getBytes()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ //关闭输出流 if(fos!=null){ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public void testFileInputOutputStream(){ //提供写入写出的文件 File file1 = new File("hello.txt"); File file2 =new File("hello1.txt"); //提供相应的流 FileInputStream fis = null; FileOutputStream fos = null; try{ fis = new FileInputStream(file1); fos = new FileOutputStream(file2); //实现文件的复制 byte[] b = new byte[20]; int len; while((len = fis.read(b))!=-1){ // fos.write(b); // fos.write(b,0,b.length);此两种写法是错误的 fos.write(b, 0, len); } }catch(Exception e){ e.printStackTrace(); }finally{ //关闭相应的流 if(fos !=null){ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fis !=null){ try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
//使用字符流:FileReader FileWriter可以实现文本文件的复制 //非文本文件,使用字节流 @Test public void testFileReaderFileWriter(){ //输入流对应的文件一定要存在,否则抛异常,输出流对应的文件可以不存在,执行过程中会自动创建 FileReader fr = null; FileWriter fw = null; try{ File src = new File("dbcp.txt"); File desc = new File("dbcp1.txt"); fr =new FileReader(src); fw =new FileWriter(desc); char[] c = new char[24]; int len; while((len = fr.read(c))!=-1){ fw.write(c, 0, len); } }catch(Exception e){ e.printStackTrace(); }finally{ if(fw != null){ try { fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(fr != null){ try { fr.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } @Test public void testFileReader() { File file = new File("dbcp.txt"); FileReader fd = null; try { fd = new FileReader(file); char[] c = new char[24]; int len; while ((len = fd.read(c)) != -1) { String str = new String(c, 0, len); System.out.print(str); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fd != null) { try { fd.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
public class TestOtherStream { /** * 数据流:为了方便地操作Java语言的基本数据类型的数据,可以使用数据流。 * DataInputStream * DataOutputStream */ @Test public void test3(){ DataOutputStream dos = null; try { //创建连接到指定文件的数据输出流对象 dos = new DataOutputStream(new FileOutputStream("E:\\io\\io1\\io2\\data.txt")); dos.writeUTF("ab中国"); //写UTF字符串 dos.writeBoolean(false); //写入布尔值 dos.writeLong(1234567890L); //写入长整数 System.out.println("写文件成功!"); } catch (IOException e) { e.printStackTrace(); } finally { //关闭流对象 try { dos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 标准的输入输出流: * System.in * System.out */ @Test public void test2(){ BufferedReader br= null; try { InputStream is = System.in; InputStreamReader isr = new InputStreamReader(is); br = new BufferedReader(isr); String str; while(true){ System.out.println("请输入字符: "); str = br.readLine(); if(str.equalsIgnoreCase("e")||str.equalsIgnoreCase("exit")){ break; } String str1 = str.toUpperCase(); System.out.println(str1); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } /** * 转换流: * InputStreamReader * OutputStreamWriter */ @Test public void test1() { BufferedReader br = null; BufferedWriter bw = null; try { FileInputStream fis = new FileInputStream("dbcp.txt"); FileOutputStream fos = new FileOutputStream("dbcp3.txt"); InputStreamReader isr = new InputStreamReader(fis, "GBK"); OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK"); br = new BufferedReader(isr); bw = new BufferedWriter(osw); String str = null; while ((str = br.readLine()) != null) { bw.write(str); bw.newLine(); bw.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (br != null) { try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }
五.对象流:
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.io.Serializable; import org.junit.Test; /** * 对象的序列化机制 * @author Administrator * */ public class TestObjectInputOutputStream { //对象的反序列化过程:将硬盘的文件通过ObectInputStream转换为相应的对象 @Test public void testInputStream(){ ObjectInputStream ois = null; try { ois = new ObjectInputStream(new FileInputStream("Person.txt")); Person p1 = (Person)ois.readObject(); System.out.println(p1); Person p2 = (Person)ois.readObject(); System.out.println(p2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException 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(); } } } } @Test public void testOutputStream(){ //对象的序列化过程 将内存中的对象通过ObjectOutputStream转换为二进制流,存储在硬盘中 Person p1 = new Person("AA",20); Person p2 = new Person("BB",22); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new FileOutputStream("Person.txt")); oos.writeObject(p1); oos.flush(); oos.writeObject(p2); oos.flush(); } 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(); } } } } } //要实现序列化的类 要求: //1.此类可实现,必须实现Serializable和Externalizable两个接口之一2.类的属性同样也要实现Serializable和Externalizable两个接口之一 //3.提供版本号:private static final long serialVersionUID;4.ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量 class Person implements Serializable{ static String name; transient Integer age; private static final long serialVersionUID = 54213545221L; public Person(String name,Integer age){ this.name = name; this.age = age; } @Override public String toString() { return "Person [name=" + name + ", age=" + age + "]"; } }
原文:http://www.cnblogs.com/cj0086/p/5398777.html