12.Java中的IO流(三)——Properties与IO体系中的其他流【Java学习笔记Hatter】Java开发工程师

/ 厦门大学环境科学与工程学院Java开发工程师 / 2017-03-13


1.Properties是hashtable的子类。

也就是说它具备Map集合的特点,并且它里面存储的键值对都是字符串

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

该对象的特点:可以用于键值对形式的配置文件,那么加载数据时需要数据有固定格式,

通常为: 键=值。


例:

public class PrppertiesDemo {

public static void main(String[] args) {

setAndGet();  

}


public static void setAndGet(){


Properties pp=new Properties();

pp.setProperty("zhangsan", "21");

pp.setProperty("lisi", "23");


String value=pp.getProperty("lisi");

//System.out.println(value);

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

Set<String>names=pp.stringPropertyNames();


for(String s : names){

System.out.println(s+"::"+pp.getProperty(s));

}

}

}


2.打印流(PrintWriter)

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

(1)字节打印流:PrintStream

构造函数可以接收的参数类型:

①file对象。File

②字符串路径。String

③字节输出流:OutputStream

④字符输出流:Writer


(2)字符打印流:PrintWriter

例:

public class PrintStreamDemo {

public static void main(String[] args) throws IOException {


BufferedReader br=

new BufferedReader(new InputStreamReader(System.in));


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

String line=null;


while((line=br.readLine())!=null){

if("over".equals(line))

break;


out.println(line.toUpperCase());

out.flush();

}

out.close();

br.close();

}

}


3.切割

public class SplitFile {

public static void main(String[] args) throws IOException {

spiltFile();

}


public static void spiltFile() throws IOException{


FileInputStream fis=new FileInputStream("d://0.mp3");

FileOutputStream fos=null;


byte[]buf=new byte[2*1024*1024];

int len=0;

int count=1;


while((len=fis.read(buf))!=-1){

fos=new FileOutputStream("d://spilt//"+(count++)+".part");

fos.write(buf,0,len);

fos.close();

}


if(fos!=null)

fos.close();

fis.close();


公众号,微信

汇鱼网海峡创乐汇
汇鱼网海峡创乐汇