天天看點

黑馬程式員_<<properties,列印流,合并流,分割流>>1. Properties2. 列印流3. 合并流4. 切割流

--------------------ASP.Net+Android+IOS開發、.Net教育訓練、期待與您交流! --------------------

1. Properties

         1.概述

Properties是Hashtable的子類,存儲的格式那麼也是鍵值對,但是鍵和值都是字元串類型

        2.常用方法

public class PropertiesDemo1 {
   public static void main(String[] args) {
     Properties pro = new Properties();
     /* setProperty(String key,String value)添加鍵和值 */
     pro.setProperty("java01", "001");
     pro.setProperty("java02", "002");
     /* 通過鍵獲得值:String getProperty(String key) */
     System.out.println(pro.getProperty("java01"));
     /* Set<String>stringPropertyNames()獲得鍵集 */
     Set<String> set = pro.stringPropertyNames();
     for (String s : set)
        System.out.println(s + ":" + pro.getProperty(s));
   }
 
}
結果:
001
java02:002
java01:001
           

            3.配置檔案

将配置檔案中的資訊存到集合中,然後修改其鍵和值,然後在傳給檔案。配置檔案中的資訊都是用=号存儲的,例如:張三=001

第一種方法:我們不使用Properties對象,就是用集合來操作,思路:我們将檔案讀取出來,然後對每一行進行用=分割,把左邊的存為鍵,把右邊的存為值。

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
 
public class PropertiesDemo1 {
   public static void main(String[] args) throws IOException {
     BufferedReader br = new BufferedReader(new FileReader("F:\\pro.txt"));//讀取流
     HashMap<String, String> map = new HashMap<String, String>();//map集合
     String line = null;
     while ((line = br.readLine()) != null) {
        String[] s = line.split("=");
        map.put(s[0], s[1]);
     }
     System.out.println(map);
   }
 
}
結果:
{java02=002, java03=003, java01=001}
           

第二種方法:我們使用Properties對象,這樣我們可以友善的加載流,來操作檔案。

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Properties;
import java.util.Set;
 
public class PropertiesDemo1 {
   public static void main(String[] args) throws IOException {
     /* void load(Reader reader)将讀取流字元流加載到集合中 */
     Properties pro = new Properties();
     FileReader fr = new FileReader("F:\\pro.txt");
     pro.load(fr);// 将字元讀取流中讀取的檔案放到Properties對象中
     System.out.println("加載後的集合:" + pro);
     /* 下面我們修改集合中的數值 */
     pro.setProperty("java02", "hello");
     /*
      * store(Writer writer,String
      * comments)通過字元寫入流,把集合中的資訊更新配置檔案,comments是注視内容
      */
     FileWriter fw = new FileWriter("F:\\pro.txt");
     pro.store(fw, "java");// 更新配置檔案,注釋為:java
     fr.close();
     fw.close();
   }
 
}
結果:
加載後的集合:{java03=003, java02=002, java01=001}
           

          4.計算程式運作次數

importjava.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Properties;
 
public class PropertiesDemo {
 
      publicstatic void main(String[] args) throws IOException {
           /*
            * 計算程式的運作次數 思路: 1.建立一個配置檔案,然後然後裡面定義了一個計數器
            * 2.每次運作的時候,都取出來,然後自增,然後再存回去
            */
 
           Filefile = new File("F:\\pro.ini");
           if(!file.exists())
                 file.createNewFile();
 
           Propertiespro = new Properties();
           pro.load(newFileInputStream(file));// 加載流
           Stringvalue = pro.getProperty("count");
           intcount = 0;
           if(value != null)
                 count= Integer.parseInt(value);
           count++;//自增
           pro.setProperty("count",count + "");// 改變值
           pro.store(newFileOutputStream(file), "java for count");// 更新檔案
           System.out.println(pro);
      }
 
}結果:
運作一次,count就+1.
           

2. 列印流

     PrintStream:位元組列印流

          其構造方法傳入的參數:

                File對象,字元串路徑,位元組流。

     PrintWriter:字元列印流

          其構造方法傳入的參數有:

              File對象,字元串路徑,位元組流,字元流。

     另外注意的是:字元流中傳入的是流的話,可以設定自動重新整理,隻有使用println,printf,format方法可以自動重新整理。

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
 
