JAVA 基础 day-24 转换流与缓冲流的练习

124 篇文章 1 订阅
96 篇文章 0 订阅
  • 1.获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录 用缓冲流实现

package exrcise;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Demo1 {
	
	/*
	 * 获取指定目录及子目录下所有txt文件的个数,并将这些txt文件复制到D盘下任意目录 用缓冲流实现
	 * */
	
	
	public static void main(String[] args) throws IOException {
		
		File src = getDir("/home/alex/test");
		
		bufCopy(src, "/home/alex/java");
	}

	private static void bufCopy(File src, String dest) throws IOException {
		File[] fl = src.listFiles(new TxtFilter());
		BufferedReader bufr = null;
		BufferedWriter bufw = null;
		for (File f : fl) {
			if(f.isDirectory()) {
				bufCopy(f, dest);
			}else if(f.isFile()){
				//读取txt文件写到制定目录中
				bufr = new BufferedReader(new FileReader(f.getAbsolutePath()));
				bufw = new BufferedWriter(new FileWriter(new File(dest+"/"+f.getName())));
				String line = null;
				while((line = bufr.readLine())!=null) {
					bufw.write(line);
					bufw.newLine();
					bufw.flush();
				}
				bufr.close();
				bufw.close();
			}
		}
		
	}

	private static File getDir(String src) {
		File path = new File(src);
		if(path.exists() && path.isDirectory()) {
			return path;
		}
		return null;
	}
}


class TxtFilter implements FileFilter{

	@Override
	public boolean accept(File pathname) {
		if(pathname.isDirectory()) {
			return true;
		}else if(pathname.isFile()) {
			if(pathname.getName().endsWith("txt")) {
				return true;
			}
		}
		return false;
	}
	
}


  • 2. 用代码实现以下需求
            (1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
            (2)打印格式:
                to=3
                think=1
                you=2
                //........
            (3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
package exrcise;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

public class Demo2 {
	/*
	 * 2. 用代码实现以下需求
		(1)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)
		(2)打印格式:
			to=3
			think=1
			you=2
			//........
		(3)按照上面的打印格式将内容写入到D:\\count.txt文件中(要求用高效流)
	 * 
	 * */
	public static void main(String[] args) {
		HashMap<String, Integer> map =  function_1();
//		//复习一下遍历map
//		//keySet
//		for (String key : map.keySet()) {
//			System.out.println(key + ":" + map.get(key));
//		}
//		//用entrySet
//		Set<Entry<String, Integer>> set =  map.entrySet();
//		for (Entry<String, Integer> entry : set) {
//			System.out.println(entry.getKey() + ":" + entry.getValue());
//		}
		//iterator 1
//		Set<String> set = map.keySet();
//		Iterator<String> it = set.iterator();
//		while (it.hasNext()) {
//			String key = it.next();
//			System.out.println(key + "=" + map.get(key));
//		}
		//iterator 2
//		Set<Entry<String, Integer>> set = map.entrySet();
//		Iterator<Entry<String, Integer>> it = set.iterator();
//		while (it.hasNext()) {
//			String key = it.next().getKey();
//			System.out.println(key + "=" + map.get(key));
//		}
		
		bufWrite(map, "/home/alex/test/count.txt");
	}
	
	private static void bufWrite(HashMap<String, Integer> map, String dest) {
		BufferedWriter bufw = null;

			try {
				for(String key : map.keySet()) {
					System.out.println(key + "=" + map.get(key));
					//不要忘记在FileWriter中添加append参数,不然每次遍历写入都是重写文件。
					bufw = new BufferedWriter(new FileWriter(dest, true));
					bufw.write(key + "=" + map.get(key));
					bufw.newLine();
					bufw.flush();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				try {
					bufw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
	}

	private static HashMap<String, Integer> function_1() {
		String str = "If you want to change your fate I think you must come to the dark horse to learn java";
		HashMap<String, Integer> map = new HashMap<>();
		String[] strs = str.split(" ");
		for (int i = 0; i < strs.length; i++) {
			String key = strs[i];
			Integer value = 1;
			if(!map.containsKey(key)) {
				map.put(key, value);
			}else {
				value = map.get(key);
				map.put(key, value+1);
			}
		}
		return map;
	}

}

  • 3.利用转换流将GBK格式文件以UTF-8输出

package exrcise;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Demo3 {
	/*
	 * 3.利用转换流将GBK格式文件以UTF-8输出
	 * */
	public static void main(String[] args) throws IOException {
		File src = new File("/home/alex/test/gbk.txt");
		
		writeUtf(src, "/home/alex/test/utf.txt");
	}

	private static void writeUtf(File src, String dest) throws IOException {
		InputStreamReader isr = new InputStreamReader(new FileInputStream(src));
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(dest), "utf-8");
		//用缓冲流优化一下
		BufferedReader bufr = new BufferedReader(isr);
		BufferedWriter bufw = new BufferedWriter(osw);
		String line = null;
		while((line = bufr.readLine())!=null) {
			bufw.write(line);
			bufw.flush();
		}
		bufw.close();
		bufr.close();
	}
}


  •  4.java写一个程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号
        两种方法都试一下,LineNumberReader多了一个获取行号的方法。

package exrcise;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;

public class Demo4 {
	
	
	/*
	 * 4.java写一个程序,实现从文件中读出文件内容,并将其打印在屏幕当中,并标注上行号
	 * */

	public static void main(String[] args) throws IOException {
		
		
		printWithLineNum("/home/alex/test/gbk.txt");
		printWithLineNum2("/home/alex/test/gbk.txt");
	}

	
	
	private static void printWithLineNum2(String src) throws IOException {
		LineNumberReader lnr = new LineNumberReader(new FileReader(src));
		String line = null;
		while((line = lnr.readLine())!=null) {

			System.out.println(lnr.getLineNumber() + "  " + line);
		}
		
		
	}
	private static void printWithLineNum(String src) throws IOException {
		
		BufferedReader bufr = new BufferedReader(new FileReader(src));
		int count = 1;
		String line = null;
		while((line = bufr.readLine())!=null) {
			count++;
			System.out.println(count + "  " + line);
		}
		bufr.close();
		
		
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值