java中的字节流和字符流

字节输入流InputStream

Io程序的操作步骤

  • 1.创建源
  • 2.选择流
  • 3.操作
  • 4.释放资源

InputStream,OutputStream是可用于处理,字符文件,视频,语音,图片等文件

public class TestIO {

	public static void main(String[] args) {
		//1.创建源
		File src=new File("abc.txt");
		//2.选择流,选择文件输入流
		InputStream is=null;//方便在finally中使用,设置为全局变量
		try {
			is=new FileInputStream(src);
//单字节读取
//		int data1=is.read();//第一个数据h
//		int data2=is.read();//e
//		int data3=is.read();//l
//		System.out.println((char)data1);
//		System.out.println((char)data2);
//		System.out.println(data3);
			//3.操作,读文件,分段读取
			byte[] flush=new byte[1024*10];//10k,创建读取数据时的缓冲,每次读取的字节个数。
			int len=-1;//接受长度;
			while((len=is.read(flush))!=-1) {
				//表示当还没有到文件的末尾时
				//字符数组-->字符串,即是解码。
				String str=new String(flush,0,len);//len是读到的实际大小
				System.out.println(str);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//4.释放资源
			if(is!=null) {//表示当文打开时,才需要通知操作系统关闭
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		}
		
	}

字节输出流OutputStream

步骤:

  • 1.创建源
  • 2.选择流
  • 3.操作
  • 4.释放资源
public class TestOutputStream {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//1.创建源
		File dest=new File("dest.txt");//该文件是不存在的,会在相应的路径下创建
		// 2.选择流
		OutputStream os=null;
		try {
			//3.操作
			//os=new FileOutputStream(dest,false);
			//后面的false和true表示的是重头开始写还是在文件后面进行追加。
			
			os=new FileOutputStream(dest,true);
			
			//将内容写出
			String smg="java welcome!\r\n";
			byte[] datas=smg.getBytes();//将字符创转化成字节数组
			//将内容写入
			os.write(datas,0,datas.length);//
			os.flush();//表示刷新缓冲,避免数据驻留在内存中,一般在输出数据的时候都要将数据刷新。
			
			
		}catch(FileNotFoundException e){
			e.printStackTrace();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//4.释放资源
			try {
				if (null != os) {
					os.close();
				} 
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

}

文件的拷贝就是将输入和输出通过程序进行中转,然后将源文件和目标文件进行对接, 即可实现文件的拷贝。

public class CopyFile {
	
	public static void main(String[] args) {
		String src="D:/java/myball/src/javaIO/TestIO.java";
		String dest="copy.txt";
		copy(src,dest);
	}
		public static void copy(String srcPath,String destPath) {
	
	//1.创建源
			File src=new File(srcPath);
			File dest=new File(destPath);
			//2.选择流,选择文件输入流
			InputStream is=null;//方便在finally中使用,设置为全局变量
			OutputStream os=null;
			try {
				is=new FileInputStream(src);
				os=new FileOutputStream(dest);
				//3.操作,读文件
				byte[] flush=new byte[1024];//10k,创建读取数据时的缓冲,每次读取的字节个数。
				int len=-1;//接受长度;
				while((len=is.read(flush))!=-1) {
					//表示当还没有到文件的末尾时
					//字符数组-->字符串,即是解码。
					os.write(flush,0,len);//
					os.flush();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				//4.释放资源,依次关闭,先打开的后关闭
				try {
					if (null != os) {
						os.close();
					} 
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				//表示当文打开时,才需要通知操作系统关闭
					try {
						if(is!=null) {
						is.close();
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
			}
	}		
}

文件夹的拷贝

public class CopyFile {
	
	public static void main(String[] args) {
		String srcPath="D:/java/myball/src/javaIO";
		String destPath="D:";
		checked(srcPath,destPath);
		
	}
		public static void checked(String srcPath,String destPath) {
		File src=new File(srcPath);
		
		String name=src.getName();
		String destPath1=destPath+File.separator+name;
		
		if(src.isDirectory()) {
			createFiles(destPath1);
		}
		
		File[] files=src.listFiles();
		if(files!=null) {
			for(File s:files) {
				if(s!=null) {
					if(s.isDirectory()) {
						checked(s.getAbsolutePath(),destPath1);
					}else {
						copyFile(s.getAbsolutePath(),destPath1+File.separator+s.getName());
					}
				}
		}	
	}
}
		
		public static void createFiles(String destPath) {
			File dest=new File(destPath);
			dest.mkdirs();
		}
		

	
	public static void copyFile(String srcPath,String destPath) {
	
	//1.创建源
			File src=new File(srcPath);
			File dest=new File(destPath);
			//2.选择流,选择文件输入流
			InputStream is=null;//方便在finally中使用,设置为全局变量
			OutputStream os=null;
			try {
				
				is=new FileInputStream(src);
				os=new FileOutputStream(dest);
				//3.操作,读文件
				byte[] flush=new byte[1024];//10k,创建读取数据时的缓冲,每次读取的字节个数。
				int len=-1;//接受长度;
				while((len=is.read(flush))!=-1) {
					//表示当还没有到文件的末尾时
					//字符数组-->字符串,即是解码。
					os.write(flush,0,len);//
					os.flush();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				//4.释放资源,依次关闭,先打开的后关闭
				try {
					if (null != os) {
						os.close();
					} 
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}
				//表示当文打开时,才需要通知操作系统关闭
					try {
						if(is!=null) {
						is.close();
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				
			}
	}		
}

字符输出流FileWriter

  • FileReader:通过字符的方式读取文件,
  • FileWriter:通过字符的方式写入或者追加数据到文件中,仅适合于字符文件(即是字符串)。

java中用字符集输出流写文件,FileWriter将内容写入文件。

public class TestFileWrite {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
				//1.创建源
				File dest=new File("dest1.txt");//该文件是不存在的,会在相应的路径下创建
				// 2.选择流
				FileWriter os=null;
				try {
					//3.操作
					//os=new FileOutputStream(dest,false);
					//后面的false和true表示的是重头开始写还是在文件后面进行追加。
					
					os=new FileWriter(dest,true);
					
					//将内容写出
					String smg="java welcome!\r\n欢迎学习java";
					//将内容写入
					os.write(smg);//可以直接将字符串写入文件
					os.flush();//表示刷新缓冲,避免数据驻留在内存中,一般在输出数据的时候都要将数据刷新。
					
					
					//区别writer和appended的区别,
				//writer:每次只可以写入一次信息。
					//appended:可以在后面通过“.”的方式不断加入内容。
					
					os.append("enjoy youself!").append("开心");
					
				}catch(FileNotFoundException e){
					e.printStackTrace();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					//4.释放资源
					try {
						if (null != os) {
							os.close();
						} 
					} catch (Exception e) {
						// TODO: handle exception
					}
				}

	}

}

字符输入流FileReader

java中将文件中的内容读出来FileReader,不可以用来读取非字符的文件。

public class TestFileWrite {

	public static void main(String[] args) {
		//1.创建源
		File src=new File("dest1.txt");
		//2.选择流,选择文件输入流
		Reader is=null;//方便在finally中使用,设置为全局变量
		try {
			is=new FileReader(src);
			//3.操作,读文件
			char[] flush=new char[1024*10];//10k,创建读取数据时的缓冲,每次读取的字节个数。
			int len=-1;//接受长度;
			while((len=is.read(flush))!=-1) {
				//表示当还没有到文件的末尾时
				//不用进行解码了,直接将字符数组读出即可。
				System.out.println(flush);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//4.释放资源
			if(is!=null) {//表示当文打开时,才需要通知操作系统关闭
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
		}
		
	}


封装思想

public class FileUtil {

	public static void main(String[] args) {
		
		//文件到文件。
		try {
			InputStream is=new FileInputStream("abc.txt");
			OutputStream os=new FileOutputStream("abc_copy.txt");
			long t1=System.currentTimeMillis();
			copy(is,os);
			long t2=System.currentTimeMillis();
			System.out.println(t2-t1);
		} catch (IOException e) {//统一调用父类的异常
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		//文件到字节数组。
		byte[] datas=null;
				try {
					InputStream is=new FileInputStream("copy.jpg");
					ByteArrayOutputStream os=new ByteArrayOutputStream();
					copy(is,os);//ByteArrayOutputStream方法会自动返回一个字节数组
					datas=os.toByteArray();
					System.out.println(datas.length);
				} catch (IOException e) {//统一调用父类的异常
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
				
		//字节数组到文件。
		try {
			
			InputStream is=new ByteArrayInputStream(datas);
			OutputStream os=new FileOutputStream("copy_byte1.jpg");
			copy(is,os);
		} catch (IOException e) {//统一调用父类的异常
	// TODO Auto-generated catch block
		e.printStackTrace();
		}	
		

	}
	/**
	 * 对接输入输出流
	 * @param is
	 * @param os
	 */
	
	public static void copy(InputStream is,OutputStream os) {
				try {//在jdk9以后,新的版本中是可以通过自动释放资源的
					byte[] flush=new byte[1024];//10k,创建读取数据时的缓冲,每次读取的字节个数。
					int len=-1;//接受长度;
					while((len=is.read(flush))!=-1) {
						//表示当还没有到文件的末尾时
						//字符数组-->字符串,即是解码。
						os.write(flush,0,len);//
						os.flush();
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					//4.释放资源,依次关闭,先打开的后关闭
					close(is,os);
				}
		}	

	public static void close(InputStream is,OutputStream os) {
		try {
			if (null != os) {
				os.close();
			} 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		//表示当文打开时,才需要通知操作系统关闭
			try {
				if(is!=null) {
				is.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	
	public static void close(Closeable... ios) {//可变参数的接口
		for(Closeable io:ios) {
			try {
				if(null!=io) {
					io.close();
				}
			}catch(IOException e) {
				e.printStackTrace();
			}
		}
		
	}

}

字节缓冲流提高性能、

public class TestBufferedStream {

	public static void main(String[] args) {
		String src="copy.jpg";
		String dest="copy_buff.jpg";
		long t1=System.currentTimeMillis();
		copy(src,dest);
		long t2=System.currentTimeMillis();
		System.out.println(t2-t1);
	}
	public static void copy(String srcPath,String destPath) {
		
		//1.创建源
				File src=new File(srcPath);
				File dest=new File(destPath);
				//2.选择流,选择文件输入流
				InputStream is=null;//方便在finally中使用,设置为全局变量
				OutputStream os=null;
				try {
					is=new BufferedInputStream(new FileInputStream(src));//用字节缓冲流可以提高性能
					os=new BufferedOutputStream(new FileOutputStream(dest));
					//3.操作,读文件
					byte[] flush=new byte[1024];//10k,创建读取数据时的缓冲,每次读取的字节个数。
					int len=-1;//接受长度;
					while((len=is.read(flush))!=-1) {
						//表示当还没有到文件的末尾时
						//字符数组-->字符串,即是解码。
						os.write(flush,0,len);//
						os.flush();
					}
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}finally {
					//4.释放资源,依次关闭,先打开的后关闭
					
					try {
						if (null != os) {
							os.close();
						} 
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
					}
					//表示当文打开时,才需要通知操作系统关闭
						try {
							if(is!=null) {
							is.close();
							}
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					
				}
		}		

}


	


发布了43 篇原创文章 · 获赞 11 · 访问量 2581

猜你喜欢

转载自blog.csdn.net/weixin_43328816/article/details/104316929