java IO

流的介绍
1. 可以向其读入一个字节序列的对象叫输入流
2. 可以向其写入一个字节序列的对象叫输出流
3. 流是单向的
4. 节点流(低级流),可以从或者向一个特定的节点读写数据
5. 处理流(高级流),对一个已存在的流的处理和封装
6. InputStream是输入流的父类,Outputstream是输出流的父类
7. 流的来源和目的,可以是文件,网络连接,内存块
8. 流的读写都是阻塞的
9. 流的操作都要catch异常


 

字节流 

 

 1 /*
 2 * FileOutputStream(file,isAdd) 
 3 * 文件不存在自动创建文件,默认全部覆盖重新写,第二个参数是true就是追加 \r\n 是windows的换行
 4 */
 5 File file = new File("."+File.separator + "demo.txt");
 6 FileOutputStream fos = new FileOutputStream(file);
 7 String str = "hello /n哈哈哈";
 8 byte[] data = str.getBytes("utf-8");
 9 fos.write(data);
10 fos.close();
11 
12 /*
13 * FileInputStream(file) 
14 * 从文件读内容
15 */
16 File file = new File("."+File.separator + "debug" + ".txt");
17 FileInputStream fis = new FileInputStream(file);
18 byte[] data = new byte[100];
19 int len = fis.read(data);
20 String str = new String(data,0, len, "utf-8");
21 
22 /*
23 * @note buffered 缓冲流 高级流
24 * @note 缓冲流快,也是读一个byte数组,缓冲流内部有一个缓冲区,当read时,会读取一组数据,然后一个一个返* 回 ,直到全部返回之后,才会重新读取数据  
* * @demo 下面为复制一个文件的demo
25 */ 26 FileInputStream fis = new FileInputStream("."+File.separator + "demo.txt"); 27 BufferedInputStream bis = new BufferedInputStream(fis); 28 FileOutputStream fos = new FileOutputStream("demo_cp.txt"); 29 BufferedOutputStream bos = new BufferedOutputStream(fos); 30 int d = -1; 31 while( ( d = bis.read() ) != -1) { 32 System.out.println(d); 33 bos.write(d); 34 } 35 System.out.println("复制完毕"); 36 bis.close(); 37 bos.close(); 38 39 /* 40 * @title 缓冲写出流的问题,内容少的时候不会写出去 41 * @analysis 关注即时性,调flush,关注性能,少用flush 42 */ 43 FileOutputStream fos = new FileOutputStream("demo_cp.txt"); 44 BufferedOutputStream bos = new BufferedOutputStream(fos); 45 bos.write("hehe".getBytes("utf-8")); 46 bos.flush(); //把缓冲区的字节强制写出去 47 bos.close();// 调用flush了

 

 

对象流

对象持久化,方便的读写java的对象

 

/*
* @title 把一些基本的数据或者内置的数据写入文件 
*/
FileOutputStream ostream = new FileOutputStream("t.tmp");
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeInt(12345);
p.writeObject("Today");
p.writeObject(new Date());
p.flush();
ostream.close();
/*
*@title 自定义的类写入文件 ,需要实现Serializable接口 
*/
public class Person implements Serializable{ }
Person p = new Person();
p.setName("feifei");
p.setAge(22);
p.setAddress("ShanghaiDaxue");
List<String> other = new ArrayList<String>();
other.add("is a gril");
other.add("is college");
p.setOther(other);

FileOutputStream fos = new FileOutputStream("t.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
oos.close();

// 从文件转化为对象

FileInputStream fis = new FileInputStream("t.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Person p = (Person)ois.readObject();
System.out.println(p);

// transient修饰属性,不被序列化存储在文件
private transient int age;


//版本号,默认根据文件内容生成
//如果版本号不一改,反序列化就报错 
//版本号一样,内容不一样,也可以反序列化成功,启用兼容模式,能用的就用,不用的丢弃 

private static final long serialVersionUID = 2l;

 


 

字符流 Reader,writer 

1. Reader是字符输入流的父类,Writer是字符输出流的父类
2. jave内部是用unicode编码存东西,2个字节表示,java中的一个char的确是2个字节,windows系统默认是gbk编码
3. read和write方法,返回的是低16位


转换流(OutputStreamWriter ,InputStreamReader)
可以把低级流的字节流 转换 为字符流,然后其他流就可以处理字符流了

OutputStreamWriter 

FileOutputStream fos = new FileOutputStream("osw.txt");
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("hello 2017","utf-8"); //这里可以指定字符编码 经常用 

System.out.println("success");
osw.close();

 

InputStreamReader

 

FileInputStream fis = new FileInputStream("osw.txt");
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
int d = -1;
while( (d = isr.read()) != -1) {
    System.out.println((char)d  );//一个字符一个字符的输出 
}
isr.close();

缓冲字符流 BufferedWriter,BufferedReader
可以按行读取字符串 

BufferedReader

/*
*@description 按行输出文件的内容
*/

FileInputStream fis = new FileInputStream("src/com/io/Person.java");

InputStreamReader isr = new InputStreamReader(fis);

BufferedReader br = new BufferedReader(isr);

String line = null;

while(( line = br.readLine() ) != null) {
    System.out.println(line);
}

br.close();

 


PrintWriter
自动刷新的缓冲字符输出流,创建了pw,会自动创建一个 BufferedWriter 作为缓冲叠加,也自动创建了FileOutputStream低级流

/*
* @descfription 基本使用
*/

PrintWriter pw = new PrintWriter("pw.txt");
pw.println("床前明月光");
pw.println("红杏出墙来");
pw.close();

/*
*@descfription作为高级流处理低级流,第二个参数是true,实现自动刷新
*/
Scanner sc= new Scanner(System.in);
String str;
String fileName;
System.out.println("请输入文件名");
fileName = sc.nextLine();
fileName+=".txt";
File file = new File(fileName);
file.createNewFile();
System.out.println("请输入内容");
while(true) {
    str = sc.nextLine();
    if("exit".equals(str)) {
        break;
    }
    write(file,str);
}
sc.close();
System.out.println("app over");
public static void write(File file, String str)  {
    FileOutputStream fos = new FileOutputStream(file,true);
    OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8");
    PrintWriter pw = new PrintWriter(osw,true);//自动行刷新 
    pw.println(str);
    pw.close();
}

 

posted @ 2017-10-25 14:35  StarBugs  阅读(205)  评论(0编辑  收藏  举报