流的使用。如何读取一个文本文档。如何写一个XML文档。 当有中文的时候。如何设置字符集?保证XML不管encoding是GBK或UTF-8都能正常存储 如何使用java内置的ZIP把文件压缩存储

package com.rj.yg;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
/**
 * 
 * @author 杨刚
 * 8. 流的使用。如何读取一个文本文档。如何写一个XML文档。
 * 当有中文的时候。如何设置字符集?保证XML不管encoding是GBK或UTF-8都能正常存储
 * 如何使用java内置的ZIP把文件压缩存储 
 * Jul 29, 2010
 */
public class StreamDemo {
public static void main(String [] args){
String path="JAVA 中的IO流.txt";
//读取一个文本文档
readText(path);
// 用jdom写一个XML文档
writeXMLFile();
//使用java内置的ZIP把文件压缩存储 
String file = "test.txt";//要压缩的文件路径
String zipFile = "test.zip";//压缩后文件保存名
fileZipCompress(file, zipFile);
}
/**
* 读取一个文本文档
*/
public static void readText(String path){
try {
FileReader fr=new FileReader(path);//创建FileReader对象,用来读取字符流
BufferedReader br=new BufferedReader(fr);//缓冲指定文件的输入
FileWriter fw=new FileWriter("coppy.txt");//创建FileWriter对象,用来写入字符流
BufferedWriter bw=new BufferedWriter(fw);//将缓冲对文件的输出
String myreadline;
while(br.ready()){
myreadline=br.readLine();//读取一行
bw.write(myreadline);//写入文件
bw.newLine();
System.out.println(myreadline);//在屏幕上输出
}
bw.flush();    //刷新该流的缓冲
            bw.close();
            br.close();
            fw.close();
            fr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* 用jdom写一个XML文档
*/
public static void writeXMLFile(){
Document doc = new Document(); //创建空白文档       
  //创建根元素
  Element root=new Element("booklist");
  doc.setRootElement(root);
   //创建子元素book
  Element book=new Element("book");
  root.addContent(book);
  //为book创建子元素
  book.addContent(new Element("书名").setText("java入门"));
  book.addContent(new Element("作者").setText("地方"));
  book.addContent(new Element("价格").setText("36.0"));
  
//创建子元素book
  Element book1=new Element("book");
  root.addContent(book1);
  //为book创建子元素
  book1.addContent(new Element("书名").setText("jsp入门"));
  book1.addContent(new Element("作者").setText("撒啊"));
  book1.addContent(new Element("价格").setText("46.0"));
//格式化输出XML文档
XMLOutputter XMLout=new XMLOutputter();
Format format=Format.getPrettyFormat(); //格式化文档
  format.setEncoding("GBK"); //由于默认的编码是utf-8,中文将显示为乱码,所以设为gbk
  XMLout.setFormat(format);
try {
XMLout.output(doc, new FileOutputStream("book.xml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用java内置的ZIP把文件压缩存储 
*/
public static void fileZipCompress(String file, String zipFile) {
try {
//创建文件输入流对象
FileInputStream fin=new FileInputStream(file);
//创建文件输出流对象
FileOutputStream fout=new FileOutputStream(zipFile);
//创建zip数据输出流对象
ZipOutputStream zipOut=new ZipOutputStream(fout);
//创建指向压缩原始文件的入口
ZipEntry entry=new ZipEntry(file);
zipOut.putNextEntry(entry);
// 向压缩文件中输出数据
int b;
byte[] by=new byte[1024];//设置缓存大小
while((b=fin.read(by))!=-1){
zipOut.write(by,0,b);
}
// 关闭创建的流对象
zipOut.close();
fout.close();
fin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值