您的位置:首页 > 职场人生

“黑马程序员"Properties PrintWriter 合并流 切割

2013-07-05 13:06 387 查看
----- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------

Properties是hashtable的子类。
也就是说它具备mpa集合的特点,而且它里面存储的键值对都是字符串,

是集合中和IO技术相结合的集合容器。

该对象的特点:可以用于键值对形式的配置文件。

那么在加载数据时,需要数据有固定格式:键=值
*/
import java.io.*;
import java.util.*;
class PropertiesDemo
{
public static void main(String[] args)throws IOException
{
//setAndGet();
loadDemo();

}

public static void loadDemo()throws IOException
{
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("info.txt");

//将流中的数据加载进集合。
prop.load(fis);

prop.setProperty("wangwu","39");

FileOutputStream fos= new FileOutputStream("info.txt");
prop.store(fos,"haha");
fos.close();
fis.close();

System.out.println(prop);
//prop.list(System.out);
}

//演示,如何将流中的数据存储到集合中
//想要将info.txt中的键值数据存到集合中进行操作。
/*
1.用一个流和info.txt文件关联。
2.读取一行数据,将该行数据用"="进行切割。
3.等号左边作为键,右边作为值,存入到Properties集合中即可。
*/
public static void method_1()throws IOException
{
BufferedReader bufr = new BufferedReader(new FileReader("info.txt"));
String line = null;
Properties prop = new Properties();
while((line=bufr.readLine())!=null)
{
String[] arr = line.split("=");
//System.out.println(arr[0]+"..."+arr[1]);
prop.setProperty(arr[0],arr[1]);
}
bufr.close();

System.out.println(prop);

}

//设置和获取元素
public static void setAndGet()
{
Properties prop = new Properties();

prop.setProperty("zhangsan","30");
prop.setProperty("lisi","39");

//System.out.println(prop);
//String  value = prop.getProperty("lisi");
//System.out.println(value);

prop.setProperty("lisi","89");

Set<String> names = prop.stringPropertyNames();
for(String s :names)
{
System.out.println(s+":"+prop.getProperty(s));
}

/*
Iterator<String>it = set.iterator();
while(it.hasNext())
{
System.out.println(prop.get(it.next()));
}
*/
}
}

——————————————————————————————————————————————

/*
用于记录应用程序运行次数。
如果使用次数已到,那么给出注册提示。

很容易想到的是:计数器。
可是该计数器定义在程序中,随着程序的运行而在内存中存在,并进行了自增。
可是随着该应用程序的退出,该计数器也在内存中消失了。

下一次再启动该程序,又重新开始从零计数。
这样不是我们想要的。

程序即使结束,该计数器的值也存在。
下一次程序启动会先加载该计数器的值并加1后再重新存储起来。

所以要建立一个配置文件,用于记录该软件的使用次数。

该配置文件使用键值对形式。
这样便于阅读数据并操作数据。

键值对数据是map集合。
数据是以文件形式存储。使用IO技术。
那么map+io就是Properties

配置文件可以实现应用程序数据的共享。
*/
import java.io.*;
import java.util.*;
class RunCount
{
public static void main(String[] args)throws IOException
{
Properties prop = new Pro
4000
perties();

File file = new File("count.ini");
if(!file.exists())
file.createNewFile();

FileInputStream fis = new FileInputStream(file);

prop.load(fis);

int count = 0;
String value = prop.getProperty("time");

if(value!=null)
{
count = Integer.parseInt(value);
if(count>=5)
{
System.out.println("您好,使用次数已到,请购买注册码");
return;
}
}

count++;

prop.setProperty("time",count+"");

FileOutputStream fos = new FileOutputStream(file);
prop.store(fos,"haha");
fos.close();
fis.close();
}
}

<persons>
<person id="001">
<name>zhangsan</name>
<age>30</age>
<address>bj</address>
</person>
<person>
<name>
</person>
</persons>

dom4jdom for java

——————————————————————————————————————————————

/*
打印流:
该流提供了打印方法,可以将各种数据类型的数据都原样打印。

字节打印流:
PrintStream
构造函数可以接收的参数类型:
1.file对象 File
2.字符串路径 String
3.字节输出流 OutputStream

字符打印流:
PrintWriter
1.file对象 File
2.字符串路径 String
3.字节输出流 OutputStream
4.字符输出流 Writer
*/
import java.io.*;
class PrintStreamDemo
{
public static void main(String[] args)throws IOException
{
BufferedReader bufr = 
new BufferedReader(new InputStreamReader(System.in));

PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);

String line = null;

while((line=bufr.readLine())!=null)
{
if("over".equals(line))
break;
out.println(line.toUpperCase());
//out.flush();
}
out.close();
bufr.close();

}
}

————————————————————————————————————————————

import java.io.*;
import java.util.*;
class SequenceDemo
{
public static void main(String[] args)throws IOException
{
Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("c:\\1.txt"));
v.add(new FileInputStream("c:\\2.txt"));
v.add(new FileInputStream("c:\\3.txt"));

Enumeration<FileInputStream> en = v.elements();
SequenceInputStreamsis = new SequenceInputStream(en);

FileOutputStream fos = new FileOutputStream("c:\\4.txt");

byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();

/*
BufferedReader bufr = new BufferedReader(new InputStreamReader(sis)) ;

BufferedWriter bufw = new BufferedWriter(new FileWriter("c:\\123.txt"));

String line = null;
while((line=bufr.readLine())!=null)
{
bufw.write(line);
bufw.newLine();
}
bufw.close();
bufr.close();
*/
}
}

——————————————————————————————————————————————

import java.io.*;
import java.util.*;
class SplitFile
{
public static void main(String[] args)throws IOException
{
//splitFile();
merge();
}

public static void merge()throws IOException
{
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for(int x=1;x<=3;x++)
{
al.add(new FileInputStream("c:\\splitfiles\\"+x+".part"));
}
final Iterator<FileInputStream>it = al.iterator();

Enumeration<FileInputStream> en = new Enumeration<FileInputStream>()
{
public boolean hasMoreElements()
{
return it.hasNext();
}
public FileInputStream nextElement()
{
return it.next();
}
};

SequenceInputStream sis = new SequenceInputStream(en);

FileOutputStream fos = new FileOutputStream("c:\\splitfiles\\0.mp3");

byte[] buf = new byte[1024];
int len = 0;
while((len=sis.read(buf))!=-1)
{
fos.write(buf,0,len);
}
fos.close();
sis.close();

}

public static void splitFile()throws IOException
{
FileInputStream fis = new FileInputStream("C:\\demo.mp3");

FileOutputStream fos = null;

byte[] buf = new byte[1024*1024];
int len = 0;
int count =1;
while((len=fis.read(buf))!=-1)
{
fos = new FileOutputStream("c:\\splitfiles\\"+(count++)+".part");
fos.write(buf,0,len);
fos.close();
}

fis.close();

}
}

----- <a href="http://www.itheima.com" target="blank">android培训</a>、<a href="http://www.itheima.com" target="blank">java培训</a>、期待与您交流! ----------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