10.java io流操作
*参数常见 field 字段即类中定义的常量
*内存流 本质是一个长度可变的数组
**装饰者模式
被装饰类A 对象a 装饰类B
A被装饰的目的是为了在不动A的前提下,动态扩展A功能
装饰方法:新建B类,创建a,想扩展什么功能,
就新建一个方法,参数传(a),里面用a调用A类方法,要扩展功能写在B中。
若扩展的功能很多并且不想让这些功能一同实现,则将B抽象,创建共同方
法,其他功能让子类实现。注意:B中a对象要设私有,然后B子类要传B中的a
则B中需要创建一个geta的方法。
装饰模式与继承区别
String cmd = "cmd /c shutdown -r -t";
Runtime.getRuntime().exec(cmd);
shutdown -r -t 100 关机 shutdown -a
加密方法:将图片读取后写入到不识别文件中,并进行加密
对象流 要实现序列化保存到本地 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(f1));
实现Serializable 实现前要声明版本号
静态属性不能被序列化,属于类的都不能被序列化,只能序列化对象
*随机访问文件类 RandomAccessFile 既可以读又可以写 写入后光标跟着移动,seek(1)设置文件光标移到离开头举例 为1处skipBytes(3)向后跳3个字节
可读入基本数据类型和字节数组,直接处理文件,有readLine方法
*字节流和字符流的区别 字符流只处理字符(自带一块缓存的字节流)
*转换流是处理流
***记住了,不关流,文件删不掉,直到程序结束
**用“|”切割字符串时,应该“\\|”
*BufferedReader bfr=new BufferedReader(new InputStreamReader(System.in)); 键盘读取
*字符串转字节数组getBytes() 将字符串写入流时用到
*字节流和字符流区别:字节8位,字符16位
**常用的节点流
文 件 *FileInputStream FileOutputStrean FileReader FileWriter 文件进行处理的节点流
数 组 *ByteArrayInputStream ByteArrayOutputStream CharArrayReader CharArrayWriter 对数组进行处理的节点流(对应的不再是文件,而是内存中的一个数组)
常用方法 toString()将内存流输出为字符串,可指定编码。toByteArray()将内存流以写入字节数组,,write()将数据写入本流,writeTo将本流写入其他流。
举例FileInputStream fos=new FileInputStream("G:\\KwDownload\\song\\萧敬腾-倒带.mp3");
FileOutputStream fis=new FileOutputStream("d:\\萧敬腾-倒带.mp3");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] b=new byte[1024];
while(fos.read(b)!=-1){
baos.write(b); //往本流中写入
} baos.writeTo(fis); //直接写入输出流
管 道 *PipedInputStream PipedOutputStream PipedReader PipedWriter 对管道进行处理的节点流 不需要中间人,两个管道可以直接连接,连接方式通过构造方法或content方法,一般用于多线程
**常用处理流(关闭处理流使用关闭里面的节点流)
缓冲流 *BufferedImputStrean BufferedOutputStream BufferedReader BufferedWriter ----需要父类作为参数构造,增加缓冲功能于带了缓冲功能,所以就写数据的时候需要使用flush方法
转换流 *InputStreamReader OutputStreamWriter要inputStream 或OutputStream作为参数,实现从字节流到字符流的转换 在构造中可指定编码方式,特有方法getEncoding()
数据流 *DataInputStream DataOutputStream -提供将基础数据类型写入到文件中 字节流 可读基本数据类型和字节数组
*InputStream常用方法: 接口
---->int read(): int read(byte[] b): int read(byte[] b, int off, int len) available()返回可读取的字节数 close()
*Reader常用方法:接口
---->int read() int read(char[] c) int read(char[] c, int off, int len) close()
OutputStream字节输出流和Writer字符输出流具有以下方法:接口
void write(int c)
void write(byte[]/char[] buf)
void write(byte[]/char[] buf, int off,int len)
flush() close()
Writer特有:
void write(String s)
void write(String s, int off, int len)
flush() append()添加字符或字符串
FileInputStream 同InputStream
举例 FileInputStream fis=new FileInputStream(new File("c:\\pro\\aa.txt"));
while((temp = fis.read())!=-1){//尝试读取一个字节
System.out.println((char)temp);
FileOutputStream 同OutputStrram
FileReader 同InputStreamReader(构造中可传编码格式)
---->close() getEncoding(获得编码格式) read() read(char[] cbuf, int offset, int length)
重点注意Writer及子类构造中的append方法
FileWriter FileWriter(String fileName, boolean append) 同父类OutputStreamWriter
---->flush() close() getEncoding() write(char[] cbuf, int off, int len) write(int c) write(String str, int off, int len)
缓冲流 BufferedInputStream(InputStream in, int size)指定缓冲区大小 read(byte[] b, int off, int len)
----> close() available()
BufferedOutputStream(OutputStream out, int size)
----> write(byte[] b, int off, int len) write(int b) flush()
BufferedReader(Reader in, int sz)
----> close() read(char[] cbuf, int off, int len) readLine()
BufferedWriter(Writer out, int sz)
----> close() flush() newLine() write(char[] cbuf, int off, int len) write(String s, int off, int len)
举例 BufferedReader br=new BufferedReader(new FileReader("c:\\pro\\English.txt"));
BufferedWriter bw=new BufferedWriter(new FileWriter("c:\\copy\\English.txt"));
String line=null;
while((line = br.readLine())!=null){
bw.write(line);
bw.newLine();
判断退出while(true){
line = br.readLine();
if("over".equalsIgnoreCase(line)){
System.out.println("您已退出。。");
break;
对象流 ObjectInputStream readObject()
ObjectOutputStream 可以将任何类型对象写入文件,注意对象要实现 Serializable 实现前要声明版本号 writeUTF(String str) writeObject(Object obj)
创建对象 ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(f1));
打印流 PrintStream PrintWriter 他俩功能相同
BufferedReader bis=new BufferedReader(new InputStreamReader(System.in));
byte[] b=new byte[1024];
String str=null;
PrintWriter pw=new PrintWriter(System.out);
while ((str=bis.readLine())!=null){
pw.println(str);
pw.flush();