栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java文件读写工具类分享

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java文件读写工具类分享

本文实例为大家分享了java文件读写工具类的具体代码,供大家参考,具体内容如下

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.servlet.http.HttpServletResponse;


@SuppressWarnings({"resource","unused"})
public class FileUtils {
  
  
  public static String getConTextPath(){
    String fileUrl = Thread.currentThread().getContextClassLoader().getResource("").getPath();
    if("usr".equals(fileUrl.substring(1,4))){
      fileUrl = (fileUrl.substring(0,fileUrl.length()-16));//linux
    }else{
      fileUrl = (fileUrl.substring(1,fileUrl.length()-16));//windows
    }
    return fileUrl;
  }
    
  
  public static String[] StringToArray(String str,String splitStr){
    String[] arrayStr = null;
    if(!"".equals(str) && str != null){
      if(str.indexOf(splitStr)!=-1){
 arrayStr = str.split(splitStr);
      }else{
 arrayStr = new String[1];
 arrayStr[0] = str;
      }
    }
    return arrayStr;
  }
    
  
  public static String ReadFile(String Path) {
    BufferedReader reader = null;
    String laststr = "";
    try {
      FileInputStream fileInputStream = new FileInputStream(Path);
      InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
      reader = new BufferedReader(inputStreamReader);
      String tempString = null;
      while ((tempString = reader.readLine()) != null) {
 laststr += tempString;
      }
      reader.close();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
 try {
   reader.close();
 } catch (IOException e) {
   e.printStackTrace();
 }
      }
    }
    return laststr;
  }
    
  
  public static HashMap getFilesName(String folderPath , String queryStr) {
    HashMap map = new HashMap<>();
    List fileNameList = new ArrayList<>();//文件名列表
    List folderNameList = new ArrayList<>();//文件夹名列表
    File f = new File(folderPath);
    if (!f.exists()) { //路径不存在
      map.put("retType", "1");
    }else{
      boolean flag = f.isDirectory();
      if(flag==false){ //路径为文件
 map.put("retType", "2");
 map.put("fileName", f.getName());
      }else{ //路径为文件夹
 map.put("retType", "3");
 File fa[] = f.listFiles();
 queryStr = queryStr==null ? "" : queryStr;//若queryStr传入为null,则替换为空(indexOf匹配值不能为null)
 for (int i = 0; i < fa.length; i++) {
   File fs = fa[i];
   if(fs.getName().indexOf(queryStr)!=-1){
      if (fs.isDirectory()) {
 folderNameList.add(fs.getName());
      } else {
 fileNameList.add(fs.getName());
      }
    }
 }
 map.put("fileNameList", fileNameList);
 map.put("folderNameList", folderNameList);
      }
    }
    return map;
  }
  
  
  public static List readFileContent(String filePath) { 
    BufferedReader reader = null;
    List listContent = new ArrayList<>();
    try { 
      reader = new BufferedReader(new FileReader(filePath)); 
      String tempString = null; 
      int line = 1; 
      // 一次读入一行,直到读入null为文件结束 
      while ((tempString = reader.readLine()) != null) {
 listContent.add(tempString);
 line++;
      } 
      reader.close(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
      if (reader != null) { 
 try { 
   reader.close(); 
 } catch (IOException e1) { 
 } 
      } 
    }
    return listContent;
  } 
  
  
  public static String readLineContent(String filePath,int lineNumber){
    BufferedReader reader = null;
    String lineContent="";
    try {
      reader = new BufferedReader(new FileReader(filePath));
      int line=0;
      while(line<=lineNumber){
 lineContent=reader.readLine();
 line++;
      }
      reader.close();
    } catch (IOException e) { 
      e.printStackTrace(); 
    } finally { 
      if (reader != null) { 
 try { 
   reader.close(); 
 } catch (IOException e1) { 
 } 
      } 
    } 
    return lineContent;
  }
  
  
  public static List readLinesContent(String filePath,int beginLineNumber,int endLineNumber){
    List listContent = new ArrayList<>();
    try{
      int count = 0;
    BufferedReader reader = new BufferedReader(new FileReader(filePath));
      String content = reader.readLine();
      while(content !=null){
 if(count >= beginLineNumber && count <=endLineNumber){
   listContent.add(content);
 }  
 content = reader.readLine();
 count++;  
      }
    } catch(Exception e){
    }
    return listContent;
  }
    
  
  public static List readFileContent_list(List listFilePath) { 
    List listContent = new ArrayList<>();
    for(String filePath : listFilePath){
File file = new File(filePath);
BufferedReader reader = null;
      try { 
 reader = new BufferedReader(new FileReader(file)); 
 String tempString = null; 
 int line = 1; 
 // 一次读入一行,直到读入null为文件结束 
 while ((tempString = reader.readLine()) != null) {
   listContent.add(tempString);
   line++;
 } 
 reader.close(); 
      } catch (IOException e) { 
 e.printStackTrace(); 
      } finally { 
 if (reader != null) { 
   try { 
     reader.close(); 
   } catch (IOException e1) { 
   } 
 } 
      }
    }
    return listContent;
  }
  
  
  public static String fileLinesWrite(String filePath,String content,boolean flag){
    String filedo = "write";
    FileWriter fw = null;
    try {
      File file=new File(filePath);
      //如果文件夹不存在,则创建文件夹
      if (!file.getParentFile().exists()){
 file.getParentFile().mkdirs();
      }
      if(!file.exists()){//如果文件不存在,则创建文件,写入第一行内容
 file.createNewFile();
 fw = new FileWriter(file);
 filedo = "create";
      }else{//如果文件存在,则追加或替换内容
 fw = new FileWriter(file, flag);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
      PrintWriter pw = new PrintWriter(fw);
      pw.println(content);
      pw.flush();
    try {
      fw.flush();
      pw.close();
      fw.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return filedo;
  }
      
  
  public static void writeIntoOut(InputStream ins, OutputStream out) {
    byte[] bb = new byte[10 * 1024];
    try {
      int cnt = ins.read(bb);
      while (cnt > 0) {
 out.write(bb, 0, cnt);
 cnt = ins.read(bb);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
 out.flush();
 ins.close();
 out.close();
      } catch (IOException e) {
 e.printStackTrace();
      }
    }
  }
  
  
  private static boolean hasSame(List list){ 
    if(null == list) 
      return false; 
    return 1 == new HashSet(list).size(); 
  } 
  
  
  private static boolean hasSame2(List list){ 
    if(null == list) 
      return false; 
    return list.size() == new HashSet(list).size(); 
  } 
  
  
  public static Date DateAddOrSub(Date date, int num) { 
    Calendar startDT = Calendar.getInstance(); 
    startDT.setTime(date); 
    startDT.add(Calendar.DAY_OF_MONTH, num); 
    return startDT.getTime(); 
  }
  
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号