day12_02 IO流

来源:互联网 发布:上海证券下载什么软件 编辑:程序博客网 时间:2024/05/16 15:08

流(Stream):指一组有顺序的、有起点和终点的字节集合,是对数据传输的总称或抽象

即数据在两个对象之间的传输称为流

数据输入:数据由外部输入设备流向内存。——输入流

数据输出:数据由内存流向外部设备。——输出

流的基本操作:读操作和写操作。

读操作:从流中取得数据的操作。对输入流只能进行读操作

写操作:向流中添加数据的操作。对输出流只能进行写操作

一个流只能进行读或写中一种操作,不能同时读和写

在Java中流本身也是对象

可以看成从源头向目标搬运着内容的一种特殊对象


流的方向

输入流

输出流

处理数据单位

字节流

字符流

功能

节点流

包装流/处理流


字节流处理的数据单位是字节 byte (8bit) 0~255 整型数据。

字符流处理的数据单位是Unicode字符(16bit) 0~65535,

可以表示世界上大部分语言符号。


节点流(Node)是可以从特定的节点读/写 数据,比如:文件,内存,网络,命令行界面等。节点流是底层基础的流,不能缺少

包装流(Wrapper)是指“包装”在已存在的流上,提供一些额外扩展的数据处理功能。包装流必须在节点流的基础上不能单独运行

字节输入流(InputStream):

继承自InputStream (抽象类)的类是用于输入数据,单位为字节(8bit)。


文件字节流(FileInputStream\FileOutputStream):

在对文件进行读/写操作中的数据传输功能,由文件字节输入/ 输出流类FileInputStream和FileOnputStream类实现,其数据源都是文

FileInputStream\FileOutputStream:在处理文件的读取和写入时,按照字节的方式进行处理,一般用于处理二进制数据

