【Java学习20170328】IO流

Java第六章

IO流



InputStreamReader 是字节流通向字符流的桥梁

BufferedReader in  = new BufferedReader(new InputStreamReader(System.in));

OutputStreamWriter是字符流通向字节流的桥梁

Writer out = new BufferedWriter(new OutputStreamWriter(System.in));

随机数

int n = new Random().nextInt(5);

int n = (int)(Math.random()*list.size());

package com.share.test_3_28;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class IOTest {
	public static void main(String[] args) {
		IOTest f = new IOTest();
		File file = new File("C:\\Users\\Administrator\\Desktop\\FileCopyTest.java");
		f.readfile(file);
	}
	/**
	 * 一次读取3k
	 * @param file
	 */
	public void readfile2(File file){
		InputStream is = null;
		try {
			is = new FileInputStream(file);
			byte[] b = new byte[1024*3];
			int len = is.read(b);
			System.out.println("实际读取的字节为:"+len);
			for (byte c : b) {
				System.out.print((char)c);
			}
			System.out.println("最后:"+is.read(b));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 一次读取一个字节
	 */
	public void readfile(File file){
		InputStream is = null;
		try {
			is = new FileInputStream(file);
			int len = 0;
			while ((len = is.read()) != -1) {
				System.out.print((char)len);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if (is!=null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	
}

package com.share.test_3_28;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class IOTest1 {
	public static void main(String[] args) {
		
	}
	public void readfile(File file){
		InputStream is = null;
		int len = 0 ;
		try {
			is =new FileInputStream(file);
			while ((len = is.read()) != -1) {
				
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}

package com.share.test_3_28;

public class Main {
	public static void main(String[] args) {
		Person p = new Person("tangxin",1001){
			String school;
			
			@Override
			public void show() {
				// TODO Auto-generated method stub
				System.out.println("名字:"+name+"\t学号:"+id+school);
//				show("tangxin",1001);
			}
			
			/*public void show(String name,int id){
				System.out.println("名字:"+name+"\t学号:"+id);
			}*/
			
			
			
		};//当需要对类进行一次临时更改时,使用匿名内部类,直接在构造方法后面添加大括号,括号里面重写需要更改的内容【多用于测试】
		p.show();
		
		Person p1 = new Person("tangxin",1002);
		p1.show();
		
		Person p3 =new Person("tangxin",1003){
			@Override
			public String toString() {
				// TODO Auto-generated method stub
				return hashCode()+"";
			}
		};
		System.out.println("p1:"+p1);
		System.out.println("p:"+p);
		System.out.println("p3:"+p3);
	}
}

package com.share.test_3_28;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapHomeWork {
	/**
	 * 遍历Map集合
	 */
	public static void main(String[] args) {
		Map<Integer, String> hm = new HashMap<Integer, String>();
		hm.put(1, "a");
		hm.put(2, "b");
		hm.put(3, "c");

		// 第一种
		Set<Integer> keyset = hm.keySet();
		for (Integer integer : keyset) {
			System.out.println(integer + "::" + hm.get(integer));
		}
		System.out.println("--------------------");
		// 第二种
		for (Map.Entry<Integer, String> me : hm.entrySet()) {
			System.out.println(me.getKey() + "::" + me.getValue());
		}

	}

}
package com.share.test_3_28;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class OutPutStreamHomeWork {
	/**
	 * 从键盘录入信息,写入到文件里面
	 * 录入时,一次获取一行数据,判断输入的数据是否是quit,如果不是输出到文件里,知道输入quit为止
	 * ----将字符串转为字节数组,String,byte的api
	 */
	public static void main(String[] args) {
		OutPutStreamHomeWork hw = new OutPutStreamHomeWork();
		File file = new File("C:\\Users\\Administrator\\Desktop\\File.txt");
		hw.in(file);
	}
	/**
	 */
	public void in(File file){
		OutputStream os = null;
		try {
			os = new FileOutputStream(file);
		} catch (FileNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		while (true) {
			Scanner sc = new Scanner(System.in);
			String i = sc.next();
			System.out.println(i);
			if (i.equals("quit")) {
				break;
			}
			//InputStream is = null;
			
			
			//byte[] a = new byte[1024];
			byte[] a = i.getBytes();
			int len = 0;
			try {
				os.write(a);
				} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally{
				try {
					os.flush();
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
		
	}
	
	
	
	
}

package com.share.test_3_28;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class OutPutStreamTest {
	public static void main(String[] args) {
		File oldfile = new File("C:\\Users\\Administrator\\Desktop\\FileCopyTest.java");
		File newfile = new File("C:\\Users\\Administrator\\Desktop\\File.java");
		OutPutStreamTest o = new OutPutStreamTest();
		o.copy(oldfile, newfile);
	}
	/**
	 * 从一个文件中读取数据,并将这个数据写入到另一个文件里面
	 * @param oldfile
	 * @param newfile
	 */
	public void copy(File oldfile,File newfile){
		OutputStream os = null;
		InputStream is = null;
		try {
			is = new FileInputStream(oldfile);
			os = new FileOutputStream(newfile);
			byte[] a = new byte[1024];
			int len = 0;
			
			while ((len = is.read(a)) != -1) {
				for (int i = 0; i < len; i++) {
					os.write(a[i]);
				}
			}
			
			
//			while ((len = is.read()) != -1) {
//				os.write(len);
//			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if (os != null) {
				try {
					os.flush();//清空缓冲区
					os.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
}

package com.share.test_3_28;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

public class OutPutStreamTest {

	public static void main(String[] args) {
		OutPutStreamTest ot = new OutPutStreamTest();
		File oldfile = new File("C:\\Users\\Administrator\\Desktop\\健康养生.psd");
		File newfile = new File("C:\\Users\\Administrator\\Desktop\\test.psd");
		// ot.writeFile(oldfile);
		ot.copy2(oldfile, newfile);
//		Scanner sc =null;
//		sc.nextLine();
	}
	/**
	 * 从键盘录入信息,写入到文件里面
	 * 录入时,一次获取一行数据,判断输入的数据是否是quit,如果不是输出到文件里,直到输入quit为止
	 * ---将字符串转化为字节数组,String,Byte的API
	 */

	/**
	 * 向文件写入一个字节
	 * 
	 * @param file
	 */
	public void writeFile(File file) {
		OutputStream os = null;
		try {
			os = new FileOutputStream(file);// 实例化file输出流
			os.write(65);// 利用OutputStream的写入方法写入一个字节的数据---数据为65
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (os != null) // 当os不为空时【说明文件存在,并且连接成功】,关掉连接
					os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 从一个文件中读取数据,并将这个数据写入到另一个文件里面
	 * 
	 * @param oldfile
	 * @param newfile
	 */
	public void copy(File oldfile, File newfile) {
		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(oldfile);
			os = new FileOutputStream(newfile);
			int i = 0;// 存储读取数据变量
			while ((i = is.read()) != -1) {// 从老文件读取数据并将数据存储到变量i里面
				os.write(i);// 将数据写入到新文件
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
				if (os != null) {
					os.flush();// 清空缓冲区,,
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 更有效率的拷贝文件
	 * @param oldfile
	 * @param newfile
	 */
	public void copy2(File oldfile,File newfile){

		InputStream is = null;
		OutputStream os = null;
		try {
			is = new FileInputStream(oldfile);
			os = new FileOutputStream(newfile);
			byte b[] = new byte[1024*4];//创建缓冲区数组
			int len = 0;// 存储实际读取的字节数
			while ((len = is.read(b)) != -1) {// 从老文件读取数据并将数据存储到b数组里面,并且返回实际读取的字节数
				os.write(b,0,len);// 将数据写入到新文件
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (is != null)
					is.close();
				if (os != null) {
					os.flush();// 清空缓冲区,,
					os.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	
	}

}

package com.share.test_3_28;

public class Person {

	public int id;
	public String name;
	public Person(String name,int id){
		this.name = name;
		this.id = id;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "名字:"+name+"\t学号:"+id;
	}
	
	public void show(){
		System.out.println("名字:"+name);
	}
	
}

package com.share.test_3_28;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ShowTxt {
	/**
	 * 写一个方法,显示文件夹下所有的txt文件
	 */
	
	public void showtxt(File file){
		class MyFilter implements FilenameFilter{

			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return name.endsWith("txt");
			}
			
		}
		
		MyFilter filter = new MyFilter();
		File allfile[] = file.listFiles(filter);
		for (File file2 : allfile) {
			System.out.println(file2.getName());
		}
	}
	public void showtxt1(File file){
//		FilenameFilter filter = new FilenameFilter(){
//
//			@Override
//			public boolean accept(File dir, String name) {
//				// TODO Auto-generated method stub
//				return false;
//			}
//			
//		};
		File allfile[] = file.listFiles(new FilenameFilter(){

			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return false;
			}
			
		});
		for (File file2 : allfile) {
			System.out.println(file2.getName());
		}
	}
	/**
	 * 写一个方法,将list集合按学号排序
	 * 用匿名内部类实现
	 * @param args
	 */
	class Person{
		public int age;
		public String name;
		
	}
	public void listSort(List<Person> list){
		class Gz implements Comparator<Person>{

			@Override
			public int compare(Person o1, Person o2) {
				// TODO Auto-generated method stub
				return 0;
			}
			
		}
		Collections.sort(list,new Comparator<Person>() {

			@Override
			public int compare(Person o1, Person o2) {
				// TODO Auto-generated method stub
				return 0;
			}
		});
	}
	
	
	
	public static void main(String[] args) {
		
	}
}

package com.share.test_3_28;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
	static int t;
	public static void main(String[] args) {
//		final int a = 0;
//		class Tx{
//			String name;
//			int age;
//			Tx(String name,int age){
//				this.name = name;
//				this.age = age;
//			}
//			@Override
//			public String toString(){
//				return name+age;
//			}
//			
//			void f(){
//				System.out.println(a+t);
//			}
//		}
//		
//		Tx tx = new Tx("tangxin",18);
		
		Test t = new Test();
		t.showtxt1(new File("C:\\Users\\Administrator\\Desktop"));
		
		List<Person> list = new ArrayList<Person>();
		list.add(new Person("王小二",10));
		list.add(new Person("李二狗",5));
		list.add(new Person("王二麻子",8));
		list.add(new Person("张三疯",1));
		list.add(new Person("李四",6));
	}
	/**
	 * 写一个方法,显示文件夹下所有的txt文本,用局部内部类实现
	 * @param 需要显示的文件夹
	 */
	public void showtxt(File file){
		class MyFilter implements FilenameFilter{

			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return name.endsWith("txt");
			}
			
		}//这个类不需要或者不想让外部使用
		MyFilter filter = new MyFilter();
		File allfile[] =file.listFiles(filter);
		for (File file2 : allfile) {
			System.out.println(file2.getName());
		}
	}
	/**
	 * 写一个方法,显示文件夹下所有的txt文本,用匿名内部类实现
	 * @param 需要显示的文件夹
	 */
	public void showtxt1(File file) {
		File allfile[] = file.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				// TODO Auto-generated method stub
				return name.endsWith("txt");
			}
		});//针对一次性功能需求一般都会使用匿名内部类
		for (File file2 : allfile) {
			System.out.println(file2.getName());
		}
	}
	/**
	 * 写一个方法,将list集合按学号排序
	 * 用局部内部类实现
	 */
	public void listSort(List<Person> list){
		class Gz implements Comparator<Person>{

			@Override
			public int compare(Person o1, Person o2) {
				// TODO Auto-generated method stub
				return o1.id-o2.id;
			}
			
		}
		Collections.sort(list,new Gz());
	}
	/**
	 * 写一个方法,将list集合按学号排序
	 * 用匿名内部类实现
	 */
	public void listSort1(List<Person> list) {

		Collections.sort(list, new Comparator<Person>() {
			@Override
			public int compare(Person o1, Person o2) {
				// TODO Auto-generated method stub
				return o1.id - o2.id;
			}
		});
	}

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值