IO输入输出流

InputStreamTest .java

package cn.com.io;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class InputStreamTest {

 
 public static void main(String[] args) {
  InputStreamTest it = new InputStreamTest();
  try {
   it.print("d:/000.properties");
   Map<String, String> map = it.loadMap("d:/000.properties");
  //  Map<String, String> map=it.mapLoad("d:/000.properties");
   for (String str : map.keySet()) {
    System.out.println(str + "=" + map.get(str));
   }
  } catch (IOException e) {
   e.printStackTrace();
  }

 }

 
 public void printFile(String fileName) {
  InputStream in=null;
  try {
   in = new FileInputStream(fileName);
   byte[] buff = new byte[1024];
   int len = 0;
   while ((len=in.read(buff))!= -1) {
    System.out.write(buff,0,len);
   }
   } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    in.close();
   } catch (IOException e) {
       e.printStackTrace();
   }
  }
 }

 
 public void print(String fileName) throws IOException {
  InputStream in = new FileInputStream(fileName);
  byte[] buff = new byte[1024];
  int len = in.read(buff);
  while (len != -1) {
   for (int i = 0; i < len; i++) {
    if (i % 8 == 0 && i != 0) {
     System.out.println();
    }
    String s = Integer.toHexString(buff[i] & 0xff);
    System.out.printf("0x%2s  ", s);
   }
   len = in.read(buff);
  }
  in.close();
 }

 
 
 public Map<String, String> loadMap(String fileName) {
  Map<String, String> map = new HashMap<String, String>();
  try {
   BufferedReader reader = new BufferedReader(new FileReader(fileName));
//          BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
   String line = null;
   while ((line = reader.readLine()) != null) {
    if (line.indexOf("=") != -1) {
     String[] datas = line.split("=");
     map.put(datas[0], datas[1]);
    }
   }
   reader.close();
  } catch (FileNotFoundException e) {

   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return map;
 }

 
 
 public Map<String, String> mapLoad(String fileName) {
  Map<String, String> map = new HashMap<String, String>();
  try {
   InputStream in = new FileInputStream(fileName);
   byte[] buff = new byte[1024];
   int len = in.read(buff);
   while (len != -1) {
    String data = new String(buff, 0, len);         //new一个String,值为buff中的从0到len的值.
    String[] lines = data.split("/r/n");
    for (String line : lines) {
     String[] kv = line.split("=");
     map.put(kv[0], kv[1]);
    }
    len = in.read(buff);
   }
   in.close();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {

   e.printStackTrace();
  }
  return map;

 }

}

 

 

 

OutputStreamTest .java

package cn.com.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class OutputStreamTest {

 
 public static void main(String[] args) {
  OutputStreamTest ot=new OutputStreamTest();
  ot.copyFile1("E://Music//张学友//张学友 - 只愿一生爱一人.mp3", "e:/I love you.mp3");
  ot.copyFile2("离开以后.mp3", "e:/离开以后.mp3");
 }
 
 
 
 public void copyFile1(String fileName,String aimFileName) {
  InputStream in=null;
  OutputStream out=null;
  try {
   in = new FileInputStream(fileName);
   out=new FileOutputStream(aimFileName);
   byte[] buff = new byte[1024];
   int len = 0;
   
   while ((len=in.read(buff))!= -1) {
      out.write(buff,0,len);       //假设原文件有2049个字节,每一次读取1024个字节到buff中去,len=1024,接着再读取1024个字节到buff中去,len=1024,
                                   //接着读取剩下的一个字节到buff中去,len=1,如果直接out.write(buff),将会出错.因为最后一次读取文件实际长度只
               //有一个字节,而buff有1024个字节,其余的1023个字节都是以前的垃圾字节.所以要用out.write(buff,0,len)
   }
   } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    in.close();
    out.close();
   } catch (IOException e) {
       e.printStackTrace();
   }
  }
 }
 
 
 
  public void copyFile2(String from,String to){
  InputStream in=null;
  OutputStream out=null;
  in=ClassLoader.getSystemResourceAsStream(from);    //获取该项目下的文件,将其自动转为InputStream.文件必须放在src目录下
  try {
   out=new FileOutputStream(to);
   byte[] buff=new byte[1024];
   int len=0;
   while((len=in.read(buff))!=-1){
    out.write(buff, 0, len);
    out.flush();                             //读一个文件,马上刷新到目标文件中去,而如果不用此方法,只有到out.close()时一次性写到目标文件.
   }
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    in.close();
    out.close();
   } catch (IOException e) {
     e.printStackTrace();
   }
   
  }
   
 }
}

 

 

 

DataInputStreamTest .java

