现在的位置: 首页 > 综合 > 正文

生成txt文件并且压缩,其中有解决中文问题

2012年09月09日 ⁄ 综合 ⁄ 共 3264字 ⁄ 字号 评论关闭
Code:
/**
* 生成txt文件并且压缩
*/
public class Result {
private Log log;

Properties properties;

private File filehave;

private FileOutputStream writer;

private PrintWriter pw;

private Writer out;

private String mobileType,time,ytime,baseFilename;

public Result() {
try {
intital();

} catch (IOException iox) {
System.err.println(iox);
}

}

/**
* 初始化函数
* @return
* @throws IOException
*/
private void intital() throws IOException {
log = LogFactory.getLog(Result.class);
File sdf = new File("");
System.out.println(sdf.getAbsolutePath());
properties = new Properties();
properties.load(new FileInputStream("config.properties"));
}
private ArrayList getMidColResData(String Type,int purpose) {

ArrayList ret = new ArrayList();
Connection conn = getConn();
Statement stmt = null;
ResultSet rs = null;
try {

........数据库操作

rs.close();
stmt.close();
rs = null;
stmt = null;

} catch (Exception e) {
log.debug(e);
} finally {
if (rs != null) {
rs = null;
}
if (stmt != null) {
stmt = null;
}
if (conn != null) {
conn = null;
}
}
return ret;
}

//生成/写入txt文件
public void writeToTXT(String Type,int purpose) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
time = formatter.format(new Date());

java.util.Date myDate = new java.util.Date();
long myTime = (myDate.getTime()/1000) - 60*60*24;
myDate.setTime(myTime*1000);
String yDate=formatter.format(myDate);

//生成txt文件
try {

if(mobileType.equals("test")){
if(purpose == 0){
baseFilename = "test_" + time + "_01";
}else if(purpose == 1){
baseFilename = "test_" + time + "_02";
}
}

//删除昨天的txt文件
filehave = new File("test_" + yDate + "_01.txt");
if(filehave.exists()){
filehave.delete();
}
filehave = new File("test_" + yDate + "_02.txt");
if(filehave.exists()){
filehave.delete();
}

filehave = new File("test_" + yDate + "_01.zip");
if(filehave.exists()){
filehave.delete();
}
filehave = new File("test_" + yDate + "_02.zip");
if(filehave.exists()){
filehave.delete();
}

writer = new FileOutputStream(baseFilename + ".txt");

out = new OutputStreamWriter(writer,"UTF-8");

} catch (IOException iox) {
System.err.println(iox);
}

//写入txt文件
try {
ArrayList list = getMidColResData(mobileType,purpose);
//建立数据行
List data = list;
for (int j = 0; j < data.size(); j++) {
String[] rows = (String[]) data.get(j);
for (int k = 0; k < rows.length; k++) {
out.write(new String(rows[k].getBytes("ISO-8859-1"),"UTF-8"));//整合了的字符串再转换一次(在数据库取数据时已经转换了一次)
}
}
try {

out.flush();
writer.close();
} catch (IOException iox) {
System.err.println(iox);
}
} catch (Exception e) {
log.debug(e);
}
}
public void zipTXT(){
// These are the files to include in the ZIP file
String[] filenames = new String[]{"test" + baseFilename + ".txt"};

// Create a buffer for reading the files
byte[] buf = new byte[1024];

try {
// Create the ZIP file
String outFilename = "test" + baseFilename + ".zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

// Compress the files
for (int i=0; i<filenames.length; i++) {
FileInputStream in = new FileInputStream(filenames[i]);

// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(filenames[i]));

// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}

// Complete the entry
out.closeEntry();
in.close();
}

// Complete the ZIP file
out.close();
} catch (IOException e) {
}
}
public static void main(String args[]) {
try {
System.out.println("create TXTFile begin");

Result test = new Result();

for (int i = 0; i < 2; i++) {
test.writeToTXT("test",i);//生成文件
test.zipTXT();//压缩文件
}
for (int i = 0; i < 4; i++) {
test.writeToTXT("what",i);//生成文件
test.zipTXT();//压缩文件
}
System.out.println("create TXTFile end");

} catch (Exception e) {
System.out.println(e.toString());
}
}

}

再使用>javac -classpath ..... -encoding GBK Result.java

抱歉!评论已关闭.