Java IO 字节流 字符流

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iteye_21104/article/details/82411237
package com.demo.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileInputStream_demo {

public static void main(String[] args) {
// TODO 自动生成的方法存根
int b;
FileInputStream inStream = null;
FileOutputStream outStream = null;
FileReader inReader = null;
FileWriter outWriter = null;
try {

// 复制文件
outStream = new FileOutputStream(
"E:\\Resource code\\javabase\\kang bye bak.txt");

inStream = new FileInputStream(
"E:\\Resource code\\javabase\\kang bye.txt");
while ((b = inStream.read()) != -1) {
System.out.print((char) b);
// 向文件写内容
outStream.write(b);
}

// 复制文件
outWriter = new FileWriter(
"E:\\Resource code\\javabase\\再别康桥 复件.txt");

inReader = new FileReader("E:\\Resource code\\javabase\\再别康桥.txt");
while ((b = inReader.read()) != -1) {
System.out.print((char) b);
// 向文件写内容
outWriter.write(b);
}

inStream.close();
inReader.close();
outStream.close();
outWriter.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}

package com.demo;

import java.io.FileWriter;
import java.io.IOException;

public class ConfigureFileWriter {

/**
* @param args
* @throws IOException
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("conf.properties");
fileWriter.write("dbDriver=oracle.jdbc.driver.OracleDriver");
System.out.println("success");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

package com.demo;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ConfigureFileReader {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

FileReader fileReader = null;
try {
fileReader = new FileReader("conf.properties");
char[] chars = new char[64];
int hasRead = 0;
while ((hasRead = fileReader.read(chars)) > 0) {
System.out.println(new String(chars, 0, hasRead));
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

package com.demo;

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

public class ReadProperties {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Properties props = new Properties();
File f = new File("conf.properties");
try {
props.load(new FileInputStream(f));
System.out.println(props.getProperty("dbDriver"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
展开阅读全文

没有更多推荐了,返回首页