生成txt文件并且压缩,其中有解决中文问题
Code:
再使用>javac -classpath ..... -encoding GBK Result.java
/** * 生成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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!