字节流File读写
一、功能定义
OutputStream<--FileOutputStream 字节流写入
InputStream<--FileInputStream 字节流读出
二、方法
1.FileOutputStream
1)构造方法(创建一个向指定
File
对象表示的文件中写入数据的文件输出流。)
FileOutputStream(File file)
2)将 b.length
个字节从指定 byte 数组写入此文件输出流中。
void write(byte[] b)
import java.io.*; class FileOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("demo4.txt"); fos.write("abcde".getBytes()); //未刷新,也未关闭,同样可以把数据存入,由于一个中文有两个字节,当计算机读入一个字节时没法操作, //所以需要刷新一下确定字节全部读入,而字节流不需要刷新 } }2.FileInputStream
1)构造方法(通过打开一个到实际文件的连接来创建一个
FileInputStream
,该文件通过文件系统中的
File
对象
file
指定。)
FileInputStream(File file)
3)从此输入流中将最多
b.length
个字节的数据读入一个 byte 数组中。
int
read(byte[] b)
import java.io.*; class FileOutputStreamDemo { public static void main(String[] args) throws IOException { FileOutputStream fos=new FileOutputStream("demo4.txt"); fos.write("abcde".getBytes()); //未刷新,也未关闭,同样可以把数据存入,由于一个中文有两个字节,当计算机读入一个字节时没法操作, //所以需要刷新一下确定字节全部读入,而字节流不需要刷新 fos.close(); FileInputStream fis=new FileInputStream("demo2.txt"); /*read()方法 int ch=0; while((ch=fis.read())!=-1) { System.out.print((char)ch); } */ //read(byte[] b)方法 byte[] a=new byte[1024]; int num=0; while((num=fis.read(a))!=-1) { System.out.println(new String(a,0,num)); } } }4)返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳过)的估计剩余字节数。(即,此输入流下次还可以读取的数据个数)
int available()
FileInputStream fis=new FileInputStream("demo2.txt"); byte[] a=new byte[fis.available];//利用available方法,可以直接定义已知大小的数组来存放数据,但是如果数据过大,还是采用1024的方法 fis.read(a) System.out.println(new String(a)
拷贝一个图片
拷贝一个图片
一、代码
import java.io.*; class CopyPic { public static void main(String[] args) { FileOutputStream fos=null; FileInputStream fis=null; try { fos=new FileOutputStream("demo5.jpg"); fis=new FileInputStream("1.jpg"); byte[] buf=new byte[1024]; int num=0; while((num=fis.read(buf))!=-1) { fos.write(buf,0,num); } } catch (IOException e) { throw new RuntimeException("图片读取出现问题"); } finally { if(fos!=null) try { fos.close(); } catch (IOException e) { throw new RuntimeException("写入图片失败"); } if(fis!=null) try { fis.close(); } catch (IOException e) { throw new RuntimeException("读取图片失败"); } } } }
字节流的缓冲区
字节流的缓冲区
一、功能定义
BufferedOutputStream 该类实现缓冲的输出流。通过设置这种输出流,应用程序就可以将各个字节写入底层输出流中,而不必针对每次字节写入调用底层系统。
BufferedInputStream 为另一个输入流添加一些功能,即缓冲输入以及支持mark
和reset
方法的能力。
二、代码
import java.io.*; class BufferedXputStreamDemo { public static void main(String[] args) throws IOException { long start=System.currentTimeMillis(); copy_1(); long end=System.currentTimeMillis(); System.out.println(start+"..."+end); System.out.println((end-start)+"毫秒"); } public static void copy_1() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("1.jpg")); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("2.jpg")); int by=0; while((by=bis.read())!=-1) { bos.write(by); } bis.close(); bos.close(); } }
自定义字节流缓冲区
一、代码
import java.io.*; class MyBufferedInputStreamDemo { private FileInputStream fis=null; private int pos=0,count=0; private byte[] buf=new byte[1024];//建立一个字节数组缓冲区 MyBufferedInputStreamDemo(FileInputStream fis) { this.fis=fis; } public int myRead() throws IOException { if(count==0) { count=fis.read(buf); if(count<0) return -1; pos=0; byte b=buf[pos]; pos++; count--; return b&255;//将int类型的前32位全变为0 } else if(count>0) { byte b=buf[pos]; pos++; count--; return b&255;//将int类型的前32位全变为0 } return -1; } public void myClose() throws IOException { fis.close(); } } class BufferedXputStreamDemo2 { public static void main(String[] args) throws IOException { long start=System.currentTimeMillis(); copy_1(); long end=System.currentTimeMillis(); System.out.println(start+"..."+end); System.out.println((end-start)+"毫秒"); } public static void copy_1() throws IOException { MyBufferedInputStreamDemo bis=new MyBufferedInputStreamDemo(new FileInputStream("1.jpg")); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("3.jpg")); int by=0; while((by=bis.myRead())!=-1) { bos.write(by); } bis.myClose(); bos.close(); } }
read()方法返回int类型的数据,将byte类型的数据提升为了int类型,即变成了4个字节的数据
write()方法则进行了强转,把int类型强转回了byte类型。
读取键盘录入
一、代码
1.简单的键盘录入读取
import java.io.*; class ReadIn { public static void main(String[] args) throws IOException { InputStream in=System.in; int by=in.read(); System.out.println(by); } }2.如果录入一行数据后,那么就将改行数据编程大写打印;如果该行录入的数据是over,那么就结束录入。
import java.io.*; class ReadIn2 { public static void main(String[] args) throws IOException { InputStream in=System.in; StringBuilder sb=new StringBuilder(); while(true) { int ch=in.read(); if(ch=='\r') continue; if(ch=='\n') { String s=sb.toString(); if(s.equals("over")) break; System.out.println(s.toUpperCase()); sb.delete(0,sb.length()); } else sb.append((char)ch); } } }
读取转换流(InputStreamReader)
一、功能定义
InputStreamReader 是字节流通向字符流的桥梁。
二、代码
import java.io.*; class InputStreamReaderDemo { public static void main(String[] args) throws IOException { InputStream in=System.in;//获取键盘录入流 InputStreamReader fr=new InputStreamReader(in);//将字节流转换成字符流 BufferedReader br=new BufferedReader(fr);//将字符流进行缓冲区技术高效操作 String line=null; while((line=br.readLine())!=null) { if(line.equals("over")) break; System.out.println(line.toUpperCase()); } br.close(); } }
写入转换流(OutputStreamWriter)
一、功能定义
OutputStreamWriter 是字符流通向字节流的桥梁。
二、代码
import java.io.*; class OutputStreamWriterDemo { public static void main(String[] args) throws IOException { //InputStream in=System.in; //InputStreamReader isr=new InputStreamReader(in); //BufferedReader br=new BufferedReader(isr); BufferedReader br=new BufferedReader(new InputStreamReader(System.in));//获取键盘录入 //OutputStream out=System.out; //OutputStreamWriter osw=new OutputStreamWriter(out); //BufferedWriter bw=new BufferedWriter(osw); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));//将数据输出到控制台 String line=null; while((line=br.readLine())!=null) { if(line.equals("over")) break; bw.write(line.toUpperCase()); bw.newLine(); bw.flush(); } br.close(); bw.close(); } }
流操作规律(1)
一、步骤
二、实例
详情参见——19-18,19-19
改变标准输入输出设备
一、概述
System类中有两个方法可以改变输入输出设备
1.重新分配“标准”输入流。
static void setIn(InputStream in)
2.重新分配“标准”输出流。
static void setOut(PrintStream out)
二、代码

IO流——异常的日志信息
一、代码
log4j是一个专门用于生成日志信息的工具包
系统信息
一、方法
1.将属性列表输出到指定的输出流。
void
list(PrintStream out)--->list(System.out)则输出到控制台
二、代码
File类概述
一、File概述
1.用来将文件或者文件夹封装成对象
2.方便对文件或者文件进行属性信息进行操作
3.File对象可以作为参数——传递给流的构造函数
4.弥补了“流”的不足,因为流只能操作数据,操作文件的信息必须要用File对象。
定义:文件和目录路径名的抽象表示形式。
二、方法(字段)
1.与系统有关的默认名称分隔符。
static String separator
2.构造方法(通过将给定路径名字符串转换为抽象路径名来创建一个新
File
实例。)
3.构造方法(根据 parent 路径名字符串和 child 路径名字符串创建一个新
File
实例。)
4.构造方法(根据 parent 抽象路径名和 child 路径名字符串创建一个新File
实例。)

File对象功能
一、File类常见方法
1.创建
1)当且仅当不存在具有此抽象路径名指定名称的文件时,不可分地创建一个新的空文件。
boolean
createNewFile()
在指定位置创建文件,如果该文件已经存在,返回false,否则返回true代表创建成功。

2.删除
1)删除此抽象路径名表示的文件或目录。
boolean
delete()
2)在虚拟机终止时,请求删除此抽象路径名表示的文件或目录。
void
deleteOnExit()
3.判断
1)测试应用程序是否可以执行此抽象路径名表示的文件。
boolean canExecute()
2)测试应用程序是否可以读取此抽象路径名表示的文件。
boolean canRead()
3)测试应用程序是否可以修改此抽象路径名表示的文件。
boolean
canWrite()
4)按字母顺序比较两个抽象路径名。
***5)测试此抽象路径名表示的文件或目录是否存在。
boolean
exists() 在判断一个File对象是目录还是文件时,要先判断该文件是否存在
***6)测试此抽象路径名表示的文件是否是一个目录。
boolean
isDirectory()
***7)测试此抽象路径名表示的文件是否是一个标准文件。
boolean
isFile()
4.获取信息
1.返回由此抽象路径名表示的文件或目录的名称。
2.将此抽象路径名转换为一个路径名字符串。
3.返回此抽象路径名表示的文件最后一次被修改的时间。
long lastModified()
4.返回由此抽象路径名表示的文件的长度。
long length()
5.返回此抽象路径名父目录的路径名字符串;如果此路径名没有指定父目录,则返回null
。
文件列表
一、方法
1.列出可用的文件系统根。
static
File[ ]
listRoots()

