练习1:将多个文件合并成一个文件
package io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
/*
* 练习:将3个文件中的内容保存到一个文件中去。
*/
public class SequenceInputStreamDemo {
public static void method_1() throws IOException {
//设置文件目录
String dir = "E:"+File.separator+"BaiduYunDownload"+File.separator+"FILE"+File.separator;
//这里要用到Enumeration对象,所以要使用到Vector集合
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream(new File(dir+"1.txt")));
v.add(new FileInputStream(new File(dir+"2.txt")));
v.add(new FileInputStream(new File(dir+"3.txt")));
Enumeration<FileInputStream> e = v.elements();
//创建合并流
SequenceInputStream sis = new SequenceInputStream(e);
int ch = 0;
//设置输出文件
File f = new File(dir+"123.txt");
if(!f.exists()){
f.createNewFile();
}
BufferedWriter bw = new BufferedWriter(new FileWriter(f));
while((ch=sis.read())!=-1){
bw.write(ch);
}
System.out.println("合并完成");
//输出完成关闭流
bw.close();
sis.close();
}
public static void main(String[] args) {
try {
method_1();
} catch (IOException e) {
e.printStackTrace();
}
}
}
练习2:用于记录应用程序的运行次数,如果使用次数已到,那么给出注册提示。
package properties;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import java.util.Set;
/*
* Properties是HashTable的子类
* 也就是说它具备Map集合的特点。而且它里面存储的键值对都是字符串
*
* 是集合和IO技术相结合的容器
*
* 该对象的特点:可以用键值对形式的配置文件
*/
/*
* 练习:用于记录应用程序的运行次数,如果使用次数已到,那么给出注册提示。
*
* 分析: 记载运行次数需要用到计数器,但是简单的计数器是在内存中进行计数的,一旦程序关闭计数又会从0开始,
* 因此需要将计数持久化,即保存在磁盘上,每回程序运行前要先加载判断一下,然后再接着计数
*/
public class PropertiesDemo {
public static void main(String[] args) {
Properties prop = new Properties();
// setsMethod(prop);
// getsMethod(prop);
// String src = "E:"+File.separator+"BaiduYunDownload"+File.separator+"FILE"+File.separator+"1.txt";
// txtToProperties(src,prop);
// prop.setProperty("test3", "ccc");
// propertiesToTXT(src,prop);
runtimeCount(prop);
}
//设置Properties的值
private static void setsMethod(Properties prop){
prop.setProperty("test1", "AAA");
prop.setProperty("test2", "BBB");
System.out.println(prop);
}
//获取Properties中的键值
private static void getsMethod(Properties prop){
Set<String> s = prop.stringPropertyNames();
for(String key : s){
String value = prop.getProperty(key);
System.out.println(key+"::"+value);
}
}
//将txt文件中的键值数据存放到集合中,自定义方法
private static void txtToProperties(String src,Properties prop){
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(new File(src)));
String line = null;
while((line=br.readLine())!=null){
String[] s = line.split("=");
if(s.length==2){
prop.setProperty(s[0], s[1]);
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(prop);
}
//将txt文件中的键值数据存放到集合中,Properties的load(...)方法
private static void txtToProperties2(String src,Properties prop){
BufferedReader br = null;
try{
br = new BufferedReader(new FileReader(new File(src)));
prop.load(br);
}catch(IOException e){
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
System.out.println(prop);
}
//更改配置文件中的值
private static void propertiesToTXT(String src,Properties prop){
PrintWriter pw = null;
try{
pw = new PrintWriter(new File(src));
prop.list(pw);
}catch(IOException e){
e.printStackTrace();
}finally{
if(pw!=null){
pw.close();
}
}
System.out.println(prop);
}
//练习:用于记录应用程序的运行次数,如果使用次数已到,那么给出注册提示
private static void runtimeCount(Properties prop){
BufferedReader br = null;
BufferedWriter bw = null;
File file = new File("E:"+File.separator+"BaiduYunDownload"+File.separator+"FILE"+File.separator+"COUNT.ini");
int count = 1;
try {
if(!file.exists()){
file.createNewFile();
}
br = new BufferedReader(new FileReader(file));
prop.load(br);
String value = prop.getProperty("count");
if(value!=null){
count = Integer.parseInt(value);
if(count>=5){
System.out.println("请注册!");
return;
}
System.out.println("已使用"+count+"次");
}
count++;
prop.setProperty("count", count+"");
bw = new BufferedWriter(new FileWriter(file));
prop.store(bw, "");
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(br!=null){
br.close();
}
if(bw!=null){
bw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
本文介绍了如何使用Java进行文件合并操作,并实现了一个用于记录应用程序运行次数的简单实例,当达到预设次数时,程序会提示用户进行注册。
412

被折叠的 条评论
为什么被折叠?



