java IO流

将该文件

 

存储到d:\test文件夹下面,使用IO流将该文件拷贝到d盘根目录下。
【步骤】:
1、创建Client.java文件
2、把Client.java文件存储到d:\test文件夹下——此处我拷贝了两份命名为Client1.java和Client2.java
3、把d:\test下面的Client1.java和Client2.java拷贝到d根目录下
【代码】:
【client】源文件代码
package text;
public class Client {
public static void main(String[] args) {
System.out.println("文件存储!");
}
}
【存储文件代码】
package text;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ClientTest1 {

public static void main(String[] args) throws IOException {
testFileReader();
testFileInputStream();
}

private static void testFileReader() throws IOException {
String path1 = "D:" + File.separator + "test" + File.separator;
String path2 = "D:" + File.separator + "test" + File.separator
+ "client1.java";
FileReader reader = new FileReader("D:" + File.separator + "eclipse"
+ File.separator + "workspace" + File.separator + "text"
+ File.separator + "src" + File.separator + "text"
+ File.separator + "Client.java");
File file = new File(path1);
if (!file.exists()) {
file.mkdirs();
}

file = new File(path2);
// 文件会自动创建,下面判断可以省略
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(file);

BufferedReader bReader = new BufferedReader(reader);
BufferedWriter bWriter = new BufferedWriter(writer);

String temp = "";
while ((temp = bReader.readLine()) != null) {
bWriter.write(temp);
}
bWriter.flush();
bWriter.close();
bReader.close();
}

private static void testFileInputStream() throws IOException {
String path1 = "D:" + File.separator + "test" + File.separator;
String path2 = "D:" + File.separator + "test" + File.separator
+ "client2.java";
FileInputStream fis = new FileInputStream("D:" + File.separator
+ "eclipse" + File.separator + "workspace" + File.separator
+ "text" + File.separator + "src" + File.separator + "text"
+ File.separator + "Client.java");
File file = new File(path1);
if (!file.exists()) {
file.mkdirs();
}
file = new File(path2);
// 文件会自动创建,下面判断可以省略
if (!file.exists()) {
file.createNewFile();
}

FileOutputStream fos = new FileOutputStream(file);

BufferedInputStream bInputStream = new BufferedInputStream(fis);
BufferedOutputStream bOutputStream = new BufferedOutputStream(fos);

int temp = 0;
while ((temp = bInputStream.read()) != -1) {
bOutputStream.write(temp);
}
bOutputStream.flush();
bOutputStream.close();
bInputStream.close();
}

}
【存储文件效果图】

 


【拷贝文件代码】
package text;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ClientTest2 {

public static void main(String[] args) throws IOException {
testFileReader();
testFileInputStream();
}

private static void testFileReader() throws IOException {
String path1 = "D:" + File.separator + "Client1.java";
FileReader reader = new FileReader("D:" + File.separator + "test" + File.separator + "Client1.java");

FileWriter writer = new FileWriter(new File(path1));

BufferedReader bReader = new BufferedReader(reader);
BufferedWriter bWriter = new BufferedWriter(writer);

String temp = "";
while ((temp = bReader.readLine()) != null) {
bWriter.write(temp);
}
bWriter.flush();
bWriter.close();
bReader.close();
}

private static void testFileInputStream() throws IOException {
String path1 = "D:" + File.separator + "Client2.java";
FileInputStream fis = new FileInputStream("D:" + File.separator + "test" + File.separator + "Client2.java");

FileOutputStream fos = new FileOutputStream(new File(path1));

BufferedInputStream bInputStream = new BufferedInputStream(fis);
BufferedOutputStream bOutputStream = new BufferedOutputStream(fos);

int temp = 0;
while ((temp = bInputStream.read()) != -1) {
bOutputStream.write(temp);
}
bOutputStream.flush();
bOutputStream.close();
bInputStream.close();
}

}
【拷贝文件效果图】

 

posted @ 2016-09-13 10:26  涤新云  阅读(198)  评论(0编辑  收藏  举报