FileUtils

package wobo.data.utils;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;




/**
 * 文件操作的工具类
 * 
 * @author guancc
 */
public class FileUtils {

/**
* 读取一个指定文件内容
* @param filePath
* @return
*/
public static String readFile(String filePath){
if(filePath==null || filePath.trim().length()==0){
return null;
}

StringBuilder b = new StringBuilder();
try {
FileInputStream in = new FileInputStream(filePath);
byte buf[] = new byte[2048];
int len = -1;
while( (len = in.read(buf))!=-1 ){
String s = new String(buf,0,len);
b.append(s);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
return b.toString();
}

/**
* 把文件内容content写到path指定的目录中
* @param path
* @param content
*/
public static boolean writeFile(String path,String content){
if(path==null || path.trim().equals("") || content==null){
return false;
}
try {
FileOutputStream out = new FileOutputStream(path);
out.write(content.getBytes());
out.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

/**
* 读取classes目录的路径
* @return
*/
public static String getClassPath(){
String path = null;
path = FileUtils.class.getResource("/").getPath();
return path;
}

/**
* 读取classes目录的路径
* @return
*/
public static String getClassPath(String file){
try{
String path = null;
path = FileUtils.class.getResource("/"+file).getPath();
return path;
}catch(Exception e){
return null;
}
}

/**
* 在给出的路径中找到文件名并返回。
*/
public static String getFileName(String path){
if(path==null || path.trim().equals("")) {
return path;
}

String name = null;
int index = path.lastIndexOf("/");
if(index==-1){
return path;
}else{
name = path.substring(index+1,path.length());
}

return name;
}

/**
* 从完整pckgClass中提取class名称。
* @param pckg
* @return
*/
public static String getClassName(String pckgClass){
if(pckgClass==null || pckgClass.trim().equals("")){
return pckgClass;
}
String clsName = null ;
int index = pckgClass.lastIndexOf('.');
return pckgClass.substring(index+1);
}

/**
* 读取指定文件名的后缀。
* @param name
* @return
*/
public static String getSuffix(String name){
if(name==null || name.trim().equals("")) {
return "";
}

name = getFileName(name);
int index = name.lastIndexOf(".");
if(index==-1){
return "";
}

return name.substring(index+1,name.length());
}

/**
* 大写首字符
* @param str
* @return
*/
public static String upperFirstLetter(String str){
if(str==null || str.trim().equals("")){
return str;
}

char[] cs = str.toCharArray();
int c = cs[0];
if(c>=97 && c<=122){
cs[0] = (char)(c-32);
}

return new String(cs);
}

/**
* 从reader中读取全部字符
* @param reader
* @return
*/
public static String getStringFromReader(Reader reader){
if(reader==null){
return "";
}

StringBuilder buf = new StringBuilder();
int len = -1;
char[] c = new char[1024];
try{
while((len=reader.read(c))!=-1){
buf.append(new String(c,0,len));
}
}catch(IOException e){
return "";
}

return buf.toString();
}

public static void main(String[] args) {
String s = "E:/workspace/5bo_jifei/文档/调度任务说明.txt";
// String s = "E:/workspace/guagua_webchatroom/WebContent/js/room_new_1.77.js";
try {
Reader rd = new FileReader(s);
getStringFromReader(rd);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值