ҳ > 򿪷 > > Java >

javaѧϰ֮· ֮ IO-ϰ

2016-08-27

javaѧϰ֮· ֮ IO-ϰ

package com.atguigu.javae.io; import java.io.FileReader; import java.io.IOException; import org.junit.Test; public class IOTest { @Test public void testReader() { // 1) ,ֵΪnull; FileReader fReader = null; // 2) try catch finally try { // 5) , ͨ fReader = new FileReader("һıļ"); // ԴӵǰĿ¼¿ʼļ // 6) ͨ, int ch = fReader.read(); while (ch != -1) { // 1) Ѿ System.out.print((char)ch); // , Ҳȷ // 2) , ֱ-1Ϊֹ ch = fReader.read(); } } catch (Exception e) { // 4) 쳣 e.printStackTrace(); } finally { // 3) ر if (fReader != null) { try { fReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } } class FileCopy { // ʹļļĸƣÿһζȡһֽ @Test public void test() { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader("./src/com/atguigu/javase/io/Filecopy.java"); fw = new FileWriter("Filecopy.java.bak"); int ch = fr.read(); while (ch != -1) { fw.write(ch); ch = fr.read(); } } catch (Exception e) { e.printStackTrace(); } finally { if (fr != null) { try { fr.close(); } catch (Exception e2) { } } if (fw != null) { try { fw.close(); } catch (Exception e2) { } } } } } class IOTest { // ʹʼ ȡļݣûǰкţȻڿ̨ӡ public static void main(String[] args) { FileReader fr = null; try { fr = new FileReader("./src/com/atguigu/javase/io/IOTest.java"); char[] buf = new char[100]; int ch = fr.read(buf); System.out.println(buf); int count = 1; System.out.print(count++ + " "); while (ch != -1) { for (int i = 0; i < ch; i++) { System.out.print(buf[i]); if (buf[i] == &#39;\n&#39;) { System.out.print(count++ + " "); } } ch = fr.read(buf); } } catch (Exception e) { e.printStackTrace(); } finally { if (fr != null) { try { fr.close(); } catch (Exception e2) { } } } } } class TestCopy { public static void main(String[] args) { FileReader fReader = null; FileWriter fWriter = null; // ʹļļĸƣÿһζȡ100ֽ try { fReader = new FileReader("./src/com/atguigu/javase/io/TestCopy.java"); fWriter = new FileWriter("TestCopy.java.bak"); char[] buf = new char[100]; int realCount = fReader.read(buf); while(realCount != -1) { fWriter.write(buf, 0, realCount); realCount = fReader.read(buf); } } catch (Exception e) { e.printStackTrace(); } finally { if (fReader != null) { try { fReader.close(); } catch (Exception e2) { } } if (fWriter != null) { try { fWriter.close(); } catch (Exception e) { } } } } } class TestFileCopy2 { // ʹûļĸ public static void main(String[] args) { FileReader fReader = null; BufferedReader bReader = null; FileWriter fWriter = null; BufferedWriter bWriter = null; try { fReader = new FileReader("./src/com/atguigu/javase/io/TestCopy.java"); bReader = new BufferedReader(fReader); fWriter = new FileWriter("TestFileCopy2.java.bak2"); bWriter = new BufferedWriter(fWriter); String line = bReader.readLine(); while (line != null) { bWriter.write(line); bWriter.newLine(); line = bReader.readLine(); } } catch (Exception e) { e.printStackTrace(); } finally { if (bReader != null) { try { bReader.close(); } catch (Exception e2) { } } if (bWriter != null) { try { bWriter.close(); } catch (Exception e2) { // TODO: handle exception } } } } } package com.atguigu.javase.io; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Set; import java.util.TreeSet; import org.junit.Test; public class Work { @Test public void work6() { //xmlstudyļеݵظȥ, ндС дļxmlstudy2, ֤ļûȥ, FileReader fileReader = null; BufferedReader bufferedReader = null; FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { fileReader = new FileReader(new File("xmlstudy")); bufferedReader = new BufferedReader(fileReader); fileWriter = new FileWriter(new File("xmlstudy2")); bufferedWriter = new BufferedWriter(fileWriter); Set set = new TreeSet(); String line = null; while ((line = bufferedReader.readLine()) != null) { set.add(line); } for (String string : set) { bufferedWriter.write(string); bufferedWriter.newLine(); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (Exception e2) { // TODO: handle exception } try { bufferedWriter.close(); } catch (Exception e2) { // TODO: handle exception } } } @Test public void work1() { // ʹObject,д50100ڵ, ٶӡ FileOutputStream fos = null; BufferedOutputStream bos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream("50"); bos = new BufferedOutputStream(fos); oos = new ObjectOutputStream(bos); for (int i = 0; i < 50; i++) { oos.writeInt((int)(Math.random() * 100)); } } catch (Exception e) { e.printStackTrace(); } finally { try { oos.close(); } catch (Exception e2) { } } } @Test public void work2() { FileInputStream fis = null; BufferedInputStream bis = null; ObjectInputStream ois = null; try { fis = new FileInputStream("50"); bis = new BufferedInputStream(fis); ois = new ObjectInputStream(bis); for (int i = 0; i < 50; i++) { System.out.println(ois.readInt()); } } catch (Exception e) { e.printStackTrace(); } finally { try { ois.close(); } catch (Exception e2) { } } } //չ д100ڵ, ȫӡ @Test public void work3() { // ʹObject,д50100ڵ, ٶӡ FileOutputStream fos = null; BufferedOutputStream bos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(""); bos = new BufferedOutputStream(fos); oos = new ObjectOutputStream(bos); int rand = (int)(Math.random() * 50); oos.writeInt(rand); for (int i = 0; i < rand; i++) { oos.writeInt((int)(Math.random() * 100)); } } catch (Exception e) { e.printStackTrace(); } finally { try { oos.close(); } catch (Exception e2) { } } } @Test public void work4() { FileInputStream fis = null; BufferedInputStream bis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(""); bis = new BufferedInputStream(fis); ois = new ObjectInputStream(bis); int count = ois.readInt(); for (int i = 0; i < count; i++) { System.out.println(ois.readInt()); } } catch (Exception e) { e.printStackTrace(); } finally { try { ois.close(); } catch (Exception e2) { } } } } 

package com.atgiugu.javase.io;

import java.io.FileReader;
import java.io.FileWriter;

public class FileCopy {
	
	public static void main(String[] args) {
		// FileCopy.javaƳFileCopy.java.bak
		FileReader fReader = null;
		FileWriter fWriter = null;
		try {
			fReader = new FileReader("./src/com/atgiugu/javase/io/FileCopy.java");
			fWriter = new FileWriter("FileCopy.java.bak");
			int ch = fReader.read();
			while (ch != -1) {
				// 1) Ѿ
				fWriter.write(ch);
				// 2) ,ֱ-1
				ch = fReader.read();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fReader != null) {
				try {
					fReader.close();
				} catch (Exception e2) {
				}
			}
			
			if (fWriter != null) {
				try {
					fWriter.close();
				} catch (Exception e2) {
				}
			}
		}
	}
}	

package com.atgiugu.javase.io;

import java.io.FileReader;
import java.io.FileWriter;

public class TextFileCopy {
	
	public static void main(String[] args) {
		//дTextFileCopy.javaڲԷУTextFileCopy.javaΪTextFileCopy.java.bakļ
		FileReader fReader = null;
		FileWriter fWriter = null;
		try {
			//fReader = new FileReader("İ.mp3"); Դļ
			//fWriter = new FileWriter("IJ.mp3");
			fReader = new FileReader("TextFileCopy.java");
			fWriter = new FileWriter("TextFileCopy.java.bak");
			char[] buf = new char[100];
			int realCount = fReader.read(buf);
			while (realCount != -1) {
				// 1) Ѿ
				fWriter.write(buf, 0, realCount);
				// 2) 
				realCount = fReader.read(buf);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fReader != null) {
				try {
					fReader.close();
				} catch (Exception e2) {
				}
			}
			if (fWriter != null) {
				try {
					fWriter.close();
				} catch (Exception e2) {
				}
			}
		}
	}
}

package com.atguigu.javase.work;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.junit.Test;

public class HomeWork {
	
	//ѧ뿼Է¼뵽MapУʾǰɼѧԱ֡
	@Test
	public void work1() {
		Map map = new HashMap();
		map.put("С", 80);
		map.put("С", 70);
		map.put("С", 60);
		map.put("Сǿ", 100);
		map.put("Сΰ", 99);
		map.put("С1", 100);
		map.put("С2", 100);
		map.put("С3", 100);
		map.put("С4", 100);
		map.put("С5", 100);
		
		Set nameSet = map.keySet();
		String[] nameArr = nameSet.toArray(new String[0]);
		for (int i = 0; i < nameArr.length - 1; i++) {
			for (int j = 0; j < nameArr.length - 1 - i; j++) {
				if (map.get(nameArr[j]) > map.get(nameArr[j + 1])) {
					String tmp = nameArr[j];
					nameArr[j] = nameArr[j + 1];
					nameArr[j + 1] = tmp;
				}
			}
		}
		Set high3 = new TreeSet();
		for (int i = nameArr.length - 1; i >= 0 ; i--) {
			if (high3.size() == 3) {
				break;
			}
			high3.add(map.get(nameArr[i]));
		}
		List high3List = new ArrayList();
		high3List.addAll(high3);
		int no1count = 0;
		int no2count = 0;
		for (int i = nameArr.length - 1; i >= 0 ; i--) {
			if (map.get(nameArr[i]) == high3List.get(2)) {
				System.out.println(nameArr[i] + " : " + high3List.get(2));
				no1count++; // ߷ָ++
			} else if (map.get(nameArr[i]) == high3List.get(1)) {
				if (no1count >= 3) { // ߷ָѾ3 жϵڶ߷ֵͬѧĴӡ
					break;
				}
				System.out.println(nameArr[i] + " : " + high3List.get(1));
				no2count++; // ڶ߷ּ
			} else if (map.get(nameArr[i]) == high3List.get(0)) {
				if (no1count + no2count >= 3) {
					break;
				}
				System.out.println(nameArr[i] + " : " + high3List.get(0));
			}
		}
		//System.out.println(nameArr[nameArr.length - 1] + " : " + map.get(nameArr[nameArr.length - 1]));
		//System.out.println(nameArr[nameArr.length - 2] + " : " + map.get(nameArr[nameArr.length - 2]));
		//System.out.println(nameArr[nameArr.length - 3] + " : " + map.get(nameArr[nameArr.length - 3]));
	}
	
	@Test
	public void work2() {
		Map map = new HashMap();
		map.put("С", 80);
		map.put("С", 70);
		map.put("С", 60);
		map.put("Сǿ", 100);
		map.put("Сΰ", 99); // 99Сΰ
		map.put("С", 100);// 100С
		
		List list = new ArrayList();
		Set name = map.keySet();
		for (String n : name) {
			int score = map.get(n);
			list.add((char)score + n);
		}
		Collections.sort(list);
		Collections.reverse(list);
		System.out.println((int)list.get(0).charAt(0) + " : " + list.get(0).substring(1));
		System.out.println((int)list.get(1).charAt(0) + " : " + list.get(1).substring(1));
		System.out.println((int)list.get(2).charAt(0) + " : " + list.get(2).substring(1));
	}
}

package com.guigu.javase.Object;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.junit.Test;

@SuppressWarnings("serial")
class Student implements Serializable{
	
	private int id;
	private String name;
	private double score;
	
	public Student() {
	}

	public Student(int id, String name, double score) {
		super();
		this.id = id;
		this.name = name;
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", score=" + score
				+ "]";
	}
}

public class IOTest {
	// ʹö3ѧSet϶дļлļ
	@Test
	public void test1() {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		
		try {
			fos = new FileOutputStream("лļ");
			oos = new ObjectOutputStream(fos);
			
			Student s1 = new Student(1, "С", 90);
			Student s2 = new Student(2, "С", 89);
			Student s3 = new Student(3, "С", 98);
			
			Set set = new HashSet();
			set.add(s1);
			set.add(s2);
			set.add(s3);
			
			oos.writeObject(set);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (Exception e2) {
			}
		}
	}
	
	// ļڵĶȡڿ̨ӡ
	@Test
	public void test2() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		
		try {
			fis = new FileInputStream("лļ");
			ois = new ObjectInputStream(fis);
			
			@SuppressWarnings("unchecked")
			Set set = (Set)ois.readObject(); 
			Iterator iterator = set.iterator();
			while (iterator.hasNext()) {
				System.out.println(iterator.next());
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	// ʹתַUTF8д롰תдļļ
	@Test
	public void test3() {
		FileOutputStream fis = null;
		OutputStreamWriter osw = null;
		
		try {
			fis = new FileOutputStream("תдļ");
			osw = new OutputStreamWriter(fis,"UTF8");
			
			osw.write("abdcssd\n");
			osw.write("дʲô?");
			osw.write(10);
			osw.write("!!");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				osw.close();
			} catch (Exception e2) {
			}
		}
	}
	
	// ʹתתдļеַUTF8ʽڿ̨ϴӡ
	@Test
	public void test4() {
		FileInputStream fis = null;
		InputStreamReader isr = null;
		
		try {
			fis = new FileInputStream("תдļ");
			isr = new InputStreamReader(fis,"UTF8");
			
			int count = isr.read();
			while (count != -1) {
				
				System.out.print((char)count);
				count = isr.read();
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				isr.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	ȡַڿ̨ӡʾ
	@Test
	public void test5() {
		InputStreamReader isr = null;
		BufferedReader brReader = null;
		FileWriter fw = null;
		BufferedWriter brWriter = null;
		try {
			isr = new InputStreamReader(System.in); //תȡַ
			brReader = new BufferedReader(isr); // 
			fw = new FileWriter("nums.txt");  // ļ
			brWriter = new BufferedWriter(fw); // 
			
			for (int i = 0; i < 10;i++) {
				String num = brReader.readLine();
				try {
					Double.parseDouble(num);
					brWriter.write(num);
					brWriter.newLine();
				} catch (NumberFormatException e) {
					System.out.println("ִ!!");
					i--;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				brReader.close();
			} catch (Exception e2) {
			}
			try {
				brWriter.close();
			} catch (Exception e2) {
			}
		}
	}
	
	// ļϰ
	@Test
	public void test6() {
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile("ļ", "rw");
			for (int i = 0; i < 26; i++) {
				raf.write(&#39;a&#39; + i);
			}
			raf.seek(0);
			for (int i = 0; i < 10; i++) {
				raf.write(&#39;A&#39; + i);
			}
			raf.seek(raf.length() - 10);
			for (int i = 0; i < 10; i++) {
				raf.write(&#39;0&#39; + i);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (Exception e2) {
			}
		}
	}
	
	// Fileϰ
	@Test
	public void test7() {
		File file = new File(".");
		File file1 = new File("xxx");
		
		String[] childFileName = file.list();
		for (int i = 0; i < childFileName.length; i++) {
			System.out.println(childFileName[i]);
		}
		
		file1.mkdir();
		File file2 = new File("xxx/file.info");
		FileWriter fWriter = null;
		try {
			fWriter = new FileWriter(file2);
			File[] childFiles = file.listFiles();
			for (int i = 0; i < childFiles.length; i++) {
				fWriter.write(childFiles[i].getName() + ",");;
				fWriter.write(childFiles[i].length() + "\n");;
				System.out.print(childFiles[i].getAbsolutePath());
				System.out.println(childFiles[i].length());
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			try {
				fWriter.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
	}
}

package com.atguigu.javase.io;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.junit.Test;

class Person implements Serializable {
	
	private static final long serialVersionUID = -402328197027L;

	public static int no = 100; // ̬ԲԱл
	
	private String name;
	private transient int age; // transientǶݵ,˲ʱ. Ҳܱл
	private String gender;
	//private double weight;
	
	public Person() {
	}

	public Person(String name, int age, String gender) {
		super();
		this.name = name;
		this.age = age;
		this.gender = gender;
	}

	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", gender=" + gender
				+ "]";
	}
	
}

/**
 * дһStudent , id, name, score
 * лһ3, лһ4List
 * ٷл,ӡ
 */
public class IOTest {
	
	@Test
	public void exer5() {
		// Ŀ¼xxx
		File file = new File("xxx");
		file.mkdir();
		// гǰĿ¼µļĿ¼, ļļļϢдļxxx/file.info
		File currentDir = new File(".");
		File[] childFiles = currentDir.listFiles();
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter("xxx/file.info");
			for (int i = 0; i < childFiles.length; i++) {
				System.out.println(childFiles[i]);
				fileWriter.write(childFiles[i].getName() + "," + childFiles[i].length());
				fileWriter.write(10);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				fileWriter.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
		
	}
	
	@Test
	public void testFile2() throws IOException {
		File file = new File("abc");
		System.out.println("file.mkdir():" + file.mkdir());
		System.out.println("file.canRead():" + file.canRead());
		System.out.println("file.getAbsolutePath():" + file.getAbsolutePath());
		System.out.println("file.getFreeSpace():" + file.getFreeSpace());
		System.out.println("file.getTotalSpace():" + file.getTotalSpace());
		System.out.println("file.lastModified():" + file.lastModified());
		System.out.println("file.length():" + file.length());
		System.out.println("file.canWrite():" + file.canWrite());
		System.out.println("file.createNewFile():" + file.createNewFile());
		System.out.println("file.exists():" + file.exists());
		System.out.println("file.isDirectory():" + file.isDirectory());
		System.out.println("file.isFile():" + file.isFile());
		// Ŀ¼еķ
		System.out.println("------------------------");
		String[] childFileNames = file.list();
		for (int i = 0; i < childFileNames.length; i++) {
			System.out.println(childFileNames[i]);
		}
		System.out.println("------------------------");
		// ص㷽
		File[] childFiles = file.listFiles();
		for (int i = 0; i < childFiles.length; i++) {
			System.out.println(childFiles[i].length() + " " + childFiles[i].getAbsolutePath());
		}
		
	}
	@Test
	public void testFile() throws IOException {
		File file = new File("nums.txt");
		System.out.println("file.canRead():" + file.canRead());
		System.out.println("file.getAbsolutePath():" + file.getAbsolutePath());
		System.out.println("file.getFreeSpace():" + file.getFreeSpace());
		System.out.println("file.getTotalSpace():" + file.getTotalSpace());
		System.out.println("file.lastModified():" + file.lastModified());
		System.out.println("file.length():" + file.length());
		System.out.println("file.canWrite():" + file.canWrite());
		System.out.println("file.createNewFile():" + file.createNewFile());
		System.out.println("file.exists():" + file.exists());
		System.out.println("file.isDirectory():" + file.isDirectory());
		System.out.println("file.isFile():" + file.isFile());
		//System.out.println("file.delete():" + file.delete());
	}
	
	
	// дһļ, д26Сдĸ
	@Test
	public void exer3() {
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile("ļ2", "rw");
			for (int i = 0; i < 26; i++) {
				raf.write(&#39;a&#39; + i); // 0x00000061
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	// ٴļ, ǰ10ɴдĸ,10滻Ϊַ
	@Test
	public void exer4() {
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile("ļ2", "rw");
			//abcdefghijklmn...xyz
			raf.seek(0);
			for (int i = 0; i < 10; i++) {
				raf.write(&#39;A&#39; + i);
			}
			raf.seek(raf.length() - 10);
			for (int i = 0; i < 10; i++) {
				raf.write(&#39;0&#39; + i);
			}
			//raf.setLength(1024 * 1024 * 1024);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	@Test
	public void testRandomAccessFile() {
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile("ļ", "rw"); // Զдʽļ, ָָ0±괦
			char ch = (char)raf.read(); // һֽ, һֽһӢַ, ָҪӦƶ
			System.out.println(ch);
			//raf.readChar();
			System.out.println("pointer:" + raf.getFilePointer());
			
			raf.seek(4);
			byte[] buf = new byte[8192];
			int realCount = raf.read(buf, 0, 4); // ֻ4ֽ
			String string = new String(buf, 0, realCount);
			System.out.println(string);
			
			raf.seek(20);
			realCount = raf.read(buf, 0, 3); // ֻ3ֽ
			String string2 = new String(buf, 0, realCount);
			System.out.println(string2);
			
			raf.seek(8);
			raf.writeUTF("XXXX");
			//raf.write(&#39;a&#39;); һַֽдļ

			
			// ļ,ȷ
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				raf.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	@Test
	public void testSystemOut2() throws FileNotFoundException {
		System.out.println("aaaaa");
		System.err.println("bbbbb"); // System.errеķһ߳
	}
	
	@Test
	public void testSystemOut() throws FileNotFoundException {
		//System.setOut(new PrintStream(new FileOutputStream("sysout"))); // ض
		//System.out.println("abcdefg");
		
		System.setErr(new PrintStream(new FileOutputStream("syserr"))); //Ѵضļ
		System.err.println("xxxxxxxxx");
		try {
			String string = null;
			System.out.println(string.length());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	// Ӽ̻ȡ10, 10ֱļnums.txt.
	@Test
	public void exer1() {
		// ȡ벻,дļ.
		InputStreamReader isr = null;
		BufferedReader bufferedReader = null;
		FileWriter fileWriter = null;
		BufferedWriter bufWriter = null;
		try {
			isr = new InputStreamReader(System.in);
			bufferedReader = new BufferedReader(isr);
			fileWriter = new FileWriter("nums.txt", true); // ׷ӵķʽдļ
			bufWriter = new BufferedWriter(fileWriter);
			
			for (int i = 0; i < 10; i++) {
				String str = bufferedReader.readLine();
				try {
					Double.parseDouble(str);
					bufWriter.write(str);
					bufWriter.newLine();
				} catch (NumberFormatException e) {
					System.out.println("!!");
					i--;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				bufferedReader.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
			try {
				bufWriter.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	// ʹתȡַڿ̨ӡ
	@Test
	public void testSystemIn() {
		//System.out.println(System.in);
		InputStreamReader isr = null;
		BufferedReader bufReader = null;
		try {
			isr = new InputStreamReader(System.in); // System.inһ, JVMԶ, ֻҪʹü
			bufReader = new BufferedReader(isr);
			String line = bufReader.readLine();
			while (line != null) {
				// 1)
				System.out.println(line);
				// 2) 
				line = bufReader.readLine(); // ctrl+zʹ÷null
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				bufReader.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	@Test
	public void testOutputStreamWriter() {
		FileOutputStream fos = null;
		OutputStreamWriter osw = null;
		try {
			fos = new FileOutputStream("ʹתдıļ3", true);
			//osw = new OutputStreamWriter(fos); // Աر뷽ʽдıļ
			osw = new OutputStreamWriter(fos, "UTF8"); // ָ뷽ʽдıļ
			osw.write("һַ");
			osw.write(10);
			osw.write("abcdefgh\n");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				osw.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	// дһıļ,ʹUTF8뷽ʽ, ü±򿪲鿴
	// ٶȡļݲӡ
	// һʹISO8859-1뷽ʽ,дļļ
	@Test
	public void testInputStreamReader() {
		//FileReader fReader = null;
		FileInputStream fis = null;
		InputStreamReader isr = null;
		BufferedReader bufReader = null;
		try {
			//fReader = new FileReader("utf8ıļ.txt"); // ֻʹĬϵGBKʽıļ
			fis = new FileInputStream("ʹתдıļ2");
			//isr = new InputStreamReader(fis); // ĬϰGBKʽֽתΪַ
			isr = new InputStreamReader(fis, "UTF8"); // UTF8뷽ʽֽתΪַ
			bufReader = new BufferedReader(isr);
			String line = bufReader.readLine(); // ֱӴжһַ, lineвκλ
			while (line != null) {
				// 1) 
				System.out.println(line);
				// 2) 
				line = bufReader.readLine();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// ر, ֻҪرո߼
			if (bufReader != null) {
				try {
					bufReader.close();
				} catch (Exception e2) {
				}
			}
		}
	}
	
	// , л : GCжݱ浽.
	// ֱлʵSeriliableӿڵĶ, л, 󼯺
	// ллʱİ汾Ҫһ.
	@Test
	public void testSerialize() {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream("лļ");
			oos = new ObjectOutputStream(fos);
			// л
			Person.no = 1000;
			Person obj1 = new Person("", 30 , "");
			Person obj2 = new Person("", 40 , "Ů");
			Person obj3 = new Person("", 50 , "");
			
			Person[] arr = {obj1, obj2, obj3};
			
			/*
			oos.writeObject(obj1);
			oos.writeObject(obj2);
			oos.writeObject(obj3);
			*/
			
			//oos.writeObject(arr);
			
			Set set = new HashSet();
			set.add(obj1);
			set.add(obj2);
			set.add(obj3);
			
			oos.writeObject(set);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				oos.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
	
	// л : жݻԭɶĹ
	@Test
	public void testDeSerialize() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream("лļ");
			ois = new ObjectInputStream(fis);
			/*
			Person person1 = (Person)ois.readObject();
			System.out.println(person1);
			Person person2 = (Person)ois.readObject();
			System.out.println(person2);
			Person person3 = (Person)ois.readObject();
			System.out.println(person3);
			*/
			
			/*
			Person[] arr = (Person[])ois.readObject();
			for (int i = 0; i < arr.length; i++) {
				System.out.println(arr[i]);
			}
			*/
			Set set = (Set)ois.readObject();
			Iterator iterator= set.iterator();
			while (iterator.hasNext()) {
				Person person = iterator.next();
				System.out.println(person);
			}
			System.out.println(Person.no);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {
			try {
				ois.close();
			} catch (Exception e2) {
				// TODO: handle exception
			}
		}
	}
}

package com.atguigu.javase.work;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

import org.junit.Test;
// Ӽ̶ȡһַжǸַȻڿ̨ϴӡ
public class HomeWork {
	
	@Test
	public void test3() {
		Scanner scanner = null;
		try {
			scanner = new Scanner(System.in);
			/*
			while (scanner.hasNextLine()) {
				String string = scanner.nextLine();
				System.out.println(string);
			}*/
			// aaa 111 222.3 bbb ccc 999
			while (scanner.hasNext()) {
				if (scanner.hasNextInt()) {
					int num = scanner.nextInt();
					System.out.println(":" + num);
				} else if (scanner.hasNextDouble()) {
					double value = scanner.nextDouble();
					System.out.println(":" + value);
				} else {
					String string = scanner.next();
					System.out.println("ַͨ:" + string);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			scanner.close();
		}
	}
	
	// Ӽ̶ȡַڿ̫ӡ
	@Test
	public void test2() throws IOException {
		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
		String line = bufferedReader.readLine();
		while (line != null) {
			System.out.println(line);
			line = bufferedReader.readLine();
		}
		bufferedReader.close(); // һر,޷ʹ
	}
	
	@Test
	public void work1() {
		String path = "c:/MyWork";
		File file = new File(path);
		deleteFile(file);
	}
	
	// ɾļмĿ¼ļ
	public void deleteFile(File file) {
		if (file.isFile()) {
			file.delete();
		} else {
			File[] childFiles = file.listFiles();
			for (int j = 0; j < childFiles.length; j++) {
				deleteFile(childFiles[j]);
			}
			file.delete();
			System.out.println(file + " Ŀ¼ɾɹ!");
		}
	}
}




		
ȵƼ