import java.io.*;
public class TestFileInputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
try {
in = new FileInputStream("d:\\share\\java\\io\\TestFileInputStream.java");
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
System.exit(-1);
}
try {
long num = 0;
while((b=in.read())!=-1){ // 读取 文件的内容。就是 取出 这些代码。。哈哈
System.out.print((char)b);
num++;
}
in.close();
System.out.println();
System.out.println("共读取了 "+num+" 个字节");
} catch (IOException e1) {
System.out.println("文件读取错误"); System.exit(-1);
}
}
}
import java.io.*;
public class TestFileOutputStream {
public static void main(String[] args) {
int b = 0;
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("d:/share/java/HelloWorld.java"); // 对准 文件,准备取出 文件的内容
out = new FileOutputStream("d:/share/java/io/HW.java"); // 对准 另一文件 准备像文件中 写数据 若没有 这个文件 FIleOutputStream会自动 建立一个空的。
while((b=in.read())!=-1){
out.write(b);
}
in.close();
out.close();
} catch (FileNotFoundException e2) {
System.out.println("找不到指定文件"); System.exit(-1);
} catch (IOException e1) {
System.out.println("文件复制错误"); System.exit(-1);
}
System.out.println("文件已复制");
}
}
import java.io.*;
public class TestBufferStream1 {
public static void main(String[] args) {
try {
FileInputStream fis =
new FileInputStream("d:\\share\\java\\HelloWorld.java"); // 找到这个文件 准备读取数据内容
BufferedInputStream bis =
new BufferedInputStream(fis);
int c = 0;
System.out.println(bis.read());
System.out.println(bis.read());
bis.mark(100); // 把标记 标到 100,直接 从100上 读取。
for(int i=0;i<=10 && (c=bis.read())!=-1;i++){
System.out.print((char)c+" ");
}
System.out.println();
bis.reset(); // 再回到 100那个标记上。
for(int i=0;i<=10 && (c=bis.read())!=-1;i++){
System.out.print((char)c+" ");
}
bis.close();
} catch (IOException e) {e.printStackTrace();}
}
}
import java.io.*;
public class TestBufferStream2 {
public static void main(String[] args) {
try {
// new FileWriter("d:\\share\\java\\dat2.txt") // 准备 往里些数据
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\share\\java\\dat2.txt"));
BufferedReader br = new BufferedReader(
new FileReader("d:\\share\\java\\dat2.txt")); // 把写进去的数据 读出来。
String s = null;
for(int i=1;i<=100;i++){
s = String.valueOf(Math.random());
bw.write(s); // 写到 文件中。
bw.newLine();
}
bw.flush();
while((s=br.readLine())!=null){
System.out.println(s);
}
bw.close();
br.close();
} catch (IOException e) { e.printStackTrace();}
}
}
import java.io.*;
public class TestFileWriter {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("d:\\bak\\unicode.dat"); // 写到这个文件中。
for(int c=0;c<=50000;c++){
fw.write(c);
}
fw.close();
} catch (IOException e1) {
e1.printStackTrace();
System.out.println("文件写入错误");
System.exit(-1);
}
}
}
import java.io.*;
public class TestFileWriter2 {
public static void main(String[] args) throws Exception {
FileReader fr = new FileReader("d:/java/io/TestFileWriter2.java");
FileWriter fw = new FileWriter("d:/java/io/TestFileWriter2.bak");
int b;
while((b = fr.read()) != -1) {
fw.write(b);
}
fr.close();
fw.close();
}
}
import java.io.*;
public class TestFileReader {
public static void main(String[] args) {
FileReader fr = null;
int c = 0;
try {
fr = new FileReader("d:\\share\\java\\io\\TestFileReader.java"); // 从这个文件中 读取 数据。按字符
int ln = 0;
while ((c = fr.read()) != -1) {
//char ch = (char) fr.read();
System.out.print((char)c);
//if (++ln >= 100) { System.out.println(); ln = 0;}
}
fr.close();
} catch (FileNotFoundException e) {
System.out.println("找不到指定文件");
} catch (IOException e) {
System.out.println("文件读取错误");
}
}
}
import java.io.*;
public class TestDataStream {
public static void main(String[] args) {
ByteArrayOutputStream baos =
new ByteArrayOutputStream(); // 准备往里写数据
DataOutputStream dos =
new DataOutputStream(baos); // 再套一层 管道。
try {
dos.writeDouble(Math.random());
dos.writeBoolean(true);
ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
System.out.println(bais.available());
DataInputStream dis = new DataInputStream(bais);
System.out.println(dis.readDouble());
System.out.println(dis.readBoolean());
dos.close(); dis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1,写数据:
/**
* 写数据
*
* @param path
* 原文件绝对路径
* @param dataList
* 写入TXT的数据集合
* @throws IOException
*/
public void writeTXT(String path, List<String> dataList) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(path, true)));
for (String string : dataList) {
bw.write(string);
bw.newLine();
}
bw.flush();
bw.close();
bw = null;
}
2,复制文件
/**
* 复制文件
*
* @param fromPath
* 源路径
* @param toPath
* 目的路径
*/
public void copyFile(String fromPath, String toPath) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
System.out.println("copyFile start---源路径:" + fromPath + ",目的路径:"
+ toPath);
try {
bis = new BufferedInputStream(new FileInputStream(fromPath));
bos = new BufferedOutputStream(new FileOutputStream(toPath));
byte b[] = new byte[1024 * 8];
int i;
while ((i = bis.read(b)) > -1) {
bos.write(b, 0, i);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
bis = null;
bos = null;
}
}
}
3,读取的properties路径
/**
*
* @param propertiesPath
* 需求读取的properties路径
* @param propertiesKey
* properties里的key
* @return properties里的value
* @throws IOException
*/
public String getPropertiesValue(String propertiesPath,
String propertiesKey) throws IOException {
String path = PropertiesUtil.loadProperties(propertiesPath)
.getProperty(propertiesKey);
// 数据采集导入备份文件夹创建
File file;
file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
file = null;
// 数据采集导入文件夹创建
file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
file = null;
return PropertiesUtil.loadProperties(propertiesPath).getProperty(
propertiesKey);
}