public class PrintStreamDemo {
   public static void main(String[] agrs) throws IOException {
     // File f=newFile("F:\\demo.txt");
     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     PrintWriter pw = new PrintWriter(System.out,true);//true表示自動重新整理,但是必須是println方法
     String line = null;
     while ((line = br.readLine()) != null) {
        if ("over".equals(line))
          return;
        pw.println("内容是:" + line);
        //pw.flush();
 
     }
     br.close();
     pw.close();
   }
}
           

現在是把内容列印在了控制台上,也可以列印在檔案中,把PrintWriter流對象更改一下就可以的啊。

3. 合并流

          将多個流合并成一個流,便于操作

  例如:三個檔案的内容寫入到第四個檔案中,那先把指定到前三個檔案的流合并成一個流,然後指向第四個進行讀取後,寫入

     1.可以現将兩個流合并到一個流,然後再把合并流和另外一個流再合并,public SequenceInputStream(InputStreams1,InputStream s2)

     2.也可以使用集合,public SequenceInputStream(Enumeration<? extends InputStream> e)

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
import java.io.FileInputStream;
 
public class SqueDemo {
  public static void main(String[] agrs) throws IOException {
    Vector<FileInputStream> v = new Vector<FileInputStream>();
    v.add(new FileInputStream("F:\\1.txt"));
    v.add(new FileInputStream("F:\\2.txt"));
    v.add(new FileInputStream("F:\\3.txt"));
    Enumeration<FileInputStream> en = v.elements();
    SequenceInputStreamsis = new SequenceInputStream(en);
 
    FileOutputStream out = new FileOutputStream("F:\\4.txt");
    byte[] b = new byte[1024];
    int len = 0;
    while ((len = sis.read(b)) != -1) {
      out.write(b, 0, len);
      out.flush();
    }
    sis.close();
    out.close();
 
  }
}
           

4. 切割流

将圖檔分割後,然後在合并

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
 
public class PhotoDemo {
  public static void main(String[] args) throws IOException {
 
    FenGe();
 
    HeBing();
 
  }
 
  private static void HeBing() throws FileNotFoundException, IOException {
    /* 其實這個也可以使用循環,然後添加到集合, */
    Vector<FileInputStream> v = new Vector<FileInputStream>();
    v.add(new FileInputStream("F:\\part\\1.part"));
    v.add(new FileInputStream("F:\\part\\2.part"));
    v.add(new FileInputStream("F:\\part\\3.part"));
    v.add(new FileInputStream("F:\\part\\4.part"));
    v.add(new FileInputStream("F:\\part\\5.part"));
    Enumeration<FileInputStream> en = v.elements();
    SequenceInputStream sis = new SequenceInputStream(en);
 
    FileOutputStream out = new FileOutputStream("F:\\part\\1.bmp");
    byte[] b = new byte[1024];
    int len = 0;
    while ((len = sis.read(b)) != -1) {
      out.write(b, 0, len);
      out.flush();
    }
    sis.close();
    out.close();
  }
 
  /* 分割 */
  private static void FenGe() throws FileNotFoundException, IOException {
    FileInputStream input = new FileInputStream("F:\\1.png");
 
    FileOutputStream out = null;
    byte[] buf = new byte[1024 * 100];
    int count = 1;
    int len = 0;
    while ((len = input.read(buf)) != -1) {
      out = new FileOutputStream("F:\\part\\" + (count++) + ".part");// 分割後的字尾名自己可以自定義
      out.write(buf, 0, len);
      out.flush();
      out.close();
 
    }
    input.close();
  }
 
}
           
黑馬程式員_&lt;&lt;properties,列印流,合并流,分割流&gt;&gt;1. Properties2. 列印流3. 合并流4. 切割流

其實也可以分割媒體,電影和音樂的。

--------------------ASP.Net+Android+IOS開發、.Net教育訓練、期待與您交流! --------------------