package com.testIo;import java.io.File;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 TestInputStream {public static void main(String[] args) throws IOException {File f = new File("E:\\vedio\\test\\demo.txt");InputStream in=new FileInputStream(f);//byte[] bytes=new byte[1024];byte[] bytes=new byte[10];int len=0;while((len=in.read(bytes, 0, bytes.length))!=-1){//System.out.println(new String(bytes,0,len));}/*int len=in.read(bytes, 0, (int)f.length());System.out.println();只能读一次System.out.println(new String(bytes,0,len)+"=======");*///System.out.println("-----------------");readFile();System.out.println("-----------------");readFile1();System.out.println("-----------------");readFile2();System.out.println("-----------------");readFile3();System.out.println("-----------------");System.out.println("-----------------");//writeFile1();System.out.println("-----------------");//writeFile2();writeFile3();}static void readFile() throws IOException {File f = new File("E:\\vedio\\test\\demo.txt");InputStream in=new FileInputStream(f);int read=0;//记录读取了几个字节byte[] bytes=new byte[10];//不等于-1 ,开始循环while((read=in.read(bytes))!= -1) {System.out.println(new String(bytes));}}static void readFile1() throws IOException{File f = new File("E:\\vedio\\test\\demo.txt");InputStream in  = new FileInputStream(f);byte[] bytes = new byte[10];int len = 0;//记录读取了几个字节while((len=in.read(bytes, 0, bytes.length)) != -1){System.out.println(new String(bytes,0,len));}in.close();}static void readFile2() throws IOException{File f = new File("E:\\vedio\\test\\demo.txt");InputStream in  = new FileInputStream(f);byte[] bytes=new byte[1024];//int len = 0;//记录读取了几个字节//while((in.read(bytes, 0, bytes.length))!=-1)while((in.read(bytes, 0, bytes.length))!=-1){System.out.println(new String(bytes));}in.close();}static void readFile3() throws IOException{File f = new File("E:\\vedio\\test\\demo.txt");InputStream in  = new FileInputStream(f);int read=in.read();//从此输入流中读取一个数据字节 给 readwhile(read!= -1){System.out.println((char)read);//不加char打印是数字 read = in.read();//不加这个 死循环}in.close();}static void writeFile1() throws IOException{File f = new File("E:\\vedio\\test\\demo1.txt");if(! f.exists()){f.createNewFile();}String s = "发电公司";byte[] bytes = s.getBytes();//getBytes(); 默认字符集将此 String 编码为 byte 序列OutputStream  os  = new FileOutputStream(f);for(int i=0;i<bytes.length;i++){os.write(bytes[i]);}os.close();}static void writeFile2() throws IOException{File f = new File("E:\\vedio\\test\\demo1.txt");if(!f.exists()){f.createNewFile();}String s = "dgadsfgfdsgsd";
byte[] bytes = s.getBytes();OutputStream  os  = new FileOutputStream(f);os.write(bytes);os.close();}static void writeFile3() throws IOException{File f = new File("E:\\vedio\\test\\demo1.txt");if(!f.exists()){f.createNewFile();}String s = "的开门vfakmkdvk
package com.stringclaszz;public class TestString {public static void main(String[] args) {String s="nflandslakjvnajnv";for (int i = 0; i < 10; i++) {s += i + "-";//为了加-}//打印sSystem.out.println(s);//substring 从指定索引处的字符开始,直到此字符串末尾。System.out.println(s.substring(0,s.length()-1));System.out.println("----------截取-------------");String subStr=s.substring(3, 5);//[3,5) System.out.println(subStr);System.out.println("-----------------------");testIndexOf(); // //字符串第一次后面一个字符  到结尾 substring();System.out.println("-----------------------");testSplit();System.out.println();System.out.println("-----------------------");testPool();// false}static void testIndexOf(){String s = "qwweerr";// e出现了几次  clazzz里有 数组的版本int index =s.indexOf("e");//返回指定字符在此字符串中第一次出现处的索引。//System.out.println(index);s=s.substring(index+1, s.length()); //字符串第一次后面一个字符  到结尾System.out.println(s);}/** * 例如,字符串 "boo:and:foo" 使用这些表达式可生成以下结果:  *  * Regex 结果  * : { "boo", "and", "foo" }  * o { "b", "", ":and:f" }  *  */static void testSplit(){String s = "A-B-C-D-F-G-H";String[] arr = s.split("-"); // split  根据给定正则表达式的匹配拆分此字符串。for(String ss :arr){System.out.print(ss+" ");}}static void testPool(){/*String s1 = "abc";String s2 = "abc";*/String s1 = new String("abc");String s2 = new String("abc");System.out.println(s1 == s2);//false}}

package com.stringclaszz;public class Testclazzz {public static void main(String[] args) {String s="hello";String s2=new String("world");char[] c= {'j','a','v','a','s','e'};String s3=new String(c);String s4=new String(c,0,4);System.out.println(s);System.out.println(s2);System.out.println(s3);System.out.println(s4);System.out.println("--------------------");testIndexOf();System.out.println("--------------------");pool();System.out.println("--------------------");pingJie();System.out.println("--------------------");pingJie2();}static void testIndexOf(){//String s="eeeebkdj";//String[] s1= new String[s];int index = 0;String [] ss = {"e","e","e","e","b","k","d","j"};String st="e";for (int i = 0; i < ss.length; i++) {if(ss[i]==st) index++;}System.out.println(index);}static void pool() {// TODO Auto-generated method stubString s="hello";String s2=new String("hello");String s3="hello";System.out.println(s==s2);System.out.println(s.equals(s2));System.out.println(s==s3);}static void pingJie() {String s="aishiteru";//int[] arr=new int[10];for (int i = 0; i < 10; i++) {s+=i; //10个对象  String}System.out.println(s);  }static void pingJie2() {StringBuilder s=new StringBuilder();StringBuilder s2=new StringBuilder("hello");s2.append("world");System.out.println(s2);s2.insert(s2.length(), "mememda");System.out.println(s2);}}


public class TestBufferStream {public static void main(String[] args) throws IOException {// readData1();writerData2();}static void readData1() throws IOException{File f = new File("E:\\vedio\\test\\demo.txt");BufferedReader br = new BufferedReader(new  FileReader(f));String  s= br.readLine();// 读取一个文本行while(s !=null){System.out.println(s);s = br.readLine();//不加就是死循环}br.close();}static void writerData2() throws IOException{File f = new File("E:\\vedio\\test\\demo1.txt");BufferedWriter bw = new BufferedWriter(new FileWriter(f,true));String s = "的sjavaiqiyi\n";bw.write(s);bw.flush();bw.close();}}

";byte[] bytes = s.getBytes();OutputStream os = new FileOutputStream(f);os.write(bytes,0,10);// 10是一个限制//此输入流中将最多 len 个字节的数据读入一个 byte 数组中os.close();}}





原创粉丝点击