InputStream和OutputStream實例演示

InputStream實例演示

package indi.humman.readAndWrite;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

public class ReadDemo {

    public static void readProperties1() throws IOException{
        URL url = ReadDemo.class.getResource("/dbpro.properties");

        InputStream inputStream = new FileInputStream(url.getFile());
        int i = 0;
        byte[] b = new byte[1024];
        while ((i = inputStream.read(b)) != -1) {
            String str = new String(b,"UTF-8");
            System.out.print(str);
        }
    }

    public static void readProperties2() throws IOException{
        File file = new File("D:/20180421/springDemo/IOTest/target/classes/dbpro.properties");
        InputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[1024];
        int i=0;
        while((i=inputStream.read(bytes))>-1){
            String str = new String(bytes);
            System.out.println(str);
        }
    }

    public static void readProperties3() throws IOException{
        InputStream inputStream = ReadDemo.class.getResourceAsStream("/dbpro.properties");
        byte[] bytes = new byte[1024];
        int i=0;
        while((i=inputStream.read(bytes))>-1){
            String str = new String(bytes);
            System.out.println(str);
        }
    }

    public static void readProperties4() throws IOException{
        InputStream inputStream = ReadDemo.class.getResourceAsStream("/dbpro.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        String jdbc_driver = properties.getProperty("jdbc_driver");
        String jdbc_url = properties.getProperty("jdbc_url");
        String jdbc_username = properties.getProperty("jdbc_username");
        String jdbc_password = properties.getProperty("jdbc_password");
        System.out.println(jdbc_driver);
        System.out.println(jdbc_url);
        System.out.println(jdbc_username);
        System.out.println(jdbc_password);
    }

    public static void main(String[] args) throws IOException {
        readProperties1();
        System.out.println("+++++++++++++++++++++++++++");
        readProperties2();
        System.out.println("+++++++++++++++++++++++++++");
        readProperties3();
        System.out.println("+++++++++++++++++++++++++++");
        readProperties4();

    }
}

OutputStream實例演示

public class WriteDemo {
/**
* 實習向指定文件寫入操作
* @throws IOException
*/
public static void writeProperties1() throws IOException{
File file = new File(“D:/20180421/springDemo/IOTest/target/classes/dbpro.properties”);
OutputStream outputStream = new FileOutputStream(file);
String str =”123455”;
String str2 =”abcdefg”;
outputStream.write(str.getBytes());
outputStream.write(str2.getBytes());
outputStream.flush();
outputStream.close();
}

/**
 * 實現向指定文件寫入Properties鍵值對
 * @throws IOException
 */
public static void writeProperties2() throws IOException{
        //URL url = getURL.class.getClassLoader().getResource("/dbpro.properties");
        URL url = WriteDemo.class.getResource("/dbpro.properties");
        File propFile = new File(url.getFile());
        Properties props = new Properties();
        props.setProperty("monday", "2015-6-14");
        props.setProperty("daysofweek", "2015-6-15");
        OutputStream os = new FileOutputStream(propFile,true);
        props.store(os, "");
        os.flush();
        os.close();
        props.clone();
}
/*
 * 實現向指定文件末端添加數據,不覆蓋原有數據
 */
public static void writeProperties3() throws IOException{
    URL url = WriteDemo.class.getResource("/dbpro.properties");
    File propFile = new File(url.getFile());
    OutputStream outputStream = new FileOutputStream(propFile,true);

    Properties prop = new Properties();
    prop.setProperty("Hello", "world");
    prop.store(outputStream, "1234");
    prop.clone();
    outputStream.flush();
    outputStream.close();

}


public static void main(String[] args) throws IOException {
    writeProperties1();
    System.out.println("++++++++++++++++++++++++++");
    writeProperties2(); 
    System.out.println("++++++++++++++++++++++++++");
    writeProperties3();
}

}
主要練習使用字節流進行本地文件的讀取和寫入

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章