Properties(配置文件类): 主要用于生产配置文件与读取配置文件的信息。

  Properties要注意的细节:
     1. 如果配置文件的信息一旦使用了中文,那么在使用store方法生成配置文件的时候只能使用字符流解决,如果使用字节流生成配置文件的话,
      默认使用的是iso8859-1码表进行编码存储,这时候会出现乱码。
     2. 如果Properties中的内容发生了变化,一定要重新使用Properties生成配置文件,否则配置文件信息不会发生变化。

import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties; //存配置文件
public class Demo1 {
public static void main(String[] args) throws IOException {
Properties p = new Properties();
p.setProperty("李杰", "999");
p.setProperty("李英", "666");
p.setProperty("李汉三", "888"); /*复习遍历(properties基于hashtable基于Map,所以用entrySet遍历)
Set<Entry<Object, Object>> a = p.entrySet();
for(Entry<Object, Object> b:a) {
System.out.println(b);
}*/ //使用Properties产生配置文件
//方法一:使用字节输出流无法输出汉字,只会出现乱码,因为底层用的是拉丁字符表
p.store(new FileOutputStream("F:\\a.properties"), "way:1");
//方法二:使用字符输出流输出汉字,底层用的是GBK表
p.store(new FileWriter("F:\\b.properties"), "way:2");//way:2处还是无法正常解码中文,需要注意
}
}
//加载、修改配置文件
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set; public class Demo2 {
public static void main(String[] args) throws IOException {
readProperties();
}
public static void readProperties() throws IOException{
Properties properties = new Properties();
properties.load(new FileReader("F:\\b.properties"));
//遍历properties判断是否加载成功
Set<Entry<Object, Object>> entrys = properties.entrySet();
for(Entry<Object, Object> entry :entrys){
System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());
} //修改李杰的密码,把修改后的Properties再生成一个配置文件
properties.setProperty("李杰", "007");
properties.store(new FileWriter("F:\\b.properties"), "ways:2"); } }

方法一效果

#way:1
#Sat Aug 04 11:08:30 CST 2018
\u674E\u6770=999
\u674E\u6C49\u4E09=888
\u674E\u82F1=666

方法二效果

#ways:2
#Sat Aug 04 11:08:37 CST 2018
李杰=007
李汉三=888
李英=666

练习: 使用properties实现本软件只能 运行三次,超过了三次之后就提示购买正版,退jvm.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties; public class Demo3 { public static void main(String[] args) throws IOException {
File file = new File("F:\\count.properties");
if(!file.exists()){
file.createNewFile();//如果配置文件不存在,则创建该配置文件
} Properties properties = new Properties();
properties.load(new FileInputStream(file));//把配置文件的信息加载到properties中
FileOutputStream fileOutputStream = new FileOutputStream(file);//必须放在加载之后,不然他会清空文件内容 int count = 0; //定义该变量是用于保存软件的运行次数的。
String value = properties.getProperty("count");//读取配置文件的运行次数
if(value!=null){
count = Integer.parseInt(value);
}
//判断使用的次数是否已经达到了三次,
if(count==3){
System.out.println("你已经超出了试用次数,请购买正版软件!!");
System.exit(0);
}
count++;
System.out.println("你已经使用了本软件第"+count+"次");
properties.setProperty("count",count+"");
//使用Properties生成一个配置文件
properties.store(fileOutputStream,"runtime");
}
}

打印流(printStream)  打印流可以打印任意类型的数据,而且打印数据之前都会先把数据转换成字符串再进行打印。

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream; class Animal{ String name;
String color; public Animal(String name,String color){
this.name = name;
this.color = color;
} @Override
public String toString() {
return "名字:"+this.name+ " 颜色:"+ this.color;
}
} public class Demo4 { public static void main(String[] args) throws IOException {
/*FileOutputStream fileOutputStream = new FileOutputStream("F:\\a.txt");
fileOutputStream.write("97".getBytes());
fileOutputStream.close();*/ File file = new File("F:\\a.txt");
//创建一个打印流
PrintStream printStream = new PrintStream(file);
/*
printStream.println(97);
printStream.println(3.14);
printStream.println('a');
printStream.println(true);
Animal a = new Animal("天空", "蓝色");
printStream.println(a); //默认标准的输出流就是向控制台输出的,
System.setOut(printStream); //重新设置了标准的输出流对象
System.out.println("的健身卡好的艰苦");
*/ File logFile = new File("F:\\2015年1月8日.log");
PrintStream logPrintStream = new PrintStream( new FileOutputStream(logFile,true) );
try{
int c = 4/0;
System.out.println("c="+c);
int[] arr = null;
System.out.println(arr.length); }catch(Exception e){
e.printStackTrace(logPrintStream);
}
}
}

编码与解码 
  编码: 把看得懂的字符变成看不懂码值这个过程我们称作为编码。
  解码: 把码值查找对应的字符,我们把这个过程称作为解码。

注意: 以后编码与解码一般我们都使用统一的码表。否则非常容易出乱码。

