java小应用
1,程序給你免費試用5次,之后不給你玩了
package javaBase.io.others; import java.io.*; import java.util.*; /** 程序給你免費試用5次,之后不給你玩了*/public class BetaPrograme {public static void main(String[] args) throws IOException {File file = new File("sys.properties");//用File類if(!file.exists())file.createNewFile();//文件不存在就創建一個InputStream in = new FileInputStream(file);//讀取流Properties prop = new Properties();prop.load(in);int count = 0;//試用次數String value = prop.getProperty("times");if(value!=null){count = Integer.parseInt(value);if(count>=5){System.out.println("您好,您的試用次數已到,請付費!");return;}}count++;prop.setProperty("times", count+"");//寫入文件中OutputStream out = new FileOutputStream(file);prop.store(out, "");out.close();in.close();} }2,將幾個文本文件合成一個文件
package javaBase.io; import java.io.*; import java.util.Enumeration; import java.util.Vector;//(ArrayList非線程安全) /** 將3個文本文件合成一個文本文件*/ public class MergeText {public static void main(String[] args) throws IOException {Vector<FileInputStream> v = new Vector<FileInputStream>();v.add(new FileInputStream("1.txt"));v.add(new FileInputStream("2.txt"));v.add(new FileInputStream("3.txt"));Enumeration<FileInputStream> en = v.elements();SequenceInputStream sin = new SequenceInputStream(en);//合并流FileOutputStream out = new FileOutputStream("4.txt");byte[] buff = new byte[1024];int len = 0;while((len = sin.read(buff))!=-1){out.write(buff,0,len);}out.close();sin.close();} }3,文件分割及合并
package javaBase.io;import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; /** 分割文件(媒體文件)然后還原*/ public class SplitFile {public static void main(String[] args) throws IOException {split();merge();}/** 分割文件,最大為1M*/public static void split() throws IOException{FileInputStream in = new FileInputStream("man.mp3");FileOutputStream out = null;byte[] buff = new byte[1024*1024];//1M 每讀取1M寫入文件int len = 0;int count = 1;while((len = in.read(buff))!=-1){out = new FileOutputStream("man"+(count++)+".part");out.write(buff,0,len);//僅寫入有效數據out.close();}in.close(); }/** 合并文件*/public static void merge() throws IOException{ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();for(int i=1;i<=4;i++){//總共有4個文件list.add(new FileInputStream("man"+i+".part"));}//vector可以直接調用elements()方法變成enumeration;//ArrayList用迭代器:final Iterator<FileInputStream> it = list.iterator();//匿名內部類要使用,所以用finalEnumeration<FileInputStream> en = new Enumeration<FileInputStream>() {@Overridepublic FileInputStream nextElement() {return it.next();}@Overridepublic boolean hasMoreElements() {return it.hasNext();}};//合并流SequenceInputStream sin = new SequenceInputStream(en);FileOutputStream out = new FileOutputStream("man_merge.mp3");byte[] buff = new byte[1024*1024];//這里只有1M,若是太大的話會內存溢出,可以用循環每100M新建一個輸出流int len = 0;while((len = sin.read(buff))!=-1){out.write(buff,0,len);}out.close();sin.close();} }轉載于:https://www.cnblogs.com/xukunn/p/4080376.html
總結
- 上一篇: 为安装有系统及应用的服务器更换硬盘方法一
- 下一篇: 基本概念-编写第一个C程序