欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

第十二章:IO流

程序员文章站 2022-04-09 17:29:26
...
	/*
	 * 将读取文件内容的gbk的编码集在写入文件时转成UTF-8编码集
	 * 注意 : 读取文件内容的编码集和InputStreamReader中设置的编码集必须相同
	 */
	@Test
	public void test15() throws Exception{
		FileInputStream fileInputStream = new FileInputStream("123.txt");
		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
		FileOutputStream fileOutputStream = new FileOutputStream("888.txt");
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream,"utf-8");
		char[] c = new char[1024];
		int read = 0;
		while((read = inputStreamReader.read(c)) != -1){
			System.out.println(new String(c,0,read));
			outputStreamWriter.write(c,0,read);
			outputStreamWriter.write("abc爱我中华");
		}
		inputStreamReader.close();
		outputStreamWriter.close();
		fileInputStream.close();
		fileOutputStream.close();
	}
	/*
	 * 转换流
	 * 作用:
	 * 1.可以对字节流和字符流进行转换
	 * 2.可以改变写出内容的编码集
	 * 
	 */
	@Test
	public void test14() throws Exception{
		FileInputStream fileInputStream = new FileInputStream("123.txt");
		InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
		FileOutputStream fileOutputStream = new FileOutputStream("111000.txt");
		OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
		char[] c = new char[1024];
		int read = 0;
		System.out.println("--------");
		while((read = inputStreamReader.read(c)) != -1){
			System.out.println(new String(c,0,read));
			outputStreamWriter.write(c,0,read);
			
		}
		inputStreamReader.close();
		outputStreamWriter.close();
		fileInputStream.close();
		fileOutputStream.close();
	}
	/*
	 * 写内容
	 * 
	 */
	@Test
	public void test13() throws Exception{
		FileWriter writer = new FileWriter("999.txt");
		writer.write("中国我爱你哈哈哈哈");
		writer.close();
	}
	/*
	 * 字符流 : 不能用来复制文件。
	 * FileReader
	 * 读取文件内容
	 */
	@Test
	public void test12() throws Exception{
		FileReader reader = new FileReader("5656.txt");
		char[] c = new char[100];
		int len = 0;
		while((len = reader.read(c)) != -1){
			System.out.println(new String(c,0,len));
		}
		reader.close();
	}
	/**
	 * 中文乱码
	 * @throws Exception
	 */
	@Test
	public void test11() throws Exception{
			//1.创建File对象
			File file = new File("5656.txt");
			//2.创建流的对象
			FileInputStream fis = new FileInputStream(file);
			//3.读取内容
			byte[] b = new byte[20]; //用来装数据
			
			int len = 0; //读取的内容的长度
			
			while((len = fis.read(b)) != -1){
				
				for (byte c : b) {
					System.out.println((char)c);
				}
			}
			//4.关流
			fis.close();
	}
