文件流总结

来源:互联网 发布:mac 读取exe 编辑:程序博客网 时间:2024/04/30 13:32

String str= "";
    String all ="";
    
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
    while ((str = br.readLine())!=null){
     all +=str;
    }

读取某文件

 

把某字符串写入指定文件

      String   content= “要写入的字符串”;
       if(content != null && !"".equals(content)){
       File   tofile=new   File(docFilePathHtm);  
        
       FileWriter   fw=new   FileWriter(tofile);  
       BufferedWriter   buffw=new   BufferedWriter(fw); 
       PrintWriter   pw=new   PrintWriter(buffw);  

 

       
        
       pw.write(content);
       pw.flush();
       pw.close();  
       buffw.close();  
       fw.close();  

  /**
     * 读取二进制数据从文件
     * @param filePath
     * @return
     * @throws Exception
     */
    public static byte [] getByteFromFile(String filePath) throws Exception{
     
      if(fileValidate(filePath)){
   
   File file = new File(filePath);
   byte b [] = new byte [(int) file.length()];
   InputStream is = new FileInputStream(file);
      BufferedInputStream bi = new BufferedInputStream(is);
      bi.read(b);
      bi.close();
      is.close();
      return b;
   
  }else{
   logger.info("文件不合法");
  }
  return null;
    }

 

 

 

 /**
     * 读取二文本数据从文件
     * @param filePath
     * @return
     * @throws Exception
     */
    public static String getStringOfFile(String filePath) throws Exception{
  
  if(fileValidate(filePath)){
   
   File file = new File(filePath);
   byte b [] = new byte [(int) file.length()];
   InputStream is = new FileInputStream(file);
      BufferedInputStream bi = new BufferedInputStream(is);
      bi.read(b);
      bi.close();
      is.close();
      return new String(b);
   
  }else{
   logger.info("文件不合法");
  }
  return null;
 }