怎样用Java将文件追加到zip文件

怎样用Java将文件追加到zip文件,第1张

import java.io.BufferedOutputStream

import java.io.File

import java.io.FileInputStream

import java.io.FileOutputStream

import org.apache.tools.zip.ZipEntry

import org.apache.tools.zip.ZipOutputStream

/**

* @project: Test

* @author chenssy

* @date 2013-7-28

* @Description: 文件压缩工具类

* 将指定文件/文件夹压缩成好乎zip、rar压缩文件

*/

public class CompressedFileUtil {

/**

* 默友陪悉认乱或构造函数

*/

public CompressedFileUtil(){

java文件追加内容的携灶三种方法

方法一:

public static void writeToTxtByRandomAccessFile(File file, String str){

RandomAccessFile randomAccessFile = null

try {

randomAccessFile = new RandomAccessFile(file,"rw")

long len = randomAccessFile.length()

randomAccessFile.seek(len)

randomAccessFile.writeBytes(new String(str.getBytes(),"iso8859-1")+"\r\n")

} catch (FileNotFoundException e) {

e.printStackTrace()

}catch (IOException e) {

e.printStackTrace()

}finally{

try {

randomAccessFile.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

方法二誉此:辩虚扮

public static void writeToTxtByFileWriter(File file, String content){

BufferedWriter bw = null

try {

FileWriter fw = new FileWriter(file, true)

bw = new BufferedWriter(fw)

bw.write(content)

} catch (IOException e) {

e.printStackTrace()

}finally{

try {

bw.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}

方法三:

public static void writeToTxtByOutputStream(File file, String content){

BufferedOutputStream bufferedOutputStream = null

try {

bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file, true))

bufferedOutputStream.write(content.getBytes())

} catch (FileNotFoundException e) {

e.printStackTrace()

} catch(IOException e ){

e.printStackTrace()

}finally{

try {

bufferedOutputStream.close()

} catch (IOException e) {

e.printStackTrace()

}

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/12141347.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-21
下一篇2023-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存