io流

File中的一些方法
File file = new File("C:/Users/Administrator/Desktop/GG");
    file.createNewFile();  创建文件
    file.mkdirs(); 创建文件夹 
    file.delete(); 删除

    file.isDirectory(); 判断是否是目录
    file.isFile(); 判断是否是文件
    file.exists(); 判断是否存在
    file.canRead(); 判断是否可读
    file.canwrite();  判断是否可写
    file.isHidden()  判断是否隐藏

    file.getAbsolutepath();  获取绝对路径
    file.getpath();  获取路径
    file.getName();  

    file.length();  获取长度 (Long类型)
    file.lastModified(); 获取最后一次的修改时间 (Long类型)
    file.list(); 获取文件夹下所有东西(String类型)
    file.listFile();  获取文件夹下所有东西(File类型—含路径)
File、FileInputStream、ByteArrayOutputStream
            File file = new File("C:/Users/Administrator/Desktop/GG");
	    	File filedoc = new File(file+"/gg.doc");
	    	if (!file.exists()) {
				log.info("文件夹不存在,创建文件夹");
				file.mkdirs();
			}
	    	if (!filedoc.exists()) {
	    		log.info("文件不存在,创建文件");
				filedoc.createNewFile();
			}    	
	    	FileOutputStream out = new FileOutputStream(filedoc,true); //true:不覆盖以前的类容(可为空)
	    	FileInputStream in = new FileInputStream(new File("C:/Users/Administrator/Desktop/1.doc"));
	    	/**
	    	 * 将读取到1.doc的数据 写入 ByteArrayOutputStream 字节数组输出流
	    	 */
	    	ByteArrayOutputStream bytewrite = new ByteArrayOutputStream();
	    	int i = 0;
	    	while((i=in.read()) != -1){
	    		bytewrite.write(i);
	    	}
	    	byte[] aby = bytewrite.toByteArray();
	    	
	    	String str = new String(aby,0,aby.length);
	    	log.info("读取到1.doc文件数据为:"+str);
	    	//将1.doc 中的内容复制到 gg.doc 中
	    	out.write(str.getBytes());
	    	//out.flush(); //将缓冲区中数据送达目的地
	    	bytewrite.close();
	    	out.close();
	    	in.close();
ServletOutputStream流,写入字节数组
public void showfileUpload(HttpServletResponse response, String id) {
		log.info("读取图片信息 ");
		try {
			//设置读取路径
			String filenameUrl = FILEUPLOAD_PATH+id+".jpg";
			//创建文件输入流 ,读取信息
			FileInputStream fis = new FileInputStream(new File(filenameUrl));
			
			ByteArrayOutputStream bytewrite = new ByteArrayOutputStream();
	    	int i = 0;
	    	while((i=fis.read()) != -1){
	    		bytewrite.write(i);
	    	}
	    	byte[] bs = bytewrite.toByteArray();
			/**
			 *  设置 响应格式(没有则显示不了)
			 */
			response.setContentType("image/jpeg; charset=utf-8");
			//servlet输出流
			ServletOutputStream outputStream = response.getOutputStream();
			outputStream.write(bs);				
		} catch (Exception e) {
			log.info("读取信息异常"+e);
		}		
	}
InputStream(字节流) —> InputStreamReader(字符流) —> BufferedReader(字符缓冲区流)
  String str = "";
  InputStream in = request.getInputStream();  
  InputStreamReader reader = new InputStreamReader(in);  
  BufferedReader bd = new BufferedReader(reader);
     while ((inputLine = bd.readLine()) != null) {
           str += inputLine;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值