java__properties和转换流(字节转字符流)

package cn.itcast.other;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

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


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




 */

public class Demo4 {

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

//      readProperties();
    }

    //读取配置文件爱你的信息 
    public static void readProperties() throws IOException{
        //创建Properties对象
        Properties properties = new Properties();
        //加载配置文件信息到Properties中
        properties.load(new FileReader("F:\\persons.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:\\persons.properties"), "hehe");


    }



    //保存配置文件文件的信息。
    public static void creatProperties() throws IOException{
        //创建Properties
        Properties properties = new Properties();
        properties.setProperty("狗娃", "123");
        properties.setProperty("狗剩","234");
        properties.setProperty("铁蛋","345");
        // 遍历Properties
        /*Set<Entry<Object, Object>> entrys = properties.entrySet();
        for(Entry<Object, Object> entry  :entrys){
            System.out.println("键:"+ entry.getKey() +" 值:"+ entry.getValue());
        }*/

        //使用Properties生产配置文件。
        //properties.store(new FileOutputStream("F:\\persons.properties"), "haha"); //第一个参数是一个输出流对象,第二参数是使用一个字符串描述这个配置文件的信息。
        properties.store(new FileWriter("F:\\persons.properties"), "hehe");
    }

}
package cn.itcast.other;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/*

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

 */

public class Demo5 {

    public static void main(String[] args) throws IOException {
        File file = new File("F:\\count.properties");
        if(!file.exists()){
            //如果配置文件不存在,则创建该配置文件
            file.createNewFile();
        }

        //创建Properties对象。
        Properties properties = new Properties();
        //把配置文件的信息加载到properties中
        properties.load(new FileInputStream(file));
        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");

    }

}
package cn.itcast.other;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

/*
打印流(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 Demo6 {

    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);

        }



    }

}
package cn.itcast.other;

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);




    }

}

这里写图片描述

package cn.itcast.other;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
/*
 转换流:

输入字节流的转换流:InputStreamReader 是字节流通向字符流的桥
    InputStreamReader

输出字节流的转换流:
    OutputStreamWriter   可以把输出字节流转换成输出字符流 。  


转换流的作用:
    1. 如果目前所 获取到的是一个字节流需要转换字符流使用,这时候就可以使用转换流。  字节流----> 字符流
    2. 使用转换流可以指定编码表进行读写文件。


 */


public class Demo8 {

    public static void main(String[] args) throws IOException {
//      readTest();
//      writeTest();'

//      writeTest2();
        readTest2();
    }

    //使用输入字节流的转换流指定码表进行读取文件数据
    public static void readTest2() throws IOException{
        File file = new File("F:\\a.txt");
        FileInputStream fileInputStream = new FileInputStream(file);
        //创建字节流的转换流并且指定码表进行读取
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"utf-8");
        char[] buf = new char[1024];
        int length = 0;
        while((length = inputStreamReader.read(buf))!=-1){
            System.out.println(new String(buf,0,length));
        }

    }





    //使用输出字节流的转换流指定码表写出数据
    public static void writeTest2() throws IOException{
        File file = new File("F:\\a.txt");
        //建立数据的输出通道
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //把输出字节流转换成字符流并且指定编码表。
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "utf-8");
        outputStreamWriter.write("新中国好啊");
        //关闭资源
        outputStreamWriter.close();

    }



    public static void writeTest() throws IOException{
        File file = new File("F:\\a.txt");
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        //把输出字节流转换成输出字符流。
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);

        outputStreamWriter.write("大家好");
        outputStreamWriter.close();

    }




    public static void readTest() throws IOException{
        InputStream in = System.in; //获取了标准的输入流。
//      System.out.println("读到 的字符:"+ (char)in.read());  //read()一次只能读取一个字节。
        //需要把字节流转换成字符流。
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        //使用字符流的缓冲类
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = null;
        while((line = bufferedReader.readLine())!=null){
            System.out.println("内容:"+ line);
        }


    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值