File相关的读取和写入以及复制

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;

public class FileIO {
//字节读取流
public static void inFile() {
//定义在最外面
File file;
FileInputStream fis;
try {
//1.创建文件对象
file=new File("c:\\abc.txt");
//2.创建读取字节流
fis=new FileInputStream(file);
//3.读取流操作
//3.1逐个字节读取:一个中文占2个字节,所以中文部分显示乱码
for (int i = 0; i < file.length(); i++) {
char c=(char)fis.read();//返回值是字节
System.out.print(c);
}
//3.2多个字节读取
byte[] bt=new byte[(int)file.length()];
fis.read(bt); //将读取的字节存入字节数组bt中
System.out.print(new String(bt));
//4.关闭字节流
fis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
//字节写入流
public static void outFile(String str) {
File file;
FileOutputStream fos;
try {
//1.获取文件对象
file=new File("c:\\abc.txt");
//2.常见写入字节流
//fos=new FileOutputStream(file);//将数据写到file这个目标文件中
fos=new FileOutputStream(file, true);//当true为false是,覆盖写入,默认false
//3.写入操作
str.getBytes();
fos.write(str.getBytes());
//4.关闭字节流,一定要加上关闭,否则写入不成功
fos.close();
} catch (Exception e) {
// TODO: handle exception
}
}
//字符读取流
public static void readerFile() {
File file;
FileReader fr;
try {
//创建文件对象
file=new File("c:\\abc.txt");
//创建读取字符流
fr=new FileReader(file);
//创建读取字符流缓冲对象
BufferedReader br =new BufferedReader(fr);
String str=br.readLine();
while(str!=null){//换行可以读取出来
System.out.println(str);
str=br.readLine();
}
//关闭流:后用的先关闭,先用的后关闭
br.close();
fr.close();
} catch (Exception e) {
// TODO: handle exception
}
}
//字符写入流
public static void writerFile(String str) {
File file;
FileWriter fw;
try {
//创建文件对象
file=new File("c:\\abc.txt");
//创建写入字符流
//fw=new FileWriter(file);
fw=new FileWriter(file, true);//true追加,默认false覆盖重写
//创建写入字符流缓冲对象
BufferedWriter bw=new BufferedWriter(fw);
//写入操作
bw.write(str);//写入一个字符串
bw.append('我');//加入一个字符
//关闭流:后用的先关闭,先用的后关闭
bw.close();
fw.close();
} catch (Exception e) {
// TODO: handle exception
}
}

//读取二进制流
public static void binaryIn() {
File file;
FileInputStream fis;
try {
file=new File("c:\\abc.txt");
fis=new FileInputStream(file);
DataInputStream dis=new DataInputStream(fis);
byte[] bt=new byte[1024];
int i=dis.read(bt);
while(i!=-1){
System.out.println(i);
i=dis.read(bt);
}
} catch (Exception e) {
// TODO: handle exception
}
}
//写入二进制流
public static void binaryOut(){
File file;
FileOutputStream fos;
try {
file=new File("c:\\abc.txt");
fos=new FileOutputStream(file);
DataOutputStream dos=new DataOutputStream(fos);
byte[] bt=new byte[1024];
dos.write(bt);
} catch (Exception e) {
// TODO: handle exception
}
}

}

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileCopy {
private File inputFile;
private File outputFile;
private InputStream inputStream;
private OutputStream outputStream;
public FileCopy(String inputPath,String outputPath) throws FileNotFoundException{
inputFile=new File(inputPath);
outputFile=new File(outputPath);
inputStream=new FileInputStream(inputFile);
outputStream=new FileOutputStream(outputFile);

}
//一次性把数据全部读取到内存中来,再一次性写入
public void copy1() throws IOException{
byte b[]=new byte[(int)inputFile.length()];
inputStream.read(b); //一次性读入
outputStream.write(b); //一次性写入
// inputStream.close();
// outputStream.close();
}
//边读边写
public void copy2() throws IOException{
int temp=0;
while((temp=inputStream.read())!=-1){
outputStream.write(temp);
}
inputStream.close();
outputStream.close();
}

public File getInputFile() {
return inputFile;
}
public void setInputFile(File inputFile) {
this.inputFile = inputFile;
}
public File getOutputFile() {
return outputFile;
}
public void setOutputFile(File outputFile) {
this.outputFile = outputFile;
}
public static void main(String[] args) throws IOException{
String inputPath="e:"+File.separator+"Xfire.rar";
String outputPath="f:"+File.separator+inputPath.substring(inputPath.indexOf(File.separator));
FileCopy fileCopy=new FileCopy(inputPath, outputPath);
long start1=System.currentTimeMillis();
fileCopy.copy1();
long end1=System.currentTimeMillis();
System.out.println("一次性全部读入内存复制文件大小为"+fileCopy.getInputFile().length()+"位花费时间为:"+(end1-start1)+"ms");


fileCopy.getOutputFile().delete();
long start2=System.currentTimeMillis();
fileCopy.copy2();
long end2=System.currentTimeMillis();
System.out.println("边读边写复制文件大小为"+fileCopy.getInputFile().length()+"位花费时间为:"+(end2-start2)+"ms");
}

}

上一篇:Linux基础精华


下一篇:机器学习实战:基于Scikit-Learn和TensorFlow(一)