JAVA IO
1. FileInputStream FileOutputStream通过字节流来读写文件
[java] view plaincopy
- 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 带有缓存的读写字节流
[java] view plaincopy
- 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 可以读写基本数据类型的字节流
[java] view plaincopy
- 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());
- }
- }
- }<strong>
- </strong>
2. PrintStream 将内存中的数据经过相应的转换后再输出
[java] view plaincopy
- 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);
- }