--------------------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();
}
}
其实也可以分割媒体,电影和音乐的。
--------------------ASP.Net+Android+IOS开发、.Net培训、期待与您交流!
--------------------
分享到:
相关推荐
<properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> ...
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-common</...
系统设计<br> 1 jive设计思路 <br> 2 jive的工作内幕 <br> 3 Jive源代码研究 <br> 4 Jive中的设计模式 <br> 5 jive学习笔记 <br><br><br><br><br>数据库设计<br> 1 Jive Forums数据库说明(英文) <br> 2 Jive KB...
<Properties/> </Field> <Field name="DEPT_ID" label="部门" dataType=""> <Properties/> </Field> </Fields> <Parameters/> <Properties/> </Dataset> <Dataset type="Custom" listener=...
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- 使用spring-core --> <dependency> <groupId>org.springframework</groupId> ...
<br>系统设计<br> 1 jive设计思路 <br> 2 jive的工作内幕 <br> 3 Jive源代码研究 <br> 4 Jive中的设计模式 <br> 5 jive学习笔记 <br><br><br> <br>设计模式<br> 1 大道至简-Java之23种模式一点就通 <br> 2 设计模式...
<properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </...
<br><br> <br><br>System monitoring 系统监控 <br><br> <br><br>Memory monitoring 内存监控 <br><br> <br><br>Cluster information 集群信息 <br><br> <br><br>Log file tailing 实时追踪log文件 <br><br> <br><br...
<properties> <elasticsearch.version>5.6.0</elasticsearch.version> <spring-version>5.0.0.RC3</spring-version> <fastjson-version>1.2.7</fastjson-version> <junit-version>4.12</junit-version> <slf4j...
<properties> <tk-mapper.version>4.1.5</tk-mapper.version> <pagehelper.version>1.2.10</pagehelper.version> </properties> <dependencies> <!-- Mybatis通用Mapper --> <dependency> <groupId...
</properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org....
Conclusion <br>730 <br>References <br>732 <br>Appendix A Measurement Conversion Tables <br>735 <br>Appendix <br>B Physical Constants <br>737 <br>Appendix <br>C Index of Professional Organizations <br...
Conclusion <br>730 <br>References <br>732 <br>Appendix A Measurement Conversion Tables <br>735 <br>Appendix <br>B Physical Constants <br>737 <br>Appendix <br>C Index of Professional Organizations <br...
1、整合准备 整合内容: 整合架构:Spring、... <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build> </project>
><br><ARCXML version="1.1"><br><REQUEST><br><GET_IMAGE><br><PROPERTIES><br><ENVELOPE minx="-125" miny="25" maxx="-67" maxy="50" /><br><IMAGESIZE width="700" height="500" /><br></PROPERTIES><br></GET_...
<value>WEB-INF/jdbc.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" dependency-check="none...
<properties> <springside.version>4.2.2.GA</springside.version> </properties> <dependency> <groupId>org.springside</groupId> <artifactId>springside-core</artifactId> <version>${springside.version...
<include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> ...
1. mvn-examples-1.0 ... <module>ch13-properties</module> <module>ch15-sitegen</module> <module>ch17-writing-plugins</module> <module>ch18-alternate-plugins</module> </modules> 2.Maven权威指南中文版
<properties> <property> <name>user</name> <!--value>df80_20171113</value--> <!--value>sd_2017</value--> <value>gyl_v80_6008</value> </property> </properties> <password-encrypted>...