您的位置:首页 > 编程语言 > Java开发

Java培训-IO流补充

2015-07-22 19:14 483 查看

1.RandomAccessFile:

支持文件读写。
构造器
RandomAccessFile(String file,String mode)
Mode:
r 读
rw 读写
read()….
Write()….
Seek(long pos) 跳过pos字节,pos+1开始读取或写入
skipBytes(int n)丢弃n个字节,不抛异常
public void rw(){
try {
RandomAccessFile raf=new RandomAccessFile("c:/a.txt","rw");
raf.writeUTF("今天是个好日子啊,人民欢欣鼓舞!");
//将指针跳到文件开头位置
raf.seek(0);
String s;
s=raf.readUTF();
System.out.println(s);
//          while((s=raf.readLine())!=null){
//              System.out.println(s);
//          }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

2.DataInputStream和DataOutputStream

DataInputStream能以一种与机器无关(当前操作系统等)的方式,直接从地从字节输入流读取JAVA基本类型和String类型的数据,常用于网络传输等(网络传输数据要求与平台无关)
public void dataInput(){
FileInputStream fis = null;
FileOutputStream fos = null;
DataInputStream dis = null;
DataOutputStream dos = null;
try {
fis = new FileInputStream("c:/1.txt");
dis=new DataInputStream(fis);
fos=new FileOutputStream("c:/b.txt");
dos=new DataOutputStream(fos);
byte[] buff=new byte[1024];
String str;
while((str=dis.readLine())!=null){
System.out.println(str);

dos.writeChars(str);
dos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
closeOutput(dos);
closeOutput(fos);
closeInput(dis);
closeInput(fis);
}
}

3.ObjectInputStream和ObjectOutputStream

创建学生对象:

public class Student implements Serializable{

private String Sname;
private String Ssex;
private int age;
public static String nativ="中国";
public String getSname() {
return Sname;
}
public void setSname(String sname) {
Sname = sname;
}
public String getSsex() {
return Ssex;
}
public void setSsex(String ssex) {
Ssex = ssex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

用ObjectOutputStream保存输入的学生数据:

public class ObjectOutputDemo {

FileOutputStream fos=null;
ObjectOutputStream oos=null;

public void outputStudent(){
try {
fos=new FileOutputStream("c:/student");
oos=new ObjectOutputStream(fos);
Student student=new Student();
student.setSname("刘浩");
student.setSsex("男");
student.setAge(22);
oos.writeObject(student);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
closeOutput(oos);
closeOutput(fos);
}

}

用ObjectInputStream读取保存的学生数据:

public class ObjectInputDemo {

FileInputStream fis=null;
ObjectInputStream ois=null;

public void getStudent(){
try {
fis=new FileInputStream("c:/student");
ois=new ObjectInputStream(fis);
Student student=(Student) ois.readObject();
System.out.println(student.getSname());
System.out.println(student.getSsex());
System.out.println(student.getAge());
System.out.println(student.nativ);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
closeInput(ois);
closeInput(fis);
}
}

4.压缩流

4.1 java 压缩流

zipinputstream 输入流
zipoutputstream 输出流
压缩:
先读取被压缩的文件,在通过压缩输出流 写入到压缩包中。
首先在压缩包中,建立该文件(putNextEntry)。压缩算法不需要关心。
解压缩:
先利用zipinputstream 读取压缩包中的文件,在利用fileoutputstream 写入到磁盘
ZipFile 压缩文件类
可以获取entry,获取压缩文件name

4.2 ZipEntry的方法

构造方法:public ZipEntry(String name) 创建对象并指定要创建的ZipEntry名称
方法:public boolean isDirectory() 判断此ZipEntry是否是目录
在压缩文件中,每一个压缩的内容都可以用一个ZipEntry 表示,所以在进行压缩之前必须通过putNextEntry 设置一个ZipEntry 。
// 获取文件输入流
FileInputStream fis = null;
// 获取文件输出流
FileOutputStream fos = null;
// 获取压缩输出流
ZipOutputStream zos = null;

//压缩指定文件到指定路径
public void zipFile() {
try {
fis = new FileInputStream("c:/1.log");
fos = new FileOutputStream("c:/1.zip");
zos = new ZipOutputStream(fos);

ZipEntry zipEntry = new ZipEntry("1.log");
zos.putNextEntry(zipEntry);
byte[] buff = new byte[1024];
int len;
while ((len = fis.read(buff)) != -1) {
zos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
CloseIO ci = new CloseIO();
ci.closeOutput(zos);
ci.closeOutput(fos);
ci.closeInput(fis);
}
}

4.4 压缩文件夹

/**压缩文件夹
* @param srcPath
*            被压缩文件路径
* @param targetPath
*            压缩文件路径
*/
public void zipDirectory(String srcPath,String targetPath){
try {
File srcFile=new File(srcPath);
fos=new FileOutputStream(targetPath);
zos=new ZipOutputStream(fos);
if(srcFile.isDirectory()){
File[] srcFiles=srcFile.listFiles();
for (int i = 0; i < srcFiles.length; i++) {
if(srcFiles[i].isFile()){
fis=new FileInputStream(srcFiles[i]);
ZipEntry zipEntry=new ZipEntry(srcFile.getName()+
File.separator+
srcFiles[i].getName());
zos.putNextEntry(zipEntry);
byte[] buff = new byte[1024];
int len;
while ((len = fis.read(buff)) != -1) {
zos.write(buff, 0, len);
zos.flush();
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
CloseIO ci=new CloseIO();
ci.closeOutput(zos);
ci.closeInput(fis);
ci.closeOutput(fos);
}
}

4.5 解压缩

//解压缩只有单个文件的
public void unZipFile(String srcPath){
try {
File srcFile=new File(srcPath);
fis=new FileInputStream(srcFile);
zis=new ZipInputStream(fis);
zipFile=new ZipFile(srcFile);

zipEntry=zis.getNextEntry();
String name=zipEntry.getName();
is=zipFile.getInputStream(zipEntry);
//只能自动创建文件,而不能创建不存在的文件夹
fos=new FileOutputStream("d:/"+name);
byte[] buff=new byte[1024];
int len;
while((len=is.read(buff))!=-1){
fos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息