转载请注明出处,谢谢!html
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p> <p align="center"><font color="#FF3030" size="5">写在前面</font></p> <p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>java
InputStream
OutputStream
Reader
Writer
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>windows
<p align="center"><font color="#FF3030" size="5">目 录</font></p>设计模式
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p> <br/>数组
<p align="center"><font face="仿宋" color="#004D88" size="4">1.read()一次读取一个字节</font></p>dom
<p align="center"><font face="仿宋" color="#004D88" size="4">2.read()方法返回值为何是int</font></p>ide
<p align="center"><font face="仿宋" color="#004D88" size="4">3.定义小数组实现缓冲</font></p>优化
<p align="center"><font face="仿宋" color="#004D88" size="4">5.flush方法和close方法</font></p>this
<p align="center"><font face="仿宋" color="#004D88" size="4">6. 字节流读写中文</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">7.流的标准处理异常代码1.6版本及其之前</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">8.流的标准处理异常代码1.7版本以后</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">9.拷贝文件</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">10.录入数据拷贝到文件</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">11.字符流 FileReader</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">12.字符流 FileWriter</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">13.字符流的拷贝</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">14.什么状况下使用字符流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">15.字符流是否能够拷贝非纯文本的文件</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">16.自定义字符数组的拷贝</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">17.带缓冲的字符流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">18.readLine()和newLine()方法</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">19.LineNumberReader</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">20.装饰设计模式</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">21.使用指定的码表读写字符</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">22.序列流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">23.序列流整合多个</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">24.内存输出流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">26.对象操做流ObjecOutputStream</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">27.对象操做流ObjectInputStream</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">28.对象操做流优化</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">29.id号</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">30.打印流的概述和特色</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">31.标准输入输出流概述和输出语句</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">32.修改标准输入输出流拷贝图片</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">33.两种方式实现键盘录入</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">34.随机访问流概述和读写数据</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">35.数据输入输出流</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">36.Properties的概述和做为Map集合的使用</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">37.获取Properties中的每个键</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">38.Properties的load()和store()功能</font></p>
<p align="center"><font face="仿宋" color="#004D88" size="4">39.ZIP文档</font></p>
<p align="center"><b><font color="#FF6600" size="4"> - - - - - - - - - - - - - - - </font></b></p>
<br/>
<br/>
<br/>
<span id = "1"></span>
FileInputStream fis = new FileInputStream("乌合之众.txt"); //建立一个文件输入流对象,并关联乌合之众.txt int b; //定义变量,记录每次读到的字节 while((b = fis.read()) != -1) { //将每次读到的字节赋值给b并判断是不是-1 System.out.println(b); //打印每个字节 } fis.close(); //关闭流释放资源
<br/>
<span id = "2"></span>
read()
方法读取的是一个字节,为何返回是int
,而不是byte
byte
,有可能在读到中间的时候遇到11111111
,那么这11111111
是byte
类型的-1
,程序是遇到-1就会中止
,后面的数据就读不到了。因此在读取的时候用int
类型接收,会在其前面补上24个0凑足4个字节,那么byte
类型的-1
就变成int
类型的255
了这样能够保证整个数据读完,而结束标记的-1
就是int
类型。<br/> <span id = "3"></span>
write(byte[] b)
write(byte[] b, int off, int len)
写出有效的字节个数
定义小数组的标准格式
FileInputStream fis = new FileInputStream("李志 - 梵高先生.flac"); FileOutputStream fos = new FileOutputStream("梵高先生.flac"); int len; byte arr[] = new byte[8*1024]; //自定义字节数组 while((len=fis.read(arr))!=-1){ fos.write(arr, 0, len); //写出字节数组写出有效个字节个数 } fis.close(); fos.close();
<br/> <span id = "4"></span>
BufferedInputStream
BufferedInputStream
内置了一个缓冲区(数组)BufferedInputStream
中读取一个字节时,BufferedInputStream
会一次性从文件中读取8192
个, 存在缓冲区中, 而后返回给程序一个字符。8192
个BufferedOutputStream
BufferedOutputStream
也内置了一个缓冲区(数组)BufferedOutputStream
才会把缓冲区中的数据一次性写到文件里。组合流过滤器实现拷贝
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("李志 - 梵高先生.flac")); //建立缓冲区对FileInputStream对象的装饰 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("2004-梵高先生.flac")); //建立缓冲区对FileOutputStream对象的装饰 int b; while((b = bis.read()) != -1) { bos.write(b); } bis.close();//只关装饰后的对象便可 bos.close();
Buffered
的读取哪一个更快?
8192
个字节大小和Buffered
比较的话,定义小数组会略胜一筹,由于读和写操做的是同一个数组,而Buffered
操做的是两个数组
.<br/> <span id = "5"></span>
flush()
方法
close()
方法
close()
方法,不但会关闭流,还会再关闭流以前刷新缓冲区,关闭后不能再写出<br/> <span id = "6"></span>
<br/> <span id = "7"></span> ## ~~<font color="#FF6600" size="4">**流的标准处理异常代码1.6版本及其之前**</font>~~
FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("from.txt"); fos = new FileOutputStream("to.txt"); int b; while((b = fis.read()) != -1) { fos.write(b); } } finally { try { if(fis != null) fis.close(); }finally { if(fos != null) fos.close(); } }
<br/> <span id = "8"></span>
try( FileInputStream fis = new FileInputStream("from.txt"); FileOutputStream fos = new FileOutputStream("to.txt"); ){ int b; while((b = fis.read()) != -1) { fos.write(b); } }
{. . .}
执行后就会自动调用流对象的close方法将流关掉.<br/> <span id = "9"></span>
Scanner sc = new Scanner(System.in); System.out.println("请输入一个文件路径"); String line = sc.nextLine(); //将键盘录入的文件路径存储在line中 File file = new File(line); //封装成File对象 FileInputStream fis = new FileInputStream(file); FileOutputStream fos = new FileOutputStream(file.getName()); int len; byte[] arr = new byte[8192]; //定义缓冲区 while((len = fis.read(arr)) != -1) { fos.write(arr,0,len); } fis.close(); fos.close();
<br/> <span id = "10"></span> ## <font color="#FF6600" size="4">**录入数据拷贝到文件**</font>
Scanner sc = new Scanner(System.in); FileOutputStream fos = new FileOutputStream("text.txt"); System.out.println("请输入:"); while(true) { String line = sc.nextLine(); if("quit".equals(line)) break; fos.write(line.getBytes()); fos.write("\r\n".getBytes()); } fos.close();
<br/> <span id = "11"></span> ## <font color="#FF6600" size="4">**字符流 FileReader**</font>
字符流是什么
字符
, 就要先读取到字节
数据, 而后转为字符
. 若是要写出字符, 须要把字符转为字节再写出.FileReader
FileReader fr = new FileReader("from.txt"); //建立输入流对象,关联from.txt int ch; while((ch = fr.read()) != -1) { //将读到的字符赋值给ch System.out.println((char)ch); //将读到的字符强转后打印 } fr.close(); //关流
<br/> <span id = "12"></span> ## <font color="#FF6600" size="4">**字符流 FileWriter**</font>
字符
转为字节
写出FileWriter fw = new FileWriter("to.txt"); fw.write("write"); fw.close();
<br/> <span id = "13"></span> ## <font color="#FF6600" size="4">**字符流的拷贝**</font>
FileReader fr = new FileReader("from.txt"); FileWriter fw = new FileWriter("to.txt"); int ch; while((ch = fr.read()) != -1) { fw.write(ch); } fr.close(); fw.close();
<br/> <span id = "14"></span>
<span id = "15"></span>
"?"
代替,写出的时候会将"?"
字符转换成字节写出去。如此这般,写出以后的文件就错乱了。<br/> <span id = "16"></span>
FileReader fr = new FileReader("form.txt"); //建立字符输入流,关联aaa.txt FileWriter fw = new FileWriter("to.txt"); //建立字符输出流,关联bbb.txt int len; char[] arr = new char[1024*8]; //建立字符数组 while((len = fr.read(arr)) != -1) { //将数据读到字符数组中 fw.write(arr, 0, len); //从字符数组将数据写到文件上 } fr.close(); //关流释放资源 fw.close();
<br/> ## <span id = "17"></span> <font color="#FF6600" size="4">**带缓冲的字符流**</font>
BufferedReader
的read()
方法读取字符时会一次读取若干字符到缓冲区, 而后逐个返回给程序, 减小读取次数, 以期提升效率。BufferedWriter
的write()
方法写出字符时会先写到缓冲区, 缓冲区写满时才会写到文件, 减小写入次数, 以期提升效率。BufferedReader br = new BufferedReader(new FileReader("form.txt")); //建立字符输入流对象,关联aaa.txt BufferedWriter bw = new BufferedWriter(new FileWriter("to.txt")); //建立字符输出流对象,关联bbb.txt int ch; while((ch = br.read()) != -1) { //read一次,会先将缓冲区读满,从缓冲去中一个一个的返给临时变量ch bw.write(ch); //write一次,是将数据装到字符数组,装满后再一块儿写出去 } br.close(); //关流 bw.close();
<br/> <span id = "18"></span>
BufferedReader
的readLine()
方法能够读取一行字符(不包含换行符号)BufferedWriter
的newLine()
能够输出一个跨平台的换行符号"\r\n"BufferedReader br = new BufferedReader(new FileReader("aaa.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("bbb.txt")); String line; while((line = br.readLine()) != null) { bw.write(line); //bw.write(""); //只支持windows系统 bw.newLine(); //跨平台的 } br.close(); bw.close();
<br/> <span id = "19"></span>
LineNumberReader
是BufferedReader
的子类, 具备相同的功能, 而且能够统计行号
getLineNumber()
方法能够获取当前行号setLineNumber()
方法能够设置当前行号LineNumberReader lnr = new LineNumberReader(new FileReader("form.txt")); String line; lnr.setLineNumber(100); //设置行号 while((line = lnr.readLine()) != null) { System.out.println(lnr.getLineNumber() + ":" + line);//获取行号 } lnr.close();
<br/> <span id = "20"></span>
interface Coder { public void code(); }
class Persion implements Coder { @Override public void code() { System.out.println("It's none of my business during the daytime"); System.out.println("Write java at night"); } }
class XPersion implements Coder { private Persion s; //被包装的类的引用 public XPersion (Persion s) { //构造方法将被包装的对象做为参数传入 this.s = s; } @Override public void code() { //对其原有功能进行升级 s.code(); System.out.println("Get cervical spondum buff"); System.out.println("......"); System.out.println("sudden death"); System.out.println("......"); } }
<br/> <span id = "21"></span>
InputStreamReader(字节流,编码表)
OutputStreamWriter(字节流,编码表)
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("UTF-8.txt"), "UTF-8")); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("GBK.txt"), "GBK")); int ch; while((ch = br.read()) != -1) { bw.write(ch); } br.close(); bw.close();
<br/> <span id = "22"></span>
SequenceInputStream(InputStream, InputStream)
FileInputStream fis1 = new FileInputStream("a.txt"); //建立输入流对象,关联a.txt FileInputStream fis2 = new FileInputStream("b.txt"); //建立输入流对象,关联b.txt SequenceInputStream sis = new SequenceInputStream(fis1, fis2); //将两个流整合成一个流 FileOutputStream fos = new FileOutputStream("c.txt"); //建立输出流对象,关联c.txt int b; while((b = sis.read()) != -1) { //用整合后的输入流 fos.write(b); //写到指定文件上 } sis.close(); fos.close();
<br/> <span id = "23"></span>
SequenceInputStream(Enumeration)
FileInputStream fis1 = new FileInputStream("a.txt"); //建立输入流对象,关联a.txt FileInputStream fis2 = new FileInputStream("b.txt"); //建立输入流对象,关联b.txt FileInputStream fis3 = new FileInputStream("c.txt"); //建立输入流对象,关联c.txt Vector<InputStream> v = new Vector<>(); //建立vector集合对象 v.add(fis1); //将流对象添加 v.add(fis2); v.add(fis3); Enumeration<InputStream> en = v.elements(); //获取枚举引用 SequenceInputStream sis = new SequenceInputStream(en); //en传递给SequenceInputStream的构造方法 FileOutputStream fos = new FileOutputStream("d.txt"); int b; while((b = sis.read()) != -1) { fos.write(b); } sis.close(); fos.close();
<br/> <span id = "24"></span>
new ByteArrayOutputStream()
write(int), write(byte[])
toByteArray()
FileInputStream fis = new FileInputStream("a.txt"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int b; while((b = fis.read()) != -1) { baos.write(b); } //byte[] newArr = baos.toByteArray(); //将内存缓冲区中全部的字节存储在newArr中 //System.out.println(new String(newArr)); System.out.println(baos); fis.close();
<br/> <span id = "25"></span>
FileInputStream fis = new FileInputStream("a.txt"); //建立字节输入流,关联a.txt ByteArrayOutputStream baos = new ByteArrayOutputStream(); //建立内存输出流 byte[] arr = new byte[5]; //建立字节数组,大小为5 int len; while((len = fis.read(arr)) != -1) { //将文件上的数据读到字节数组中 baos.write(arr, 0, len); //将字节数组的数据写到内存缓冲区中 } System.out.println(baos); //将内存缓冲区的内容转换为字符串打印 fis.close();
<br/> <span id = "26"></span>
new ObjectOutputStream(OutputStream)
,writeObject()
public class ObjectOutputStream { /** * @param args * @throws IOException * 将对象写出,序列化 */ public static void main(String[] args) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("e.txt"));//建立对象输出流 oos.writeObject(p1); oos.writeObject(p2); oos.close(); } }
<br/> <span id = "27"></span>
new ObjectInputStream(InputStream)
, readObject()
public class ObjectInputStream { /** * @param args * @throws IOException * @throws ClassNotFoundException * @throws FileNotFoundException * 读取对象,反序列化 */ public static void main(String[] args) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("e.txt")); Person p1 = (Person) ois.readObject(); Person p2 = (Person) ois.readObject(); System.out.println(p1); System.out.println(p2); ois.close(); } }
<br/> <span id = "28"></span>
Person p1 = new Person("Tom", 20); Person p2 = new Person("Jerry", 22); Person p3 = new Person("Jack", 10); Person p4 = new Person("Herry", 20); ArrayList<Person> list = new ArrayList<>(); list.add(p1); list.add(p2); list.add(p3); list.add(p4); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("f.txt")); oos.writeObject(list); //写出集合对象 oos.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("f.txt")); ArrayList<Person> list = (ArrayList<Person>)ois.readObject(); //泛型在运行期会被擦除,索引运行期至关于没有泛型 //想去掉黄色能够加注解@SuppressWarnings("unchecked") for (Person person : list) { System.out.println(person); } ois.close();
<br/> <span id = "29"></span>
<br/> <span id = "30"></span>
toString()
结果输出, 而且自动加上换行, 并且可使用自动刷出的模式System.out
就是一个PrintStream
, 其默认向控制台输出信息PrintStream ps = System.out; ps.println(97); //底层用的是Integer.toString(x),将x转换为数字字符串打印 ps.println("a string"); ps.println(new Person("Tom", 20)); Person p = null; ps.println(p); //若是是null,就返回null,若是不是null,就调用对象的toString()
print()
, println()
PrintWriter(OutputStream out, boolean autoFlush, String encoding)
PrintWriter pw = new PrintWriter(new FileOutputStream("g.txt"), true); //若是为 true,则 println、printf 或 format 方法将刷新输出缓冲区 pw.write(97); pw.print("Hello"); pw.println("你好"); pw.close();
<br/> <span id = "31"></span>
System.in
是InputStream
, 标准输入流, 默承认以从键盘输入读取字节数据System.out
是PrintStream
, 标准输出流, 默承认以向Console中输出字符和字节数据System.setIn(InputStream)
System.setOut(PrintStream)
System.setIn(new FileInputStream("a.txt")); //修改标准输入流 System.setOut(new PrintStream("b.txt")); //修改标准输出流 InputStream in = System.in; //获取标准输入流 PrintStream ps = System.out; //获取标准输出流 int b; while((b = in.read()) != -1) { //从a.txt上读取字节 ps.write(b); //将数据写到b.txt上 } in.close(); ps.close();
<br/> <span id = "32"></span>
System.setIn(new FileInputStream("png.png")); //改变标准输入流 System.setOut(new PrintStream("copy.png")); //改变标准输出流 InputStream is = System.in; //获取标准输入流 PrintStream ps = System.out; //获取标准输出流 int len; byte[] arr = new byte[1024 * 8]; while((len = is.read(arr)) != -1) { ps.write(arr, 0, len); } is.close(); ps.close();
<br/> <span id = "33"></span>
BufferedReader
的readLine
方法。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Scanner
<br/> <span id = "34"></span>
A:随机访问流概述
RandomAccessFile
类不属于流,是Object
类的子类。但它融合了InputStream
和OutputStream
的功能。B:read()
,write()
,seek()
<br/> <span id = "35"></span>
DataInputStream
, DataOutputStream
能够按照基本数据类型大小读写数据DataOutputStream(OutputStream)
, writeInt()
, writeLong()
DataInputStream(InputStream)
, readInt()
, readLong()
DataOutputStream dos = new DataOutputStream(new FileOutputStream("b.txt")); dos.writeInt(998); dos.writeInt(1998); dos.writeInt(2998); dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("b.txt")); int x = dis.readInt(); int y = dis.readInt(); int z = dis.readInt(); System.out.println(x); System.out.println(y); System.out.println(z); dis.close();
<br/> <span id = "36"></span>
Properties prop = new Properties(); prop.put("abc", 123); System.out.println(prop);
<br/> <span id = "37"></span>
public Object setProperty(String key,String value)
public String getProperty(String key)
public Enumeration<String> stringPropertyNames()
Properties prop = new Properties(); prop.setProperty("name", "Tom"); prop.setProperty("tel", "18000000000"); //System.out.println(prop); Enumeration<String> en = (Enumeration<String>) prop.propertyNames(); while(en.hasMoreElements()) { String key = en.nextElement(); //获取Properties中的每个键 String value = prop.getProperty(key); //根据键获取值 System.out.println(key + "="+ value); }
<br/> <span id = "38"></span>
Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); //将文件上的键值对读取到集合中 prop.setProperty("tel", "18912345678"); prop.store(new FileOutputStream("config.properties"), null); //第二个参数是对列表参数的描述,能够给值,也能够给null System.out.println(prop); Output: {tel=18912345678}
<br/>
<span id = "39"></span>
private static void zipdemo1() throws IOException { try ( ZipInputStream zin = new ZipInputStream(new FileInputStream("code.zip")); ByteArrayOutputStream baos = new ByteArrayOutputStream();//内存输入流 ) { ZipEntry entry; int len; byte arr[] = new byte[1024]; while ((entry = zin.getNextEntry()) != null) { String name = entry.getName();//压缩项目的文件名 System.out.println("文件"+name+"\n解压后大小:"+entry.getSize()); while ((len = zin.read(arr)) != -1) {//读取数据内容 baos.write(arr, 0, len);//内容写入内存输入流 } System.out.println("缓冲区大小:"+baos.size()); System.out.println(baos); baos.reset();//将此 byte 数组输出流的 count 字段重置为零,从而丢弃输出流中目前已累积的全部输出 zin.closeEntry(); } } } Output: 文件code.txt 解压后大小:29 缓冲区大小:29 AW9C2-JN9T2-H5CBV-24QT7-G4YB8 文件code1.txt 解压后大小:39 缓冲区大小:39 激活码:W9WW6-JN9W2-M8CTX-24QR7-M4HB8
<span id = "zz"></span>
——@guoyangde http://www.cnblogs.com/LittleTreasureBox/p/8904016.html