javase笔记

hashMap与hashTable的区别:

1、hashMap是不同步的,运行速度快。hashTable是同步的,运行速度慢。

2、hashMap可以存入null的键和值,hashTable不可以。

文件过滤器的使用:

当使用listFiles()方法时,可以添加过滤器过滤列出的文件。

//过滤器
public class MyFilter implements FileFilter{

	@Override
	public boolean accept(File pathname) {
		if(pathname.getName().endsWith(".txt")) {
			//如果是true,放进数组中
			return true;
		}
		return false;
	}

}
//演示
public void test1() throws Exception {
		File file=new File("e:\other");
		File[] files=file.listFiles(new MyFilter());
		for(File f:files) {
			System.out.println(f.getName());
		}
	}

IO字节流之复制:

public class Demo02 {
	public static void main(String[] args) {
		FileInputStream fis=null;
		FileOutputStream fos=null;
		try {
			File file=new File("e:\other\note.txt");
			fis=new FileInputStream(file);
			fos=new FileOutputStream(new File("e:\other\copy.txt"));
			byte[] buf=new byte[1024];
			int len=0;
			while((len=(fis.read(buf)))!=-1) {
                    //System.out.println(new String(buf, 0, len)); fos.write(buf, 0, len); } }catch (IOException e) { e.printStackTrace(); }finally { if(fis!=null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } if(fos!=null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

list进行去重:

ArrayList<Person> al=new ArrayList<Person>();
        al.add(new Person("张三", 34));
        al.add(new Person("李四", 30));
        al.add(new Person("王五", 36));
        al.add(new Person("王五", 36));
        HashSet<Person> hs=new HashSet<Person>(al);
        al=new ArrayList<Person>(hs);

读取properties文件:

 1 public class Demo07 {
 2     public static void main(String[] args) {
 3         FileInputStream fis=null;
 4         FileOutputStream fos=null;
 5         try {
 6             Properties prop=new Properties();
 7             fis=new FileInputStream(new File("e:\other\config.properties"));
 8             prop.load(fis);
 9             //fos具有清空文件的作用 所以读取完后再打开
10             fos=new FileOutputStream("e:\other\config.properties");
11             //Set<String> names = prop.stringPropertyNames();
12             //System.out.println(names);
13             prop.setProperty("score", "100");
14             //System.out.println(prop.getProperty("score"));
15             //prop.setProperty("score", "100");
16             prop.store(fos, "changed");
17         }catch (IOException e) {
18             e.printStackTrace();
19         }finally {
20             
21             try {
22                 if(fis!=null) {
23                     fis.close();
24                 }
25                 if(fos!=null) {
26                     fos.close();
27                 }
28             } catch (IOException e) {
29                 e.printStackTrace();
30             }
31         }
32     }
33 }
原文地址:https://www.cnblogs.com/may12138/p/8416761.html