I/O流


一,字节流

InputStream  

OutputStream   两者都是抽象类

InputStream 下的子类  FileInputStream 下的方法   read( )读取字节返回的是 Int类型

因为字节输入流可以操作任意类型的文件,比如图片,音频,这些文件是以二进制形式存在,,如果每次读取都返回Byte,有可能在读时遇到11111111,这在byte类型里是-1,而程序里遇到-1就会停止不读,后面的数据就读不到了 ,因此读取的时候用int类型接受,如11111111会在前面补上24个0,那么byte类型的-1就变成in类型的255了,这样就可以保证数据读完,而结束标记的-1是int类型。

OutputStream 下的子类 FileOutputStream 在创建对象时如果没有这个文件会创建出来,如果有这个文件会将文件清空  用 write( ) 方法续写时需要在 FileOutputStream 对象的第二个参数传入true

FileOutputStream fp = new FileOutputStream("bb.txt",true);
		fp.write(100);
		fp.write(97); fp.write(98); fp.close();

int ?=read(byte[ ]) 将读到的字节个数存到数组里,返回的时读取到的有效字节字数

拷贝文件定义小数组:

FileInputStream fill=new FileInputStream("aa.txt");
		FileOutputStream foll= new FileOutputStream("zz.txt");
		//利用小数组拷贝文件,比较合理 byte [] bt=new byte[1024*9]; int len; while((len=fill.read(bt))!=-1){ foll.write(bt, 0, len); } fill.close(); foll.close(); ___________________________________________________________________________________________ //利用缓冲对象拷贝文件,与小数组速率差不多 BufferedInputStream bui=new BufferedInputStream(new FileInputStream("aa.txt")); BufferedOutputStream buo=new BufferedOutputStream( new FileOutputStream("zz.txt")); int b; while((b=bui.read())!=-1){ buo.write(b); } bui.close(); buo.close();

Close( )方法,关闭前会刷新一次,将数组里残留的字节刷到输出流,刷新后即关闭

Flush( )是刷新方法,每调用一次都会刷新一次,刷新后还可以继续写

//字节输出流输出字符串需先转换成字节数组
FileOutputStream fio =new FileOutputStream("bb.txt");
		fio.write("我的天,你怎么这么帅!".getBytes());
		fio.close();

简单图片加密:将字节异或一个数即可,异或再异或等于它本身


二.字符流

读的时候将字符转换为字节,写入的时候将字节转换为字符,拷贝文件时不推荐使用字符流操作一般只进行读或是只进行写操作时用           不可以拷贝非纯文本文件!


BufferedRead,BufferedWrite 里特有的方法,readLine( )一次性读取一行字符串;newLine( )写出回车换行符

public static void main(String[] args) throws IOException { BufferedReader br=new BufferedReader(new FileReader("aa.txt")); BufferedWriter bw=new BufferedWriter(new FileWriter("kk.txt")); String line; while((line=br.readLine())!=null){ bw.write(line); //bw.newLine(); bw.write("\r\n"); } br.close(); bw.close();

流尽量晚开早关,代码示例如下:

//流尽量晚开早管
	public static void main(String[] args) throws IOException { BufferedReader brc=new BufferedReader(new FileReader("aa.txt")); ArrayList<String>all=new ArrayList<>(); String line; while((line=brc.readLine())!=null){ all.add(line); } brc.close(); BufferedWriter bwc=new BufferedWriter(new FileWriter("bb.txt")); for(int i=all.size()-1;i>=0;i--){ bwc.write(all.get(i)); bwc.newLine(); } bwc.flush(); }

装饰设计模式:

package Demo_FileRead_FileWrite;

public class demo6_Wrap { //装饰设计模式:耦合性不强,被装饰的类与装饰的类变换无关 public static void main(String[] args) { WrapStudent ws=new WrapStudent(new Student()); ws.code(); } } interface Code{ public void code(); } class Student implements Code{ @Override public void code() { System.out.println("我会什么"); System.out.println("我会:"); } } class WrapStudent implements Code{ //1.引用被装饰的类 Student s; //2.在构造方法中传入被装饰的类的对象 public WrapStudent(Student s) { this.s=s; } @Override //3.对原有方法进行升级 public void code() { s.code(); System.out.println("ssh"); System.out.println("ssm"); System.out.println("大数据"); } }

//可以这样写
while((b=bufferedReader.read())!=-1){
			char ch=(char)b; /*if(!tMap.containsKey(ch)){ tMap.put(ch, 1); }else { tMap.put(ch, tMap.get(ch)+1); }*/ tMap.put(ch, !tMap.containsKey(ch)?1:tMap.get(ch)+1); }

递归次数不能过多,否者会造成栈内存溢出   递归举例:

package Demo_FileRead_FileWrite;

import java.io.File;
import java.util.Scanner;

public class demo10_test3 { public static void main(String[] args) { File dir=Getder(); pringJavaFile(dir); } public static File Getder(){ Scanner scanner =new Scanner(System.in); System.out.println("请输入文件夹路径"); while(true){ String nextLine = scanner.nextLine(); File file =new File(nextLine); if(!file.exists()){ System.out.println("您输入的路径不存在!"); }else if (file.isFile()) { System.out.println("您输入的是文件路径"); }else { return file; } } } public static void pringJavaFile(File dir){ File[] listFiles = dir.listFiles(); for (File file : listFiles) { if(file.isFile()&&file.getName().endsWith("jpg")){ System.out.println(file); }else if (file.isDirectory()) { //利用递归思想 pringJavaFile(file); } } } } 

三,其他流

遇到中文乱码如何解决,有以下方法:

1.用字符流

2.用内存输出流    ByteArrayOutputStream        其会在内存中创建自动增长的数组,用完后不需要用close()方法关闭、


序列流  SquenceInputStream    将多个字节输入流整合成一个

随机操作流  RandomAccessFile  既能写也能读

对象操作流  ObjectInputStream/ObjectOutputStream         能够将对象读写入文件

数据操作流  DataInputStream/DataOutputStream         能够根据基本类型的大小读写数据

打印输出流      PrintStream-字节打印流             PrintWrite-字符打印流

Properties 示例代码:

package Demo_OtherIO;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import java.util.Enumeration; import java.util.Properties; public class demo8_Property { public static void main(String[] args) throws IOException, IOException { //demo1(); Properties pr2=new Properties(); pr2.setProperty("name","张三"); pr2.setProperty("age","23"); pr2.setProperty("tel","134526735632"); Enumeration<String> propertyNames = (Enumeration<String>) pr2.propertyNames(); while(propertyNames.hasMoreElements()){ String key = propertyNames.nextElement(); String value = pr2.getProperty(key); System.out.println(key+"="+value); } } private static void demo1() throws IOException, FileNotFoundException { //properties 是一个Map集合,没有泛型,默认是Object Properties pr1=new Properties(); pr1.load(new FileInputStream("properties")); pr1.setProperty("tel", "13548928542"); pr1.store(new FileOutputStream("properties"), null); } }

转载于:https://www.cnblogs.com/wuba/p/11025900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值