package cn.com.io;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataInputStreamTest {

 
 public static void main(String[] args) {
  DataInputStreamTest dst=new DataInputStreamTest();
  String fileName="e:/test.dat";
  dst.writeToFile(fileName);
  dst.readFormFile(fileName);
 }

 
 
 public void writeToFile(String fileName){
  try {
  DataOutputStream dos=new DataOutputStream(new FileOutputStream(fileName));
  dos.writeInt(10);
  dos.writeInt(100);
  dos.writeInt(1000);
  dos.writeInt(10000);
  dos.writeInt(100000);
  dos.close();
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 } catch (IOException e) {
  e.printStackTrace();
 } 
}
 
 public void readFormFile(String fileName){
   try {
   DataInputStream dos=new DataInputStream(new FileInputStream(fileName));
   int data1=dos.readInt();
   int data2=dos.readInt();
   int data3=dos.readInt();
   int data4=dos.readInt();
   int data5=dos.readInt();
   dos.close();
   System.out.println(data1+"/t"+ data2+"/t"+data3+"/t"+data4+"/t"+data5+"/t");
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  } 
   }
}

 

 

 

ZipInputStreamTest .java

package cn.com.io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZipInputStreamTest {

 public static void main(String[] args) {
  try {
   ZipInputStreamTest zst = new ZipInputStreamTest();
   zst.zipToFile("E:/MyMusic.zip", "E:/myMusicZip/");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 
 @SuppressWarnings("unchecked")
 public void zipToFile(String sourceFile, String toFolder) throws Exception {

  ZipFile zipFile = new ZipFile(sourceFile);                                           // 连接待解压文件
  System.out.println("要解压的文件是:" + zipFile.getName());

  Enumeration<ZipEntry> zipEntryList = (Enumeration<ZipEntry>) zipFile.entries();      // 得到zip包里的所有元素
  byte[] buf = new byte[1024];

  while (zipEntryList.hasMoreElements()) {
   ZipEntry zipEntry = (ZipEntry) zipEntryList.nextElement();
   System.out.println(zipEntry.getName());
   // 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
   OutputStream out = new BufferedOutputStream(new FileOutputStream(getRealFileName(toFolder, zipEntry.getName())));
   InputStream in = new BufferedInputStream(zipFile.getInputStream(zipEntry));
   int len = 0;
   while ((len = in.read(buf)) != -1) {
    out.write(buf, 0, len);
   }
   in.close();
   out.close();
  }
  zipFile.close();
  System.out.println("文件解压成功!");
 }

 
 private File getRealFileName(String zippath, String absFileName) {
  String fileName = zippath + absFileName;

  String dirs = fileName.substring(0, fileName.lastIndexOf("/"));

  String[] lastFileName = fileName.split("//.");

  fileName = lastFileName[0] + "." + lastFileName[2];

  File file = new File(dirs);

  if (!file.exists()) {
   file.mkdirs();
  }
  file = new File(fileName);

  return file;
 }
}

 

 

ZipOutputStreamTest .java

package cn.com.io;

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 ZipOutputStreamTest {

 
 private ZipOutputStream out;
 public ZipOutputStreamTest(String aimZipName){
  try {
   out = new ZipOutputStream(new FileOutputStream(aimZipName));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
 }
 
 public static void main(String[] args) {
  
  ZipOutputStreamTest zst=new ZipOutputStreamTest("E:/myMusic.zip");
//  zst.fileToZip("e:/NineNine.txt");
  zst.fileListToZip("E:/Music");
  try {
   zst.out.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 
 public void fileToZip(String fromFile){
  File file=new File(fromFile);
  try {
   InputStream in=new FileInputStream(file);
   out.putNextEntry(new ZipEntry(file.getName()));
   byte[] buff=new byte[1024];
   int len=0;
   while((len=in.read(buff))!=-1){
    out.write(buff, 0, len);
   }
   
   out.close();
   in.close();
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  } catch (IOException e) {
   
   e.printStackTrace();
  }
 }
 
 public void fileToZip(File fromFile){
  try {
   InputStream in=new FileInputStream(fromFile);
   out.putNextEntry(new ZipEntry(fromFile.getPath().substring(fromFile.getPath().indexOf(":")+1)+ fromFile.getName()));
   
   byte[] buff=new byte[1024];
   int len=0;
   while((len=in.read(buff))!=-1){
    out.write(buff, 0, len);
    out.flush();
   }
   
   in.close();
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  } catch (IOException e) {
   
   e.printStackTrace();
  }
 }
 
 
 
 public void fileListToZip(File file){
  if (file.isFile()) {
   fileToZip(file);
  } else {
   File[] files = file.listFiles();
   for (File fileList : files) {
    fileListToZip(fileList);
   }
  }
 }
 
 public void fileListToZip(String fileName){
  fileListToZip(new File(fileName));
  System.out.println("文件压缩成功!");
 }
 
}

你可能感兴趣的:(IO输入输出流)