黑马程序员---IO流

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------

String

StringBuffer  同步安全,效率比之StringBuilder低,可变字符序列
StringBuilder 不同步,效率高,可变字符序列。

String str=”1”+”2”+”3”+”4”; 会分别在字符串池中创建“12”123”  “1234”多出2个不需要的字符串。而StringBuilderStringBuffer对象通过append(String)添加字符串只会在字符串中创建一个字符串。

IO

流向分为:输出流(写入),输入流(读取)  流操作数据分为:字符流,字节流

字符流抽象基类:Reader Writer    字节流抽象基类:InputStream OutputStream

流对象一旦创建,必须close()流。关闭流后,流就不能进行任何操作了。好比男女朋友一样,一旦创建恋爱关系,当不爱时也得说一句分手,得告诉对方咱们不是男女朋友关系了,彼此自由了。但你我就不要再有任何联系了。

字节输入流中的available()判断读取文件的大小

flush():当输出流使用到缓冲区的时候需要在write()flush()

//字符输入流

	FileReader fileReader=null;
	String str="";
         BufferedReader bufr=null;
		try {
			fileReader=new FileReader("abc.txt");
			bufr=new BufferedReader(fileReader);
			
			while((str=bufr.readLine())!=null){
				System.out.println(str);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
				if (bufr!= null) {
				try {
					bufr.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

}
//字符输出流
		FileWriter fileWriter = null;
		BufferedWriter bufw = null;
		String str = "";
		try {
			fileWriter = new FileWriter("abc.txt");
			bufw= new BufferedWriter(fileWriter);

			bufw.write("234");
			bufw.flush();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (bufw != null) {
				try {
					bufw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}



//复制文件1

	private static void copyFile1() throws IOException {
		FileInputStream fileInputStream = new FileInputStream("123.mp4");
		BufferedInputStream bufInputStream = new BufferedInputStream(
				fileInputStream);

		FileOutputStream fileOutputStream = new FileOutputStream("321.mp4");
		BufferedOutputStream bufOutputStream = new BufferedOutputStream(
				fileOutputStream);
		
		//缓冲区中再次使用缓冲区
		int len = 0;
		byte []by=new byte[1024]; 
		while ((len = bufInputStream.read(by)) != -1) {
			bufOutputStream.write(by,0,len);
			bufOutputStream.flush();
		}
		//不在自定义缓冲区了(经过简单测试此比有缓冲区慢)
		/*int ch=0;
		while ((ch=bufInputStream.read())!=-1) {
			bufOutputStream.write(ch);
		}*/
		bufInputStream.close();
		bufOutputStream.close();
	}


//复制文件2(自定义缓冲区)

	private static void copyFile2() throws IOException {
		FileInputStream fileInputStream=new FileInputStream("123.mp4");
		FileOutputStream fileOutputStream=new FileOutputStream("321.mp4");
		
		int len=0;
		byte[]by=new byte[1024];
		while((len=fileInputStream.read(by))!=-1){
			fileOutputStream.write(by, 0, len);
			fileOutputStream.flush();
		}
		
		fileInputStream.close();
		fileOutputStream.close();
		
	}

缓冲:BufferedReader (子类 LineNumberReader跟踪行号的缓冲字符输入流) BufferedWriter  BufferedOutputStream BufferedInputStream  装饰设计模式

装饰设计模式

需对某对象的功能进行扩展,可以使用装饰设计模式而非使用继承。因为只对某功能进行扩展,使用继承的话,子类会多出没必要进行扩展的功能会使得整个类体系杂乱臃肿。装饰比继承灵活。装饰类和被装饰类都必须实现同一接口或所属同一父类。


 默认的输入设备和输出设备的流不需要关,因为只有一个流。InputStream in=System.in;in.read();in.close();

转换流:InputStreamReader 字节流转换为字符流 此过程称为解码

                        OutputStreamWriter   字符流转换为字节流  此过程称为编码

//遍历目录

 private static void fileList(File file) throws IOException {
  if (file.isDirectory()) {
   File[] strings = file.listFiles();
   for (File string1 : strings) {
    if (string1.isDirectory()) {
     fileList(string1);
    } else {
     System.out.println(string1.getName());
    }
   }
  }

 }


//删除文件夹及文件

	private static void fileList(File file) throws IOException {
		if (file.isDirectory()) {
			File[] strings = file.listFiles();
			for (File string1 : strings) {
				/*if (string1.isDirectory()) {
					fileList(string1);
				} else {
					System.out.println(string1.getName());
				}*/
				if(string1.isDirectory()){
					fileList(string1);
				}else{
					string1.delete();
				}
			}
			file.delete();
		}

	}


//将properties数据存储到文件中

	Properties properties=new Properties();
		properties.setProperty("123", "abc");
		properties.setProperty("345", "efg");
		properties.setProperty("567", "hjk");
		
		OutputStream outputStream=new FileOutputStream("info.txt");
		
		properties.store(outputStream, "");
		
		outputStream.close();

//文件合并

		ArrayList<FileInputStream> alArrayList=new ArrayList<FileInputStream>();
		alArrayList.add(new FileInputStream("1.txt"));
		alArrayList.add(new FileInputStream("2.txt"));
		alArrayList.add(new FileInputStream("3.txt"));
		
		Enumeration<FileInputStream> en=Collections.enumeration(alArrayList);
		
		SequenceInputStream sequenceInputStream=new SequenceInputStream(en);
		
		FileOutputStream fileOutputStream=new FileOutputStream("4.txt");
		byte []by=new byte[1024];
		int len=0;
		while((len=sequenceInputStream.read(by))!=-1){
			fileOutputStream.write(by,0,len);
		}
		sequenceInputStream.close();
		fileOutputStream.close();

//文件切割

		FileInputStream fileInputStream=new FileInputStream("38.mp4");
		FileOutputStream fileOutputStream=null;
		int len=0;
		byte[]by=new byte[1024*1000];//1M
		int count=0;
		while((len=fileInputStream.read(by))!=-1){
			count++;
			fileOutputStream=new FileOutputStream(count+".part");
			fileOutputStream.write(by,0, len);
		}
		fileInputStream.close();
		fileOutputStream.close();


//文件过滤及删除

	File file = new File("E:\\Eclipse\\exam");//此处得是目录,否则异常 得先判断
           if(file.isDirectory()){
		File[] f = file.listFiles(new MyFileFilter());//此处可以使用内部类避免单独创建一个过滤类
		for (File file2 : f) {
			System.out.println(file2.toString());	
			file2.delete();
		}
}
//过滤器
public class MyFileFilter implements FilenameFilter {

	@Override
	public boolean accept(File dir, String name) {
		// TODO Auto-generated method stub
		return name.endsWith(".part");
	}

}

//对象流 ObjectOutputStream  ObjectInputStream

ObjectOutputStream objOutputStream = new ObjectOutputStream(    //写入
				new FileOutputStream("123.txt"));

		objOutputStream.writeObject(new Person(12, "jeck"));
		objOutputStream.flush();
		objOutputStream.close();
                  //读取
		ObjectInputStream objectInputStream = new ObjectInputStream(
				new FileInputStream("123.txt"));

		Person person = (Person) objectInputStream.readObject();
		System.out.println(person.getAge() + ":" + person.getName());
		objectInputStream.close();



//编码

中文在GBK中2个字节,UTF—8中3个字节

		String string="123中午";//编码
		byte[]b=string.getBytes("GBK");
		for (byte c : b) {	
			System.out.println(c);
		}



 

---------------------- ASP.Net+Android+IOS开发.Net培训、期待与您交流! ----------------------详细请查看: http://edu.csdn.net
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值