IO中的字节流练习
来源:互联网 发布:jsp中引入java包 编辑:程序博客网 时间:2024/05/16 16:22
1. FileInputStream FileOutputStream通过字节流来读写文件
public static void main(String[] args) throws Exception {//将文件里写数据File f = new File("d:\\dmeo.txt");FileOutputStream output = new FileOutputStream(f);String s = "I Am Learning Java , 我在学习Java";byte[] b = s.getBytes(); //将String变为byte数组//output.write(b);write可以接收byte数组for (int i = 0; i < b.length; i++) {output.write(b[i]);}output.flush();output.close();//读取文件里数据FileInputStream input = new FileInputStream(f);//开辟一个文件大小的数组空间byte[] in = new byte[(int) f.length()];//将读取来的字节放入数组中for (int i = 0; i < in.length; i++) {in[i] = (byte) input.read(); }System.out.print(new String(in));input.close();}
2. BufferedInputStream BufferedOutPutStream 带有缓存的读写字节流
public static void main(String[] args) throws Exception {//将文件里写数据File f = new File("d:\\dmeo.txt");BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(f));String s = "I Am Learning Java , 我在学习Java";byte[] b = s.getBytes();output.write(b); //默认会有512字节的缓存,当缓存存满时一次性向文件中写入output.flush(); output.close();BufferedInputStream input = new BufferedInputStream(new FileInputStream(f));byte[] in = new byte[(int)f.length()];for (int i = 0; i < in.length; i++) {in[i] = (byte)input.read();}System.out.println(new String(in));input.close(); }
2. DataInputStream DataOutputStream 可以读写基本数据类型的字节流
import java.io.*;//一个简单的人员类,只有姓名和年龄public class Person {private String name;private int age;public Person(String name ,int age){this.name = name;this.age = age;}public String getName(){return this.name;}public int getAge() {return this.age;}public static void main(String[] args) {//构造对象数组Person[] p1 = {new Person("Ryan",20),new Person("Tom",30),new Person("Jerry",15)};File f = new File("d:\\demo\\demo.txt");try {DataOutputStream output = new DataOutputStream(new FileOutputStream(f));//写入数据for (int i = 0; i < p1.length; i++) {output.writeUTF(p1[i].getName()); //以UTF编码写入姓名output.writeInt(p1[i].getAge()); //以Int型写入年龄}output.flush();output.close();} catch (Exception e) {e.printStackTrace();}Person[] p2 = new Person[p1.length];try {//读出数据DataInputStream input = new DataInputStream(new FileInputStream(f));for (int i = 0; i < p1.length; i++) {String name = input.readUTF(); //读出姓名int age = input.readInt(); //读出年龄//重新构造person对象p2[i] = new Person(name,age);} input.close();} catch (Exception e) {e.printStackTrace();}//检查是否还原正确for (int i = 0; i < p2.length; i++) {System.out.println("姓名:" + p2[i].getName() + " ;年龄:" + p2[i].getAge());}}}
public static void main(String[] args){File f = new File("d:\\dmeo.txt");String studentName = "TOM";int age = 20;double totalScore = 600.0f;PrintStream ps = null;try {ps = new PrintStream(new FileOutputStream(f));} catch (FileNotFoundException e) {e.printStackTrace();}//格式化后输出ps.printf("姓名:%s , 年龄:%d , 总分:%3.1f",studentName,age,totalScore);}
- IO中的字节流练习
- IO之字节流练习
- io流中的字节流
- IO中的字符流练习
- io流中的字节和字符
- Java基础之-IO中的字节流
- java:java中的字节流的练习
- io流中的输入,读取文件中的字节
- 21-IO流-22-IO流(字节流-练习-复制MP3)
- 24-IO流-70-IO流(练习-按字节截取字符串)
- IO练习--按字节截取字符串
- 黑马程序员java基础之IO流中的字节流
- java中的IO流之字节流详解(二)
- Java学习笔记之IO流中的文件字节流
- IO流(字节流)
- IO流字节流
- IO流字节流
- IO流-字节流
- Inno Setup入门(十五)——Inno Setup类参考(1)
- Inno Setup入门(十六)——Inno Setup类参考(2)
- WIN7添加TELNET
- Inno Setup入门(十七)——Inno Setup类参考(3)
- jbpm4.4 eclipse 安装
- IO中的字节流练习
- windows Mobile 软件开发入门小结
- Inno Setup入门(十八)——Inno Setup类参考(4)
- 用数据发生器产生海量的数据
- Inno Setup入门(十九)——Inno Setup类参考(5)
- 90%的人正在面对的9个职场错误,你有吗?
- Inno Setup入门(二十)——Inno Setup类参考(6)
- 学习设计模式--观察者模式(C++)
- Inno Setup入门(二十一)——Inno Setup类参考(7)