四套读写方案 外加 序列化和反序列化

    xiaoxiao2021-04-17  43

    第一套: 用FileInputStream和FileOutputStream

    package cn.happy; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { /*File file=new File("D:/S2226.txt"); file.createNewFile(); FileInputStream fis=new FileInputStream("D:/S2226.txt"); byte[]bytes=new byte[1024]; int data = fis.read(bytes); while (data!=-1) { String str=new String(bytes, 0, data); System.out.println(str); data=fis.read(bytes); }*/ FileOutputStream fis2=new FileOutputStream("D:/S2226.txt"); String str2="我爱你宝贝儿!"; byte[]bytes2=str2.getBytes(); fis2.write(bytes2); fis2.close(); } } 第二套:用FileReader和FileWriter

    package cn.happy; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class Test2 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { /*FileReader reader=new FileReader("D:/S2226.txt"); char[]chars=new char[1024]; int data; while ((data=reader.read(chars))!=-1) { String str=new String(chars); System.out.println(str); } reader.close();*/ FileWriter writer=new FileWriter("D:/S2226.txt"); String words="I Love You !"; writer.write(words); System.out.println("OK"); writer.close(); } } 第三套:用BufferedReader和BufferedWriter

    package cn.happy; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; public class Test3 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { /*Reader reader=new FileReader("D:/S2226.txt"); BufferedReader br=new BufferedReader(reader); String str; while ((str=br.readLine())!=null) { System.out.println(str); } br.close(); reader.close();*/ Writer writer =new FileWriter("D:/S2226.txt"); BufferedWriter bw=new BufferedWriter(writer); String words="已经晚了!"; bw.write(words); bw.close(); writer.close(); } } 第四套:用InputStream和OutStream

    package cn.happy; 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 Test4 { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { InputStream is=new FileInputStream("C:/美女.jpg"); DataInputStream dis=new DataInputStream(is); byte[]bytes=new byte[1024]; OutputStream os=new FileOutputStream("D:/美女.jpg"); DataOutputStream dos=new DataOutputStream(os); int data; while ((data=dis.read(bytes))!=-1){ dos.write(bytes); } dos.close(); os.close(); dis.close(); is.close(); } } 序列化

    package cn.happy2; import java.io.DataOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.util.List; import java.util.ArrayList; public class Test { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { List<Student> list=new ArrayList<Student>(); Student stu1=new Student("刘振宇", 20); Student stu2=new Student("郭华", 20); list.add(stu1); list.add(stu2); OutputStream os=new FileOutputStream("D:/save.bin"); ObjectOutputStream oos=new ObjectOutputStream(os); oos.writeObject(list); oos.close(); os.close(); } }

    反序列化

    package cn.ten; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.List; public class day06 { /** * 反序列化 * @throws IOException * @throws ClassNotFoundException */ public static void main(String[] args) throws IOException, ClassNotFoundException{ InputStream is=new FileInputStream("E:/S2226.txt"); ObjectInputStream op=new ObjectInputStream(is); List<Student> list= (List<Student>) op.readObject(); for (Student student : list) { System.out.println(student.getAge()+student.getName()); } op.close(); is.close(); System.out.println("OK"); } }

    转载请注明原文地址: https://ju.6miu.com/read-674329.html

    最新回复(0)