package com.ostrichmyself.api.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
/**
* 普通的文本文件读取与写入操作
* 注意,面向字符的, 用Reader/Writer, 面向字节的, InputStream/OutputStream
* 面向字符会产生乱码问题, 此时需要采用 InputStreamReader去改写字符
* 为了不造成问题的复杂, 这里不做研究, 采用系统默认编码[GB2312], 仅仅写了一个处理乱码的函数
* getStringFromFile(String filePath, String code)
* @author Galaxy Team
*
*/
public class FileProcesser {
/**
* 文件保存为String
* 读取的是字符, 不是字节
* 采用默认编码
* @param filePath
* @return
*/
public static String getStringFromFile(String filePath)
{
try {
StringBuffer sBuffer = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(filePath));
if(!new File(filePath).exists())
{
return null;
}
while (in.ready()) {
sBuffer.append(in.readLine() + "\r\n");
}
in.close();
return sBuffer.toString();
} catch (Exception e)
{
System.out.println("不存在制定的文件" + filePath);
}
return null;
}
/**
* 文件保存为String
* 读取的是字符, 不是字节
* code 的取值不区分大小写
* GBK, GB2312, UTF8/UTF-8, UTF-16/UTF16, utf-16be等
* @param filePath
* @param code 编码方式
* @return
*/
public static String getStringFromFile(String filePath, String code)
{
try {
StringBuffer sBuffer = new StringBuffer();
FileInputStream fInputStream = new FileInputStream(filePath);
InputStreamReader inputStreamReader = new InputStreamReader(fInputStream, code);
BufferedReader in = new BufferedReader(inputStreamReader);
if(!new File(filePath).exists())
{
return null;
}
while (in.ready()) {
sBuffer.append(in.readLine() + "\r\n");
}
in.close();
return sBuffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 从文件中取二进制字节数组
* @param filePath
* @return
*/
public static byte[] getByteFromFile(String filePath)
{
FileInputStream fileInputStream = null;
byte[] data = null;
BufferedInputStream bf = null;
try {
fileInputStream = new FileInputStream(filePath);
bf = new BufferedInputStream(fileInputStream);
data = new byte[bf.available()];
bf.read(data);
return data;
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
try {
fileInputStream.close();
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 保存文本文件
* 写入的是字符, 不是字节
* @param filePath
* @param content
*/
public static void setStringContentToFile(String filePath, String content)
{
try {
File file = new File(filePath);
file.createNewFile();
FileOutputStream fo = new FileOutputStream(filePath);
PrintWriter pw = new PrintWriter(new File(filePath));
pw.write(content);
fo.close();
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
*
* @param filePath
* @param content
*/
public static void setBytesContentToFile(String filePath, byte[] content)
{
FileOutputStream fileOutputStream = null;
BufferedOutputStream bf = null;
try {
File file = new File(filePath);
file.createNewFile();
fileOutputStream = new FileOutputStream(filePath);
bf = new BufferedOutputStream(fileOutputStream);
fileOutputStream.write(content);
}
catch(Exception e)
{
e.printStackTrace();
}
finally {
try {
fileOutputStream.close();
bf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args)
{
//res/IniFileProcessor.java采用的是UTF-8
System.out.println(getStringFromFile("res/IniFileProcessor.java").trim());
System.out.println(getStringFromFile("res/IniFileProcessor.java", "UTF-8").trim());
}
}