当前位置: 代码迷 >> Web前端 >> java 读写工具种
  详细解决方案

java 读写工具种

热度:206   发布时间:2012-09-02 21:00:34.0
java 读写工具类
public class Util {
/**
* 读文件到内存,返回的编码方式是utf-8
* @param fileName
* @return
* 2011-11-2
*/
public static String readFile(String fileName){
File file = new File(fileName);
    Reader reader = null;
    StringBuffer buf = new StringBuffer();
    String outString ="";
    try {
    //以utf-8的形式读入文件
        reader = new InputStreamReader(new FileInputStream(file),"utf-8");
        int tempchar;
        while ((tempchar = reader.read()) != -1) {
            if (((char) tempchar) != '\r') {
            char aa = (char) tempchar;
                buf.append((char) tempchar);
            }
        }
        outString = buf.toString();
        //判断读入的文件编码方式是否是gb2312的形式,要是不是,重新以gb2312的形式读入文件,之后再改成utf-8的形式
        if(buf.toString().contains("charset=gb2312")){
        buf = new StringBuffer();
        //以gb2312的形式读入文件
        reader = new InputStreamReader(new FileInputStream(file),"gb2312");
        while ((tempchar = reader.read()) != -1) {
            if (((char) tempchar) != '\r') {
            char aa = (char) tempchar;
                buf.append((char) tempchar);
            }
        }
        outString = Util.code2code(buf.toString(), "gb2312", "utf-8");
    }
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
return outString;
}
/**
* 输出文件
* @param fileName
* @param s
* 2011-11-2
*/
public static void writerFile(String fileName,String s){
String mkd = fileName.substring(0, fileName.lastIndexOf("\\"));
//生成目录
new File(mkd).mkdirs();
FileWriter fw;
try {
fw = new FileWriter(fileName);
fw.write(s);
fw.flush(); 
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 读目录,把目录下的文件名生成List
* @param delpath
* @return
* 2011-11-2
*/
public static List readDirectory(String delpath){
List list = new ArrayList();
File file = new File(delpath);
String[] monthlist = file.list();
int l=1;
for (int i = 0; i < monthlist.length; i++) {

File monthfile = new File(delpath + "\\" + monthlist[i]);
String monthpath=monthfile.getPath();

String[] daylist = monthfile.list();
for(int j = 0 ; j < daylist.length ; j++){
File dayfile = new File(monthpath + "\\" + daylist[j]);
String[] contentlist = dayfile.list();
String datepath=dayfile.getPath();
if(contentlist!=null){
for(int k =0;k < contentlist.length;k++){
File content = new File(datepath + "\\" + contentlist[k]);
String path = content.getPath();
list.add(content.getPath());

System.out.println(l++);
// System.out.println(content.getPath());
}
}

}

}
System.out.println("******");
return list;
}

/**
* 编码转换
* @param strIn 要转换的字符串
* @param sourceCode 原来的编码方式
* @param targetCode 目标编码方式
* @return
* 2011-11-2
*/
public static String code2code(String strIn, String sourceCode,String targetCode) {
String strOut = null;
if (strIn == null || (strIn.trim()).equals(""))
return strIn;
try {
byte[] b = strIn.getBytes(sourceCode);
String str = new String(b, sourceCode);
byte[] c = str.getBytes(targetCode);
strOut = new String(c, targetCode);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return strOut;
}
}
  相关解决方案