java oop 第十章 IO

来源:互联网 发布:数据库使用表语句 编辑:程序博客网 时间:2024/06/03 09:20
package cn.happy.IO;import java.io.FileInputStream;public class 字节流 {public static void main(String[] args) throws Exception{// TODO Auto-generated method stubFileInputStream fis=new FileInputStream("E:\\S2226.txt");byte[] bytes=new byte[1024];int date=fis.read(bytes);while(date!=-1){String topString=new String(bytes,0,date);System.out.println(topString);date=fis.read(bytes);}}}

package cn.happy.IO;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;public class 字节流写入 {public static void main(String[] args) throws IOException {FileOutputStream fos=new FileOutputStream("E:\\sss6.txt");String word="hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh";byte[] bytes=word.getBytes();fos.write(bytes);fos.close();}}

------字符流package cn.happy.IO.b;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;public class 字符流读取 {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFileReader reader=new FileReader("E:\\S2226.txt");char[] chars=new char[1024];int date;while((date=reader.read(chars))!=-1){String top=new String(chars,0,date);System.out.println(top);}reader.close();}}

package cn.happy.IO.b;import java.io.FileWriter;import java.io.IOException;public class 字符流写入 {public static void main(String[] args) throws IOException {FileWriter fWriter=new FileWriter("E:\\S2226.txt");String word="升高的困扰姐咖啡奶粉 的看法那我开三年的 ";fWriter.write(word);fWriter.close();}}

-package cn.happy.IO.c;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.io.Reader;public class buffer {public static void main(String[] args) throws IOException {Reader reader=new FileReader("E:\\S2226.txt");BufferedReader bf=new BufferedReader(reader);String line;while((line=bf.readLine())!=null){System.out.println(line);}bf.close();reader.close();}}

package cn.happy.IO.c;import java.io.BufferedWriter;import java.io.FileWriter;import java.io.IOException;import java.io.Writer;public class buffereddd {public static void main(String[] args) throws IOException { Writer writer=new FileWriter("E:\\S226.txt"); String ss="wowowowowowwwwwwwwwwwwwwwwwwwwwwwwwww"; BufferedWriter bf=new BufferedWriter(writer); bf.write(ss); bf.close(); writer.close(); }}

----二进制package cn.happy.IO.d;import java.io.DataInput;import java.io.DataInputStream;import java.io.DataOutputStream;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 date {public static void main(String[] args) throws IOException {InputStream is=new FileInputStream("E:\\QQ图片20170414115705.jpg");         DataInputStream dis=new DataInputStream(is);                  OutputStream os=new FileOutputStream("F:\\S2222.jpg");         DataOutputStream dos=new DataOutputStream(os);                  byte[] bytes=new byte[1024];         int date;         while((date=dis.read(bytes))!=-1){         os.write(bytes, 0, date);         }         dos.close();         os.close();         dis.close();         is.close();         System.out.println("成功");}}

序列化---------------package cn.happy.IO.e;import java.io.Serializable;public class Student implements Serializable{private String name;private 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 Student(String name, int age) {super();this.name = name;this.age = age;}public Student() {super();// TODO Auto-generated constructor stub}}

package cn.happy.IO.e;import java.io.DataOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.util.ArrayList;import java.util.List;public class Test {public static void main(String[] args) throws Exception {List<Student> list=new ArrayList<Student>();Student stu=new Student("ddd",1);Student stu1=new Student("eee",2);list.add(stu);list.add(stu1);OutputStream osOutputStream=new FileOutputStream("E:\\S226.txt");ObjectOutputStream dos=new ObjectOutputStream(osOutputStream);dos.writeObject(list);dos.close();osOutputStream.close();System.out.println("成功");}}

0 1
原创粉丝点击