字节流的文件读取和字符流的文件读取基本上是一摸一样的。。。
缓冲区的操作是在内存中操作的。。。把硬盘数据弄到缓冲区中,然后从缓冲区里面取就可以。
字节流的输入输出
/*异常的三种抛出方式,一种是用捕获异常的方式,在本函数抛出,一种是用间接法调用函数,中间那个函数要捕获,最后那个函数要抛出,抛出给中间的函数捕获,一种是本函数直接抛出异常,但是他的所有父函数都要抛出异常知道遇到捕获或者遇到主函数,由主函数抛给操作系统。。。字节流的文件读取和字符流的文件读取基本上是一摸一样的。。。缓冲区的操作是在内存中操作的。。。把硬盘数据弄到缓冲区中,然后从缓冲区里面取就可以。*/import java.io.*;public class anli{ public static void main (String[] args)throws IOException//第三种方法的抛出异常 { //fileoutputstream(); //普通的输入 //filearray(); //按照字节数组的输入 long beggin = System.currentTimeMillis(); //获取当前时间的方法,时间要用long来存放,这样子才能减少溢出的情况发生 availabledemo(); //字节流特有的获取文件长度方法 long end = System.currentTimeMillis(); sop(end-beggin); } //这种方式会读取数据更快,但是可能有内存溢出的危险。 public static void availabledemo()throws IOException { FileInputStream fi = new FileInputStream ("anli.java"); int number = fi.available(); //available是字节流特有的操作,可以得出文件的长度,一次性读取数据而不需要用while循环。 //但是available很容易就发生内存溢出。。。虚拟机启动了,默认是64兆的 byte[] bt = new byte[number]; int length = 0; while ((length = fi.read(bt))!=-1) { sop(new String(bt,0,length)); } } public static void filearray()throws IOException { FileInputStream fi = new FileInputStream ("anli.java"); byte[] bt = new byte[1024]; //使用的是字节数组 int length = 0; //返回个数要考虑换行符,换行符占据两个字节。。。\r\n while ((length = fi.read(bt))!=-1) //除了读取行的是返回null,其他的一般都是返回-1 { sop(new String(bt,0,length)); //String构造器的使用 } } public static void fileoutputstream()throws IOException { FileOutputStream fs = new FileOutputStream ("demo.txt",true); fs.write("aafgg".getBytes()); //字节流的输入方式唯一的区别就是,输入的参数是字节,这里调用的是String的getBytes的方法。。。 fs.close(); } public static void sop(Object obj) { System.out.println(obj); }}
字符流的输入输出
字符缓存流的输入输出
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { //要嘛就跑出异常,要嘛就用try对每个操作捕获异常 FileWriter fw = new FileWriter("demo.txt",true); //加上true,使文件不会被覆盖 FileReader fr = new FileReader("string.txt"); BufferedWriter bfw = new BufferedWriter(fw); BufferedReader bfr = new BufferedReader(fr); String str1 = null; while((str1 = bfr.readLine())!=null){ bfw.write(str1); bfw.newLine(); //要加上换行,因为readline()是不会读取换行的 bfw.flush(); } bfw.close(); bfr.close(); //第一种读取数据的方法,是一个一个读取的,效率不高,缓冲区是第二种读取数据的方法,效率高 FileReader fr1 = new FileReader("string.txt"); int term; while((term = fr1.read())!=-1){ print((char)term); //read的返回值一直都是整形 } fr1.close(); //第三种读取数据的方法 是通过数组的 FileReader fr2 = new FileReader("string.txt"); //对文件操作的流 char[] array = new char[1024]; //数据作为参数传递进去之后,数据就存放在数组里面 int term1; //返回值就是数据的长度 while((term1 = fr2.read(array))!=-1){ print(new String(array,0,term1)); } fr2.close(); FileReader fr3 = new FileReader("string.txt"); LineNumberReader lnr = new LineNumberReader(fr3); lnr.setLineNumber(6); String str3 = null; while((str3 = lnr.readLine())!=null){ print(lnr.getLineNumber()+"******"+str3); //在输出的时候对每一行都获取当前行数 System.out.println(); //readline()不获取换行,所以输出的数据都会在同一行上 } } static public void print(Object obj){ System.out.print(obj); }}
标准输入输出流(System.in System.out)的使用
当录入一行数据后就对该行数据打印
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { InputStream is = System.in; InputStreamReader isr = new InputStreamReader(is); //字节流通向字符流 BufferedReader bfr = new BufferedReader(isr); //记得这里是bufferedreader 这里很不理解 BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(System.out)); String str = null; while((str = bfr.readLine())!= null){ if("over".equals(str))break; bfw.write(str.toUpperCase()); bfw.newLine(); bfw.flush(); //print(str.toUpperCase()); } bfr.close(); } static public void print(Object obj){ System.out.println(obj); }}重新分配标准输入输出流
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { System.setIn(new FileInputStream("string.txt")); //重新分配标准输出设备 System.setOut(new PrintStream("demo.txt")); //重新分配标准输出设备 InputStream is = System.in; InputStreamReader isr = new InputStreamReader(is); //字节流通向字符流 BufferedReader bfr = new BufferedReader(isr); //记得这里是bufferedreader 这里很不理解 BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(System.out)); String str = null; while((str = bfr.readLine())!= null){ if("over".equals(str))break; bfw.write(str.toUpperCase()); bfw.newLine(); bfw.flush(); //print(str.toUpperCase()); } bfr.close(); } static public void print(Object obj){ System.out.println(obj); }}
把流中的数据加载到集合中,并把集合中的修改操作同步到流中
package hanshi.net;import java.util.*;import java.io.*;public class count { public static void main(String[] args)throws Exception { Properties prop = new Properties(); FileInputStream fis = new FileInputStream("string.txt"); prop.load(fis); //把流中的数据加载到集合中 prop.list(System.out); //把集合中的数据全部包装后输出到参数指定的位置 } static public void print(Object obj){ System.out.println(obj); }}对多个流进行合成 也可以用于文件或者视频等的合成和分割,可用于加密解密
package hanshi.net;import java.util.*;import java.io.*;public class count { public static void main(String[] args)throws Exception { Vector操作对象的对象流,保留对象的数据以便下次可以再次使用al = new Vector (); //合成的是vector集合里面所存放的集合 al.add(new FileInputStream(new File("string.txt"))); al.add(new FileInputStream(new File("demo.txt"))); File file = new File("file.txt"); if(!file.exists())file.createNewFile(); //如果指定的文件不存在则自己创建一个文件 FileOutputStream fi = new FileOutputStream(file); Enumeration em = al.elements(); //关键部分就只有这两行 SequenceInputStream sis = new SequenceInputStream(em); byte[] by = new byte[1024]; int len = 0; while((len = sis.read(by))!=-1){ fi.write(by,0,len); } fi.close(); sis.close(); } static public void print(Object obj){ System.out.println(obj); }}
package hanshi.net;import java.io.*;class Person implements Serializable{ //实现序列号接口 让对象变成了唯一性 int age; String name; static String sex = "nv"; //静态不参与到合成序列号 Person(int age,String name){ this.age = age; this.name = name; } public String toString(){ return (age+"***"+name+"***"+sex); }}public class count { public static void main(String[] args)throws Exception { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("string.txt")); oos.writeObject(new Person(12,"chenruibing")); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("string.txt")); Person person = (Person)ois.readObject(); //返回值一定是object,所以要强制类型转换 print(person); } static public void print(Object obj){ System.out.println(obj); }}操作数据的数据流
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { DataOutputStream dos = new DataOutputStream(new FileOutputStream("string.txt")); dos.writeUTF("您好"); dos.writeUTF("我也好"); dos.close(); DataInputStream dis = new DataInputStream(new FileInputStream("string.txt")); print(dis.readUTF()); print(dis.readUTF()); dis.close(); } static public void print(Object obj){ System.out.println(obj); }}
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("string.txt"),"UTF-8"); osw.write("我叫陈锐炳"); osw.close(); InputStreamReader isr = new InputStreamReader(new FileInputStream("string.txt"),"GBK"); char[] c = new char[10]; int len = 0; while((len = isr.read(c))!=-1){ print(new String(c,0,len)); } //print(isr.read()); isr.close(); } static public void print(Object obj){ System.out.println(obj); }}
PrintStream流
Date date = new Date();PrintStream ps = new PrintStream ("demo.txt");ps.println(date.toString()); //把内容写入ps指定的文件
流的小应用
当录入一行数据后就对该行数据打印(字节流的方式)
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { InputStream is = System.in; StringBuilder sb = new StringBuilder(); int term; while(true){ term = is.read(); if(term == '\t')continue; if(term =='\n'){ String s = sb.toString(); if("over".equals(s))break; print(s.toUpperCase()); sb.delete(0, sb.length()); } else{ sb.append((char)term); } } } static public void print(Object obj){ System.out.println(obj); }}当录入一行数据后就对该行数据打印
字节流转成字符缓存流的方式,可以使用整行读取的方式,这也是转变的原因,就是为了使用其他流字符流具有而字节流没有的功能
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { InputStream is = System.in; InputStreamReader isr = new InputStreamReader(is); //字节流通向字符流 BufferedReader bfr = new BufferedReader(isr); String str = null; while((str = bfr.readLine())!= null){ if("over".equals(str))break; print(str.toUpperCase()); } bfr.close(); } static public void print(Object obj){ System.out.println(obj); }}对指定目录进行所有文件和文件夹的遍历并且删除自己指定的文件
package hanshi.net;import java.io.*;public class count { public static void main(String[] args)throws Exception { File[] file = File.listRoots(); //列出可用的文件系统根 for(File f : file){ print(f); } File file1 = new File("H:\\"); showdir(file1); } public static void showdir(File f){ //遍历指定目录下的所有文件和目录 File[] files = f.listFiles(); for(int i=0; i