java应用技术
3(4)
一、读写基本数据类型的流
案例:
package cn.java.java4;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
//读写基本数据类型的流
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String path="F:"+File.separator+"aa"+File.separator+"aa.txt";
DataOutputStream dos = new DataOutputStream(new FileOutputStream(path));//将数据按照他的数据类型存储
int age = 1;
double cc = 12.1;
try {
dos.writeInt(age);
dos.writeChar('\t');
dos.writeDouble(cc);
dos.writeChar('\t');
dos.writeInt(5);
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*DataInputStream dps = new DataInputStream(new FileInputStream(path));//将数据按照他的数据类型输出
System.out.print(dps.readInt());
System.out.print(dps.readChar());
System.out.print(dps.readDouble());
System.out.print(dps.readChar());
System.out.print(dps.readInt());
dps.close();
*/
}
}
二、合并流
1.合并文件
案例:
package cn.java.java4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
//合并流
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
/*String path1 = "F:"+File.separator+"aa"+File.separator+"aa.txt";//合并流,合并两个文件
String path2 = "F:"+File.separator+"aa"+File.separator+"bb.txt";
String path3 = "F:"+File.separator+"aa"+File.separator+"cc.txt";
String path4 = "F:"+File.separator+"aa"+File.separator+"dd.txt";
FileInputStream fs1 = new FileInputStream(path1);
FileInputStream fs2 = new FileInputStream(path2);
FileInputStream fs3 = new FileInputStream(path3);
FileOutputStream fs4 = new FileOutputStream(path4);
SequenceInputStream ss = new SequenceInputStream(fs1,fs2);
int temp = 0;
while((temp=ss.read())!=-1){
fs4.write(temp);
}*/
/*String path1 = "F:"+File.separator+"aa"+File.separator+"aa.txt";//合并流,合并多个文件
String path2 = "F:"+File.separator+"aa"+File.separator+"bb.txt";
String path3 = "F:"+File.separator+"aa"+File.separator+"cc.txt";
String path4 = "F:"+File.separator+"aa"+File.separator+"dd.txt";
FileInputStream fs1 = new FileInputStream(path1);
FileInputStream fs2 = new FileInputStream(path2);
FileInputStream fs3 = new FileInputStream(path3);
FileOutputStream fp4 = new FileOutputStream(path4);
ArrayList<FileInputStream> af = new ArrayList<FileInputStream>();
af.add(fs1);
af.add(fs2);
af.add(fs3);
Enumeration<FileInputStream> ef = Collections.enumeration(af);
SequenceInputStream ss = new SequenceInputStream(ef);
int temp = 0;
while((temp=ss.read())!=-1){
fp4.write(temp);
}*/
}
}
2.分割器,合并//可以实现文件传输的功能
案例:
package cn.java.java4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
//简单的文件传输
public class SequenceInputStreamPicturesDemo {
public static void main(String[] args) {
PicturesDemotest pi = new PicturesDemotest();
SequenceInputStreamtest ss = new SequenceInputStreamtest();
pi.picturestest();
ss.SequenceInputStreamtest();
System.out.println("传输成功");
}
}
class PicturesDemotest{//要先把文件分割
public void picturestest(){
String path = "D:"+File.separator+"aa.avi";//文件的路径
InputStream is = null;
try {
is = new FileInputStream(path);//从文件系统中的文件中获得输入字节
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = new byte[1024*1024];//设定分割的大小
int len = 0;
int count = 0;
String path1 = "F:"+File.separator+"BB";//分割后保存的路径
File f1 = new File(path1);
if(!f1.exists()){//如果没有那个文件夹就创建那个文件夹
f1.mkdir();
}
try {
while((len=is.read(b))!=-1){
OutputStream os = new FileOutputStream(path1+File.separator+(++count)+".cc");//将分割后的文件count+1.cc的形式输出在分割后保存的文件夹
os.write(b,0,len);//写入文件夹以最少为0最大为读出b的大小输出
os.close();//结束写入
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();//结束获取
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SequenceInputStreamtest{
public void SequenceInputStreamtest(){//合并文件
String path = "F:" + File.separator + "BB";//分割后文件的文件夹
String path1 = "F:" + File.separator + "AA";//合并后文件的文件夹
File file = new File(path);
File file1 = new File(path1);
String[] ff = null;//建立一个String数组,用来存放指定抽象路径
OutputStream os = null;
SequenceInputStream sis = null;
if (file.exists()) {//将file路径存放如String[] ff
ff = file.list();
}
Collection<FileInputStream> cf = new ArrayList<FileInputStream>();//Collection集合存放FileInputStream的对象
for (int i = 1; i <= ff.length; i++) {//将所有分割的文件存放入Collection集合
try {
cf.add(new FileInputStream(path + File.separator + i + ".cc"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Enumeration<FileInputStream> ef = Collections.enumeration(cf);//因为Collections集合不能直接用,所以将Collections里的数据存放Enumeration集合
sis = new SequenceInputStream(ef);
if(!file1.exists()){//如果没有合并后存放的AA文件夹就创建一个
file1.mkdir();
}
try {
os = new FileOutputStream(path1+File.separator+ "cc.avi");//将合并的文件用cc.avi的
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len = 0;
byte[] b = new byte[1024 * 1024];//确定合并的大小
try {
while ((len = sis.read(b)) != -1) {//因为read最小的返回值为-1所以设置为将不为-1的read存入len
os.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sis.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三、压缩流
1.单层压缩
案例:
package cn.java.java5;
//压缩流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.sun.xml.internal.ws.api.pipe.NextAction;
public class ZipDemo {
public static void main(String[] args) {
String path = "F:"+File.separator+"cc"+File.separator+"bb.txt";
String path1 = "F:"+File.separator+"dd"+File.separator+"aa.zip";
InputStream is = null;
ZipOutputStream zos = null;
File f = new File(path);
try {
is = new FileInputStream(f);
zos = new ZipOutputStream(new FileOutputStream(path1));
zos.putNextEntry(new ZipEntry(f.getName()));
zos.setComment("asdsadas");
int temp = 0;
while((temp=is.read())!=-1){
zos.write(temp);
}
is.close();
zos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.多层压缩
案例:
package cn.java.java5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipDemos {
public static void main(String[] args) throws IOException {
Ziptest zi = new Ziptest();
zi.ziptest();
}
}
class Ziptest{
public void ziptest(){
String path1 = "F:"+File.separator+"dd";
String path2 = "F:"+File.separator+"cc"+File.separator+"aa.zip";
File f1 = new File(path1);
File f2 = new File(path2);
InputStream ips = null;
ZipOutputStream zs = null;
try {
zs = new ZipOutputStream(new FileOutputStream(f2));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(f1.isDirectory()){
File[] fa = f1.listFiles();
for(int i=0;i<fa.length;i++){
try {
ips = new FileInputStream(fa[i]);
zs.putNextEntry(new ZipEntry(fa[i].getName()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int temp;
byte[] b = new byte[1024*1024];
try {
while((temp=ips.read(b))!=-1){
zs.write(b,0,temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ips.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
zs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.解压
案例:
package cn.java.java5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnZipDemo {
public static void main(String[] args) {
unzip("F:"+File.separator+"cc.zip", "F:"+File.separator+"adc");
}
public static void unzip(String zipFileName, String outputDirectory) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z = in.getNextEntry();
while (z != null) {
File f = new File(outputDirectory);
f.mkdirs();
if(z.isDirectory()){
String name = z.getName();
f = new File(outputDirectory + File.separator + name);
f.mkdirs();
}else{
f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
byte[] buff = new byte[1024];
int len ;
while ((len = in.read(buff)) != -1) {
out.write(buff,0,len);
}
out.close();
}
z = in.getNextEntry();
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3(4)
一、读写基本数据类型的流
案例:
package cn.java.java4;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataOutputStreamDemo {
//读写基本数据类型的流
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String path="F:"+File.separator+"aa"+File.separator+"aa.txt";
DataOutputStream dos = new DataOutputStream(new FileOutputStream(path));//将数据按照他的数据类型存储
int age = 1;
double cc = 12.1;
try {
dos.writeInt(age);
dos.writeChar('\t');
dos.writeDouble(cc);
dos.writeChar('\t');
dos.writeInt(5);
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*DataInputStream dps = new DataInputStream(new FileInputStream(path));//将数据按照他的数据类型输出
System.out.print(dps.readInt());
System.out.print(dps.readChar());
System.out.print(dps.readDouble());
System.out.print(dps.readChar());
System.out.print(dps.readInt());
dps.close();
*/
}
}
二、合并流
1.合并文件
案例:
package cn.java.java4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
//合并流
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
/*String path1 = "F:"+File.separator+"aa"+File.separator+"aa.txt";//合并流,合并两个文件
String path2 = "F:"+File.separator+"aa"+File.separator+"bb.txt";
String path3 = "F:"+File.separator+"aa"+File.separator+"cc.txt";
String path4 = "F:"+File.separator+"aa"+File.separator+"dd.txt";
FileInputStream fs1 = new FileInputStream(path1);
FileInputStream fs2 = new FileInputStream(path2);
FileInputStream fs3 = new FileInputStream(path3);
FileOutputStream fs4 = new FileOutputStream(path4);
SequenceInputStream ss = new SequenceInputStream(fs1,fs2);
int temp = 0;
while((temp=ss.read())!=-1){
fs4.write(temp);
}*/
/*String path1 = "F:"+File.separator+"aa"+File.separator+"aa.txt";//合并流,合并多个文件
String path2 = "F:"+File.separator+"aa"+File.separator+"bb.txt";
String path3 = "F:"+File.separator+"aa"+File.separator+"cc.txt";
String path4 = "F:"+File.separator+"aa"+File.separator+"dd.txt";
FileInputStream fs1 = new FileInputStream(path1);
FileInputStream fs2 = new FileInputStream(path2);
FileInputStream fs3 = new FileInputStream(path3);
FileOutputStream fp4 = new FileOutputStream(path4);
ArrayList<FileInputStream> af = new ArrayList<FileInputStream>();
af.add(fs1);
af.add(fs2);
af.add(fs3);
Enumeration<FileInputStream> ef = Collections.enumeration(af);
SequenceInputStream ss = new SequenceInputStream(ef);
int temp = 0;
while((temp=ss.read())!=-1){
fp4.write(temp);
}*/
}
}
2.分割器,合并//可以实现文件传输的功能
案例:
package cn.java.java4;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
//简单的文件传输
public class SequenceInputStreamPicturesDemo {
public static void main(String[] args) {
PicturesDemotest pi = new PicturesDemotest();
SequenceInputStreamtest ss = new SequenceInputStreamtest();
pi.picturestest();
ss.SequenceInputStreamtest();
System.out.println("传输成功");
}
}
class PicturesDemotest{//要先把文件分割
public void picturestest(){
String path = "D:"+File.separator+"aa.avi";//文件的路径
InputStream is = null;
try {
is = new FileInputStream(path);//从文件系统中的文件中获得输入字节
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = new byte[1024*1024];//设定分割的大小
int len = 0;
int count = 0;
String path1 = "F:"+File.separator+"BB";//分割后保存的路径
File f1 = new File(path1);
if(!f1.exists()){//如果没有那个文件夹就创建那个文件夹
f1.mkdir();
}
try {
while((len=is.read(b))!=-1){
OutputStream os = new FileOutputStream(path1+File.separator+(++count)+".cc");//将分割后的文件count+1.cc的形式输出在分割后保存的文件夹
os.write(b,0,len);//写入文件夹以最少为0最大为读出b的大小输出
os.close();//结束写入
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();//结束获取
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SequenceInputStreamtest{
public void SequenceInputStreamtest(){//合并文件
String path = "F:" + File.separator + "BB";//分割后文件的文件夹
String path1 = "F:" + File.separator + "AA";//合并后文件的文件夹
File file = new File(path);
File file1 = new File(path1);
String[] ff = null;//建立一个String数组,用来存放指定抽象路径
OutputStream os = null;
SequenceInputStream sis = null;
if (file.exists()) {//将file路径存放如String[] ff
ff = file.list();
}
Collection<FileInputStream> cf = new ArrayList<FileInputStream>();//Collection集合存放FileInputStream的对象
for (int i = 1; i <= ff.length; i++) {//将所有分割的文件存放入Collection集合
try {
cf.add(new FileInputStream(path + File.separator + i + ".cc"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Enumeration<FileInputStream> ef = Collections.enumeration(cf);//因为Collections集合不能直接用,所以将Collections里的数据存放Enumeration集合
sis = new SequenceInputStream(ef);
if(!file1.exists()){//如果没有合并后存放的AA文件夹就创建一个
file1.mkdir();
}
try {
os = new FileOutputStream(path1+File.separator+ "cc.avi");//将合并的文件用cc.avi的
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len = 0;
byte[] b = new byte[1024 * 1024];//确定合并的大小
try {
while ((len = sis.read(b)) != -1) {//因为read最小的返回值为-1所以设置为将不为-1的read存入len
os.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
sis.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三、压缩流
1.单层压缩
案例:
package cn.java.java5;
//压缩流
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.sun.xml.internal.ws.api.pipe.NextAction;
public class ZipDemo {
public static void main(String[] args) {
String path = "F:"+File.separator+"cc"+File.separator+"bb.txt";
String path1 = "F:"+File.separator+"dd"+File.separator+"aa.zip";
InputStream is = null;
ZipOutputStream zos = null;
File f = new File(path);
try {
is = new FileInputStream(f);
zos = new ZipOutputStream(new FileOutputStream(path1));
zos.putNextEntry(new ZipEntry(f.getName()));
zos.setComment("asdsadas");
int temp = 0;
while((temp=is.read())!=-1){
zos.write(temp);
}
is.close();
zos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.多层压缩
案例:
package cn.java.java5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipDemos {
public static void main(String[] args) throws IOException {
Ziptest zi = new Ziptest();
zi.ziptest();
}
}
class Ziptest{
public void ziptest(){
String path1 = "F:"+File.separator+"dd";
String path2 = "F:"+File.separator+"cc"+File.separator+"aa.zip";
File f1 = new File(path1);
File f2 = new File(path2);
InputStream ips = null;
ZipOutputStream zs = null;
try {
zs = new ZipOutputStream(new FileOutputStream(f2));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(f1.isDirectory()){
File[] fa = f1.listFiles();
for(int i=0;i<fa.length;i++){
try {
ips = new FileInputStream(fa[i]);
zs.putNextEntry(new ZipEntry(fa[i].getName()));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int temp;
byte[] b = new byte[1024*1024];
try {
while((temp=ips.read(b))!=-1){
zs.write(b,0,temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ips.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
try {
zs.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.解压
案例:
package cn.java.java5;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnZipDemo {
public static void main(String[] args) {
unzip("F:"+File.separator+"cc.zip", "F:"+File.separator+"adc");
}
public static void unzip(String zipFileName, String outputDirectory) {
try {
ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName));
ZipEntry z = in.getNextEntry();
while (z != null) {
File f = new File(outputDirectory);
f.mkdirs();
if(z.isDirectory()){
String name = z.getName();
f = new File(outputDirectory + File.separator + name);
f.mkdirs();
}else{
f = new File(outputDirectory + File.separator + z.getName());
f.createNewFile();
FileOutputStream out = new FileOutputStream(f);
byte[] buff = new byte[1024];
int len ;
while ((len = in.read(buff)) != -1) {
out.write(buff,0,len);
}
out.close();
}
z = in.getNextEntry();
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}