日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java小应用

發布時間:2025/3/21 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的java小应用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。