@Test
	public void test10(){
		System.out.println("");
		long stat = System.currentTimeMillis();
		/*try {
			copy1("E:\\录屏视频\\自己类库的创建与API文档生成及注意事项.mp4","C:\\Users\\Administrator\\Desktop\\a.map4");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long end = System.currentTimeMillis();
		System.out.println(end - stat);//64313
*/		System.out.println("-------------------");
		long currentTimeMillis = System.currentTimeMillis();
		/*try {
			copy2("E:\\录屏视频\\自己类库的创建与API文档生成及注意事项.mp4","C:\\Users\\Administrator\\Desktop\\a.map4");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		long currentTimeMillis2 = System.currentTimeMillis();
		long sum = currentTimeMillis2 - currentTimeMillis;
		System.out.println(sum);//52222
	}
	public void copy2(String start,String end) throws Exception{
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		BufferedInputStream bufferedInputStream = null;
		BufferedOutputStream bufferedOutputStream = null;
		try {
			File file = new File(start);
			File file2 = new File(end);
			fileInputStream = new FileInputStream(file);
			fileOutputStream = new FileOutputStream(file2);
			bufferedInputStream = new BufferedInputStream(fileInputStream);
			bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
			byte[] b = new byte[20];
			int read = 0;
			while((read = bufferedInputStream.read(b)) != -1){
				System.out.println(new String(b,0,read));
				bufferedOutputStream.write(b,0,read);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{			
			try {
				bufferedInputStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				bufferedOutputStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fileInputStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				fileOutputStream.close();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void copy1(String start,String end) throws Exception{
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			File file = new File(start);
			File file2 = new File(end);
			fileInputStream = new FileInputStream(file);
			fileOutputStream = new FileOutputStream(file2);
			byte[] b = new byte[20];
			int read = 0;
			while((read = fileInputStream.read(b)) != -1){
				System.out.println(new String(b,0,read));
				fileOutputStream.write(b, 0, read);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			fileInputStream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			fileOutputStream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
//缓存流
@Test
public void test9() throws Exception{
   File file = new File("456.txt");
   FileOutputStream fileOutputStream = new FileOutputStream(file);
   BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
   bufferedOutputStream.write("ssss".getBytes());
   bufferedOutputStream.close();
   fileOutputStream.close();
}
	@Test
	public void test8() throws Exception{
		File file = new File("123.txt");
		file.createNewFile();
		FileInputStream fileInputStream = new FileInputStream(file);
		BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
		byte[] b = new byte[10];
		int read = 0;
		while((read = bufferedInputStream.read(b)) != -1){
			System.out.println(new String(b,0,read));
		}
		bufferedInputStream.close();
		fileInputStream.close();//关流 : 先关外面的再关里面的
	}
	//FileOutputStream : 向文件中写内容
	/**
	 * 复制图片
	 * @throws Exception
	 */
	@Test
	public void test7() throws Exception{
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			File file = new File("1.jpg");
			fileInputStream = new FileInputStream(file);
			fileOutputStream = new FileOutputStream(new File("6.jpg"));
			byte[] b = new byte[1024];
			int read = 0;
			while((read = fileInputStream.read(b)) != -1){
				System.out.println(new String(b,0,read));
				fileOutputStream.write(b,0,read);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{			
			try {
				if(fileInputStream != null){					
					fileInputStream.close();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(fileOutputStream != null){					
					fileOutputStream.close();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	/**
	 * 一边读一边写
	 * @throws Exception
	 */
	@Test
	public void test6() throws Exception{
		FileInputStream fileInputStream = null;
		FileOutputStream fileOutputStream = null;
		try {
			File file = new File("123.txt");
			fileInputStream = new FileInputStream(file);
			fileOutputStream = new FileOutputStream(new File("456.txt"));
			byte[] b = new byte[20];
			int read = 0;
			while((read = fileInputStream.read(b)) != -1){
				System.out.println(new String(b,0,read));
				fileOutputStream.write(b,0,read);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{			
			try {
				if(fileInputStream != null){					
					fileInputStream.close();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				if(fileOutputStream != null){					
					fileOutputStream.close();
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
/**
	 * 写文件
	 * @throws Exception
	 */
	@Test
	public void test5() throws Exception{
		File file = new File("123.txt");
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		fileOutputStream.write("abc".getBytes());
		fileOutputStream.close();
	}
	/**
	 * try-catch-finally
	 */
	@Test
	public void test4(){
		FileInputStream fileInputStream = null;
		try {
			File file = new File("123.txt");
			fileInputStream = new FileInputStream(file);
			byte[] b = new byte[10];
			int read = 0;
			while((read = fileInputStream.read(b)) != -1){
				System.out.println(new String(b,0,read));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				if(fileInputStream != null){					
					fileInputStream.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
//FileInputStream : 从文件中读取内容
	/**
	 * 第一种方式
	 * @throws Exception
	 */
	@Test
	public void test1() throws Exception{
		File file = new File("123.txt");
		FileInputStream fileInputStream = new FileInputStream(file);
	    int read = fileInputStream.read();
	    while(read != -1){
	    	System.out.println((char)read);
	    	read = fileInputStream.read();
	    }
	    fileInputStream.close();
	}
	/**
	 * 第二种方式
	 * @throws Exception
	 */
	@Test
	public void test2() throws Exception{
		File file = new File("123.txt");
		FileInputStream fileInputStream = new FileInputStream(file);
		byte[] b = new byte[20];
		int read = fileInputStream.read(b);
		while(read != -1){
			/*for (int i = 0; i < b.length; i++) {				
				//System.out.print(b[i] + " ");//97 98 99 100 101 102 103 0 0 0 0 0 0 0 0 0 0 0 0 0 
				System.out.print((char)b[i] + " ");//a b c d e f g 
			}*/
			for (byte c : b) {				
				System.out.print((char)c + " ");//a b c d e f g 
			}
			String string = new String(b,0,read);//将数组转成字符串
			System.out.println(string);
			read = fileInputStream.read(b);
		}
		fileInputStream.close();
	}
	/**
	 * 第三种方式
	 * @throws Exception
	 */
	@Test
	public void test3() throws Exception{
		File file = new File("123.txt");
		FileInputStream fileInputStream = new FileInputStream(file);
		byte[] b = new byte[10];
		int read = 0;
		while((read = fileInputStream.read(b)) != -1){
			System.out.println(new String(b,0,read));
		}
		fileInputStream.close();
	}
相关标签: IO