File读写复制
Posted Recently 祝祝
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了File读写复制相关的知识,希望对你有一定的参考价值。
1:读:从外部把数据读到内存中,input,write
package com.uplooking.io;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class BufferWriterTest {
private String path;
private String name;
public BufferWriterTest(String path, String name) {
super();
this.path = path;
this.name = name;
}
private void mkdir() {
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
}
public void bufferwrite() {
mkdir();
File file = new File(path + "/" + name);
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter = new FileWriter(file);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("hello shimmer");
bufferedWriter.flush();
System.err.println("数据读入成功,文件位置位于:" + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null)
bufferedWriter.close();
if (fileWriter != null)
fileWriter.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
BufferWriterTest bfWriter= new BufferWriterTest("H:\\\\0812", "code3.txt");
bfWriter.bufferwrite();
}
}
2:写:把数据从内存写到外部,output,reader
package com.uplooking.io;
import java.io.File;
import java.io.FileReader;
public class ReaderTest {
private String path;
private String name;
public ReaderTest(String path, String name) {
super();
this.path = path;
this.name = name;
}
public void reader() {
File file = new File(path + "/" + name);
FileReader fr = null;
try {
fr = new FileReader(file);
char[] chs = new char[10];
int len = fr.read(chs); // 把数据向chs数组中填充,
while (len != -1) {
System.out.println("本次读到的有效个数是:" + len);
String string = new String(chs, 0, len); // 构造成字符串
System.out.println("本次读到的内容:" + string);
len = fr.read(chs);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ReaderTest r1 = new ReaderTest("H:\\\\0812", "code1.txt");
r1.reader();
}
}
3:边写边读
package com.uplooking.io;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
public class ReaderWriter2 {
private String path;
private String name;
public ReaderWriter2(String path, String name) {
super();
this.path = path;
this.name = name;
}
public void read() {
File file = new File( path +"/"+name );
FileReader fReader =null;
FileWriter fWriter = null;
File Outfile = new File(path + "/"+"new"+name);
try {
fWriter =new FileWriter(Outfile);
fReader = new FileReader(file);
char[] chs =new char[10];
int len =fReader.read(chs); // 把数据向数组中填充,填了多少个由len记录
while ( len != -1 ) {
System.err.println("本次读到的有效个数 = " + len );
String s = new String(chs,0,len);
System.out.println("本次读到的数据:"+s);
fWriter.write(chs , 0 , len );
len = fReader.read(chs);
}
fWriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fReader) {
fReader.close();
}
if (null != fWriter) {
fWriter.close();
}
} catch ( Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
ReaderWriter2 rdw = new ReaderWriter2("H://0812", "code1.txt");
rdw.read();
}
}
4:复制文件
package com.uplooking.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class CopyFileTest {
private String path;
private String name;
public CopyFileTest(String path, String name) {
super();
this.path = path;
this.name = name;
}
public void copy() {
File inFile = new File(path + "/" + name);
File outFile = new File(path + "/new" + name);
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
fileInputStream = new FileInputStream(inFile);
fileOutputStream = new FileOutputStream(outFile);
byte[] bs = new byte[1024];
int len = fileInputStream.read(bs);
while (-1 != len) {
fileOutputStream.write(bs, 0, len);
len = fileInputStream.read(bs);
}
fileOutputStream.flush();
System.out.println(outFile + "复制完成");
} catch (Exception e) {
e.printStackTrace();
System.err.println(outFile + "复制失败");
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
public static void main(String[] args) {
CopyFileTest copyFileTest = new CopyFileTest("H://0812", "code3.txt");
copyFileTest.copy();
}
}
end》》》
有效沟通,合理沟通,一直以来都是一门优雅艺术。
语言的魅力,学习的活动力,语言是提升自己的一种方式。
以上是关于File读写复制的主要内容,如果未能解决你的问题,请参考以下文章