import java.io.UnsupportedEncodingException;
import java.util.Arrays; public class Demo7 { public static void main(String[] args) throws Exception {
/*
String str = "中国";
byte[] buf = str.getBytes("utf-8");// 平台默认的编码表是gbk编码表。 编码
System.out.println("数组的元素:"+Arrays.toString(buf)); //
str = new String(buf,"utf-8"); //默认使用了gbk码表去解码。
System.out.println("解码后的字符串:"+ str);
*/ /*String str = "a中国"; // ,0, 97, 78, 45, 86, -3
byte[] buf = str.getBytes("unicode"); //编码与解码的时候指定 的码表是unicode实际上就是用了utf-16.
System.out.println("数组的内容:"+ Arrays.toString(buf));
*/ String str = "大家好";
byte[] buf = str.getBytes(); //使用gbk进行编码
System.out.println("字节数组:"+ Arrays.toString(buf)); // -76, -13, -68, -46, -70, -61 str = new String(buf,"iso8859-1");
// 出现乱码之后都可以被还原吗? byte[] buf2 = str.getBytes("iso8859-1");
str = new String(buf2,"gbk");
System.out.println(str);
}
}

Day 19:Properties配置文件类、打印流(printStream) 、 编码与解码的更多相关文章

  1. IO流(五)__文件的递归、Properties、打印流PrintStream与PrintWriter、序列流SequenceInputStream

    一.文件的遍历 1.需求:对指定目录进行所有的内容的列出(包含子目录的内容)-文件的深度遍历 思想:递归的思想,在递归的时候要记住递归的层次. public class FileTest { publ ...

  2. Java IO(Properties/对象序列化/打印流/commons-io)

    Java IO(Properties/对象序列化/打印流/commons-io) Properties Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载. ...

  3. 系统学习 Java IO (十一)----打印流 PrintStream

    目录:系统学习 Java IO---- 目录,概览 PrintStream 类可以将格式化数据写入底层 OutputStream 或者直接写入 File 对象. PrintStream 类可以格式化基 ...

  4. 打印流PrintStream

    打印流PrintStream PrintStream extends OutputStream 1.打印流的特点 只负责数据的输出,不负责数据的读取 与其他的流不同,打印流永远不会抛出IOExcept ...

  5. 打印流-PrintStream

    打印流-PrintStream java.io.PrintStream为其他输出流添加了功能,使其他的流能够更方便的打印各种数据值表现形式 PrintStream特点: 1.只负责数据的输入,不负责数 ...

  6. Java自学第10期——File类与IO流(输入输出流、处理流、转换流、缓冲流、Properties集合、打印流)

    1.IO简介 IO(输入输出)通过java.io包下的类和接口来支持,包下包括输入.输出两种IO流,每种输入输出流又可分为字符流和字节流两大类. 2.File类 File类是io包下与平台无关的文件和 ...

  7. (JAVA)从零开始之--打印流PrintStream记录日志文件

    这里的记录日志是利用打印流来实现的. 文本信息中的内容为String类型.而像文件中写入数据,我们经常用到的还有文件输出流对象FileOutputStream. File file = new Fil ...

  8. 打印流-PrintStream和PrintWriter

    概念: 打印流是输出信息最方便的类,注意包含PrintStream(字节打印流)和 PrintWriter(字符打印流).打印流提供了非常方便的打印功能,可以打印任何类型的数据信息,例如:小数,整数, ...

  9. (23)IO之打印流 PrintStream & Printwriter

    PrintStream PrintStream可以接受文件和其他字节输出流,所以打印流是对普通字节输出流的增强,其中定义了很多的重载的print()和println(),方便输出各种类型的数据. Pr ...

随机推荐

  1. Intent的常用属性之ComponentName

    启动activity的另一种方式 在按钮中添加如下代码 ComponentName componentName=new ComponentName(MainActivity.this,NewActiv ...

  2. LeetCode中等题(一)

    题目一: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  3. day17-Python运维开发基础(类的封装 / 对象和类的相关操作、构造方法)

    1. 类的封装及相关操作 # ### oop 面向对象程序开发 """ #用几大特征表达一类事物称为一个类,类更像是一张图纸,表达的是一个抽象概念 "" ...

  4. 如何给Sqlite添加复合主键

    如果是想两个字段组成一个复合主键的话可以如下.SQL code sqlite> create table t2 ( ...> id1 int , ...> id2 int, ...& ...

  5. 从0开始完成SpringBoot+Mybatis实现增删改查

    1.准备知识: 1)需要掌握的知识: Java基础,JavaWeb开发基础,Spring基础(没有Spring的基础也可以,接触过Spring最好),ajax,Jquery,Mybatis. 2)项目 ...

  6. mabatis--动态sql

    1.mybatis核心,对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装: 2.使用if判断: <where> <if test="customer ...

  7. 39数组中只出现一次的数字+判断的时候一定加上括号,&的优先级低于!=

    题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.     思路:记住位运算的基本操作,与或非,异或,异或是两个数相同则为0,不同为1,理解为加法运 ...

  8. Python Download Image (python + requests + BeautifulSoup)

    环境准备 1 python + requests + BeautifulSoup 页面准备 主页面: http://www.netbian.com/dongman/ 图片伪地址: http://www ...

  9. 用go语言实现磁力猫一样的磁力搜索网站

    1.页面展示 演示站点 2.程序架构 名称 用途 server 收集torrent数据 worker 收集Hash信息 web 数据展示 Tool 工具 3.安装 在安装环境前您需要配置golang环 ...

  10. 前端学习笔记系列一:1.export default / export const

    export default 是默认导出 export const 是命名导出 参考:Javascript (ES6), export const vs export default(基本上就是翻译这 ...