2.返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中的文件和目录。

3. 返回一个字符串数组,这些字符串指定此抽象路径名表示的目录中满足指定过滤器的文件和目录。

列出目录下的所有内容——递归
代码:
import java.io.*; class FileListDemo { public static void main(String[] args) { File f=new File("C:\\"); list(f); } public static void list(File f) { System.out.println(f); File[] fl=f.listFiles(); for(int i=0;i<fl.length;i++) { if(fl[i].isDirectory()) list(fl[i]); else System.out.println(fl[i]); } } }递归注意事项:
1.要注意递归结束条件,避免死循环。
2.注意递归的次数,避免内存溢出。
删除一个带内容的目录
一、删除原理
在Windows中,删除目录,是从里往外删除文件。
二、代码
import java.io.*; class FileRemoveDemo { public static void main(String[] args) { File f=new File("D:\\remove"); list(f); } public static void list(File f) { File[] fl=f.listFiles(); for(int i=0;i<fl.length;i++) { if(fl[i].isDirectory()) list(fl[i]); else System.out.println(fl[i]+"文件"+fl[i].delete()); } System.out.println(f+"文件夹"+f.delete()); } }
创建Java文件列表清单
一、思路
1.对指定的目录进行递归
2.获取递归过程所有java文件的路径
3.将这些路径存储在集合中
4.将集合中的数据存入文件中
1.将指定目录下的所有.java文件存储到集合中
import java.io.*; import java.util.*; class JavaFileListDemo { public static void main(String[] args) { File f=new File("D:\\javap"); ArrayList<File> a=new ArrayList<File>(); fileToList(f,a); System.out.println(a.size()); } public static void fileToList(File f,ArrayList<File> a) { File[] fl=f.listFiles(); for(File f1 : fl) { if(f1.isDirectory()) fileToList(f1,a); else { if(f1.getName().endsWith(".java")); a.add(f1); } } } }2.将上文中存储到集合中的元素,存储到JavaList.txt文件中
import java.io.*; import java.util.*; class JavaFileListDemo2 { public static void main(String[] args) throws IOException { File f=new File("D:\\javap"); ArrayList<File> a=new ArrayList<File>(); fileToList(f,a); System.out.println(a.size()); BufferedWriter bw=new BufferedWriter(new FileWriter("JavaList.txt")); listToFile(bw,a); try { if(bw!=null) { bw.close(); } } catch (IOException e) { throw e; } } public static void fileToList(File f,ArrayList<File> a) { File[] fl=f.listFiles(); for(File f1 : fl) { if(f1.isDirectory()) fileToList(f1,a); else { if(f1.getName().endsWith(".java")) a.add(f1); } } } public static void listToFile(BufferedWriter bw,ArrayList<File> a) { for(File fl:a) { try { String s=fl.getAbsolutePath(); bw.write(s); bw.newLine(); bw.flush(); } catch (IOException e) { throw new RuntimeException("文件写入发生错误"); } } } }
Properties
一、概述
二、方法
1.用指定的键在此属性列表中搜索属性。
2.调用
Hashtable 的方法
put
。
import java.io.*; import java.util.*; class PropertiesDemo { public static void main(String[] args) { setAndGet(); } public static void setAndGet() { Properties p=new Properties(); p.setProperty("zhangsan","14"); p.setProperty("lisi","15"); p.setProperty("wangwu","16"); System.out.println(p); System.out.println(p.getProperty("wangwu")); } }3.返回此属性列表中的键集
import java.io.*; import java.util.*; class PropertiesDemo { public static void main(String[] args) { setAndGet(); } public static void setAndGet() { Properties p=new Properties(); p.setProperty("zhangsan","14"); p.setProperty("lisi","15"); p.setProperty("wangwu","16"); Set<String> set=p.stringPropertyNames(); for(String s: set) { System.out.println(s+"...."+p.getProperty(s)); } } }4.将属性列表输出到指定的输出流。
void

