底层IO使用练习

接IO介绍,此位置返回:IO流(IO|NIO|NIO2)
此文章只是练习,并无学习之处,如有错误还望指正。

package JunitT;

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

import org.junit.Test;

/*
 *	1.往指定位置文件写入数据
 */
public class IOWork1 {
	@Test
	public void readSrc() throws IOException {
		Scanner sc = new Scanner(System.in);
		System.out.print("输入位置:");//E:\我的文档\Eclipse\Day_1130\src\Test1.txt
		String src = sc.next();
		System.out.print("输入内容:");//"Java是一种可以撰写跨平台应用软件的面向对象的程序设计语言,是由Sun Microsystems公司于 1995年5月推出的Java程序
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = br.readLine();
		File dest = new File(src);
		WriteDate(dest, str);

		sc.close();
		br.close();
	}
	public void WriteDate(File dest, String str) throws IOException {
		if(!dest.exists())
			dest.createNewFile();
		Writer out = new FileWriter(dest);
		out.write(str);
		out.close();
	}
}
/*
 *	2. 利用程序读取 test.txt 文件的内容, 并在控制台打印
 */
public class IOWork2 {
	@Test
	public void fileSrc() throws IOException {
		//E:\我的文档\Eclipse\Day_1130\src\Test.txt
		FileInputStream in = new FileInputStream("E:\\我的文档\\Eclipse\\Day_1130\\src\\Test.txt");
		InputStreamReader a = new InputStreamReader(in);
		char[] b = new char[1024];
		while(a.read(b) != -1) {
			for(char a1 : b)
			System.out.print(a1);
		}
		a.close();
	}
}
/**
 *	3. 利用程序复制 test1.txt 为 test2.txt
 */
public class IOWork3 {
	/*
	 * 	 重定向标准输出流,字符的读入,
	 */
	@Test
	public void cpypFile() throws IOException {
		PrintStream ps = new PrintStream(new FileOutputStream("E:\\我的文档\\Eclipse\\Day_1130\\src\\Test2.0.txt"), true);
		System.setOut(ps);
		FileReader in = new FileReader("E:\\\\我的文档\\\\Eclipse\\\\Day_1130\\\\src\\\\Test1.txt");
		char[] b = new char[1024];
		while(in.read(b) != -1) {
			System.out.print(b);
		}
		in.close();
		ps.close();
	}
	/*
	 * 	字节的读入,
	 */
	@Test
	public void cpypFile1() throws IOException {
		FileOutputStream out = new FileOutputStream("E:\\我的文档\\Eclipse\\Day_1130\\src\\Test2.1.txt");
		FileInputStream in = new FileInputStream("E:\\我的文档\\Eclipse\\Day_1130\\src\\Test1.txt");
		byte[] b = new byte[1024];
		int len = 0;
		while((len = in.read(b)) != -1) {
			out.write(b, 0, len);
		}
		in.close();
		out.close();
	}	
}
/*
 *	4. 列出当前目录下全部java文件的名称
 */
public class IOWork4 {
	@Test
	public void test() throws IOException {
		File src = new File("E:\\我的文档\\Eclipse\\Day_1130");
		File dest = new File("E:\\我的文档\\Eclipse\\Day_1201");
		long begin = System.currentTimeMillis();
		fileaimSearch(src, dest);
		long end = System.currentTimeMillis();
		System.out.println(end - begin);
	}
	
	public void fileaimSearch(File src, File dest) throws IOException {
		File[] files = src.listFiles(new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				if(pathname.isDirectory()) {
					try {
						fileaimSearch(pathname, new File(dest, pathname.getName()));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				return pathname.getName().contains(".java");
			}
		});

		for(File i : files) {
			if(i.isDirectory()) {
				File f = new File(dest, i.getName());
				f.mkdirs();
				fileaimSearch(i, f);
			}
			else {
				copySrc_Buffered(i, dest);
			}
		}
	}
	
	public void copySrc(File src, File dest) throws IOException {
		InputStream in = null;
		OutputStream out = null;
		if(!dest.exists())
			dest.mkdirs();
		in = new FileInputStream(src);
		out = new FileOutputStream(dest+"\\"+src.getName());
		byte[] buff = new byte[1024];
		int len = 0;
		while ((len = in.read(buff)) != -1) {
			out.write(buff, 0, len);
		}
		in.close();
		out.close();
		}
	
	public void copySrc_Buffered(File src, File dest) throws IOException {
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		if(!dest.exists())
			dest.mkdirs();
		in = new BufferedInputStream(new FileInputStream(src));
		out = new BufferedOutputStream(new FileOutputStream(dest+"\\"+src.getName()));
		byte[] buff = new byte[1024];
		int len = 0;
		while ((len = in.read(buff)) != -1) {
			out.write(buff, 0, len);
			out.flush();
		}
		in.close();
		out.close();
	}
}
/**
 *	28.使用输入流读取试题文件,每次显示试题文件中的一道题目。读取到字符“*”时暂停读取,等待用户从键盘输入答案。用户做完全部题目后。程序给出用户的得分
 */
public class IOWork28 {
	private int source = 0;
	@Test
	public void readSrc() throws IOException {
		Scanner sc = new Scanner(System.in);
		System.out.print("输入试题位置:");
		String src = sc.next();
		File file = new File(src);//E:\我的文档\Eclipse\Day_1130\src\Test.txt
		testRead(file);
		sc.close();
	}
	
	public void testRead(File file) throws IOException {
		Scanner sc = new Scanner(System.in);
		BufferedReader in = new BufferedReader(new FileReader(file));
		String str = "";
		while((str = in.readLine()) != null) {
			System.out.println(str);
			while(str.contains("*")) {
				System.out.print("输入答案= ");
				if(sc.next().toUpperCase().equals("A")) {
					source += 10;
				}
				str = "";
			}
		}
		System.out.println(source);	
		sc.close();
		in.close();
	}	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值