2.方法二
1.以适合使用
list(PrintStream out)
——System.out类型是 PrintStreamProperties存取配置文件
一、思路
二、代码(Properties从文件取出)
1.方法一
import java.io.*; import java.util.*; class PropertiesDemo2 { public static void main(String[] args) throws IOException { Properties p=new Properties(); streamToProperties(p); } public static void streamToProperties(Properties p) throws IOException { BufferedReader br=new BufferedReader(new FileReader("info.txt")); String len=null; while((len=br.readLine())!=null) { String[] s=len.split("="); p.setProperty(s[0],s[1]); } br.close(); System.out.println(p); } }
1.从输入流中读取属性列表(键和元素对)。
void
load(InputStream inStream)
public static void loadDemo() throws IOException { Properties p=new Properties(); BufferedInputStream bis=new BufferedInputStream(new FileInputStream("info.txt")); p.load(bis); System.out.println(p); }三、代码(Properties存入文件)
1.以适合使用
load(InputStream)
方法加载到
Properties
表中的格式,将此
Properties
表中的属性列表(键和元素对)写入输出流。
void
store(OutputStream out,String comments)
public static void storeDemo() throws IOException { Properties p=new Properties(); BufferedInputStream bis=new BufferedInputStream(new FileInputStream("info.txt")); p.load(bis); p.setProperty("wangwu","99"); FileOutputStream fos=new FileOutputStream("info.txt"); p.store(fos,"haha"); }
PrintWriter(PrintStream)
一、功能定义
1.打印流
该流提供了打印方法,可以将各种数据类型的数据原样打印
1)PrintWriter 字符打印流(比较常用)
构造函数可以接收的数据类型
1.file对象。 File
2.字符串路径。 String
3.字节输出流。 OutputStream
4.字符输出流。 Writer
2)PrintStream 字节打印流
构造函数可以接收的数据类型
1.file对象。 File
2.字符串路径。 String
3.字节输出流。 OutputStream
二、代码
1.
PrintWriter 字符打印流(比较常用)
import java.io.*; class PrintWriterDemo { public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter("b.txt")),true); String len=null; while((len=br.readLine())!=null) { if(len.equals("over")) break; pw.println(len.toUpperCase()); } br.close(); pw.close(); } }
合并流(序列流——SequenceInputStream)
一、功能定义SequenceInputStream
表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
二、方法
1.构造方法(将按顺序读取这两个参数,先读取
s1
,然后读取
s2)
import java.io.*; import java.util.*; class SequenceInputStreamDemo { public static void main(String[] args) throws IOException { Vector<FileInputStream> v=new Vector<FileInputStream>(); v.add(new FileInputStream("1.txt")); v.add(new FileInputStream("2.txt")); v.add(new FileInputStream("3.txt")); Enumeration<FileInputStream> e=v.elements(); SequenceInputStream sis=new SequenceInputStream(e); FileOutputStream fos=new FileOutputStream("4.txt"); byte[] c=new byte[1024]; int len=0; while((len=sis.read(c))!=-1) { fos.write(c,0,len); } sis.close(); fos.close(); } }
切割文件与合并文件
一、切割文件
public static void spiltFile() throws IOException { FileInputStream fis=new FileInputStream("1.jpg"); FileOutputStream fos=null; byte[] b=new byte[1024*20]; int len=0; int count=1; while((len=fis.read(b))!=-1) { fos=new FileOutputStream("D:\\javap\\javapart\\"+(count++)+".part"); fos.write(b,0,len); fos.close(); } }
二、合并被切割文件
public static void merge() throws IOException { ArrayList<FileInputStream> a=new ArrayList<FileInputStream>(); a.add(new FileInputStream("D:\\javap\\javapart\\1.part"));//可以用for循环 a.add(new FileInputStream("D:\\javap\\javapart\\2.part")); a.add(new FileInputStream("D:\\javap\\javapart\\3.part")); a.add(new FileInputStream("D:\\javap\\javapart\\4.part")); a.add(new FileInputStream("D:\\javap\\javapart\\5.part")); final Iterator<FileInputStream> it=a.iterator();//匿名内部类要调用本类变量,该变量需要是final类型 Enumeration<FileInputStream> e=new Enumeration<FileInputStream>() { public boolean hasMoreElements() { return it.hasNext(); } public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis=new SequenceInputStream(e); BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("D:\\javap\\javapart\\6.jpg")); byte[] b=new byte[1024*20]; int len=0; while((len=sis.read(b))!=-1) { bos.write(b,0,len); } sis.close(); bos.close(); }
对象的序列化
一、功能定义
将堆内存中的对象存到硬盘上,将Java 对象的基本数据类型和图形写入OutputStream,并用InputSream读取。。即将对象序列化和反序列化——ObjectOutputStream 和 ObjectInputStream类
二、代码
1.将对象序列化
public static void writeObject() throws IOException { ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("object.txt")); oos.writeObject(new Person("zhangsan","15")); oos.close(); }
2.将对象反序列化
public static void readObject() throws Exception//由于readObject方法会报出ClassNotFoundException,所以要进行抛出 { ObjectInputStream ois=new ObjectInputStream(new FileInputStream("object.txt")); Person p=(Person)ois.readObject();//返回的是Object类型,要记得强转 System.out.println(p); ois.close(); }
3.对象在序列化之前需要进行的操作
<pre name="code" class="java">class Person implements Serializable { static final long serialVersionUID = 42L;//可以自定义序列号UID,防止对象修改后反序列化过程中无法返回对象。 String name=null; String age=null; Person(String name,String age) { this.name=name; this.age=age; } public String toString() { return name+":"+age; } }
管道流
一、功能定义
输入输出可以直接进行连接,通过结合现成使用
使用PipedInputStream 和PipedOutputStream类来实现
1.管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的所有数据字节。
2.通常,数据由某个线程从
PipedInputStream
对象读取,并由其他线程将其写入到相应的
PipedOutputStream
。不建议对这两个对象尝试使用单个线程,因为这样可能死锁线程。
二、代码
import java.io.*; class PipedStreamDemo { public static void main(String[] args) throws IOException { PipedInputStream in=new PipedInputStream(); PipedOutputStream out=new PipedOutputStream(); in.connect(out); Read r=new Read(in); Write w=new Write(out); new Thread(r).start(); new Thread(w).start(); } } class Read implements Runnable { private PipedInputStream in; Read(PipedInputStream in) { this.in=in; } public void run() { try { byte[] buf=new byte[1024]; int len=in.read(buf); String s=new String(buf,0,len); System.out.println(s); in.close(); } catch (IOException e) { throw new RuntimeException("管道读取失败"); } } } class Write implements Runnable { private PipedOutputStream out; Write(PipedOutputStream out) { this.out=out; } public void run() { try { out.write("piped lai la".getBytes()); out.close(); } catch (IOException e) { throw new RuntimeException("管道写入失败"); } } }
RandomAccessFile
一、功能定义
1.此类的实例支持对随机访问文件的读取和写入。
2.随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组。(即内部封装了一个数组,可以通过指针位置来对数组元素进行操作)
3.存在指向该隐含数组的光标或索引,称为
文件指针;输入操作从文件指针开始读取字节,并随着对字节的读取而前移此文件指针。
4.如果随机访问文件以读取/写入模式创建,则输出操作也可用;输出操作从文件指针开始写入字节,并随着对字节的写入而前移此文件指针。
5.该文件指针可以通过
getFilePointer
方法读取,并通过
seek
方法设置。
6.通过构造方法可以看出,该类只能操作文件
7.如果模式为r,那么则会去读取一个文件,如果这个文件不存在,那么就会报异常。
8.如果模式为rw,那么如果文件不存在,则会去自动创建;如果文件存在,则不会覆盖
9.可以实现数据的分段写入(即一个线程负责一段数据来进行写入——下载软件的原理)
完成读写的原理——内部封装了字节输入流和字节输出流。
二、方法
1.mode 参数指定用以打开文件的访问模式。
"r" | 以只读方式打开。调用结果对象的任何 write 方法都将导致抛出 IOException 。 |
"rw" | 打开以便读取和写入。如果该文件尚不存在,则尝试创建该文件。 |
"rws" | 打开以便读取和写入,对于 "rw",还要求对文件的内容或元数据的每个更新都同步写入到底层存储设备。 |
"rwd" | 打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。 |
2.设置到此文件开头测量到的文件指针偏移量,在该位置发生下一个读取或写入操作。
void
seek(long pos)
3.向此文件写入指定的字节。
void
write(int b)
4.按四个字节将 int
写入该文件,先写高字节。
void writeInt(int v)
import java.io.*; class RandomAccessFileDemo { public static void main(String[] args) throws IOException { //Write(); //Read(); Write2(); } public static void Write() throws IOException { RandomAccessFile raf=new RandomAccessFile("random.txt","rw"); raf.write("赵四".getBytes()); raf.writeInt(97);//write()方法只保留最低8位,所以会造成数据丢失。所以使用writeInt()方法来写入int类型数据。 raf.write("老七".getBytes()); raf.writeInt(95); raf.close(); } public static void Read() throws IOException { RandomAccessFile raf=new RandomAccessFile("random.txt","r"); raf.seek(8);//如果想直接取老七的数据,那么设定指针为8。因为赵四和年龄的数据占8个字节 //raf.skipBytes(8) 只能往前跳,不能往后跳————指针跳过指定的字节数 byte[] buf=new byte[4]; raf.read(buf); String name=new String(buf); int age=raf.readInt(); System.out.println(name+"..."+age); } public static void Write2() throws IOException//随机位置写入数据。不同于流,流需要按照顺序写入 { RandomAccessFile raf=new RandomAccessFile("random.txt","rw"); raf.seek(8*3);//如果设置8*0,那么就会用王五来覆盖赵四的数据。 raf.write("王五".getBytes()); raf.writeInt(99); } }
DataStream
一、功能定义
DataStream
DataOutputStream 和 DataInputStream
1.数据输出流允许应用程序以适当方式将基本 Java 数据类型写入输出流中。
2.不同于ObjectStream是处理对象,DataStream是专门用来处理基本数据类型的。
二、方法
1.创建一个新的数据输出流,将数据写入指定基础输出流。
import java.io.*; class DataStreamDemo { public static void main(String[] args) throws IOException { writeData(); } public static void writeData() throws IOException { DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt")); dos.writeInt(234); dos.writeBoolean(true); dos.writeDouble(9887.543); dos.close(); } public static void readData() throws IOException { DataInputStream dis=new DataInputStream(new FileInputStream("data.txt")); int num=dis.readInt(); boolean b=dis.readBoolean(); double d=dis.readDouble(); dis.close(); } }
ByteArrayStream
一、功能定义
——用流的思想来操作数组
相似的类有——CharArrayReader和CharArrayWriter StringReader和StringWriter
二、代码
import java.io.*; class ByteStreamDemo { public static void main(String[] args) { ByteArrayInputStream bsis=new ByteArrayInputStream("ABCDEF".getBytes()); ByteArrayOutputStream baos=new ByteArrayOutputStream(); int by=0; while((by=bsis.read())!=-1) { baos.write(by); } System.out.println(baos.size()); System.out.println(baos.toString()); } }