版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
package ReleaseCopyAndStickFunctionDemo;
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.io.Writer;
import java.text.BreakIterator;
import javax.print.DocFlavor.INPUT_STREAM;
import javax.swing.JFileChooser;
import javax.xml.transform.stream.StreamResult;
import org.omg.CORBA_2_3.portable.InputStream;
/**
* 对比使用InputStream BUfferedInputStream BufferedReader的时长
* 实现文件内容的复制和粘贴
* @author 李。。
* @version 1.8
* @date 2019年8月1日 上午9:05:41
* @content JAVA代码
* @motto 代码千万条,可读第一条。代码不规范,error多两行。
*/
public class CopyAndStickDemo{
public static final String filePath="c://test";
public static final String filePath1="c://text/复制成功的文件.txt";
public static void main(String[] args){
System.out.println("函数"+"inOutputStream()"+"的使用时长为");
long time=-1;
try {
time=inOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
System.out.println("耗时"+time+"毫秒");
}
System.out.println("函数"+" BufferedInputOutputStream()"+
"的使用时长为");
try {
time=BufferedInputOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
System.out.println("耗时"+time+"毫秒");
}
System.out.println("函数"+"WriterAndReader()"+
"的使用时长为");
try {
time=WriterAndReader();
} catch (IOException e) {
e.printStackTrace();
}
finally
{
System.out.println("耗时"+time+"毫秒");
}
}
/**
* 使用OutputStream和InputStream进指定为文件内容的复制粘贴的功能
* @throws IOException 抛出输入输出流异常
*/
public static long inOutputStream() throws IOException
{ long startTime=System.currentTimeMillis();
JFileChooser fileChoose=new JFileChooser(filePath);
//显示文件夹的对话框
fileChoose.showOpenDialog(null);
//将选择的文件的对象赋给file对象
File file=fileChoose.getSelectedFile();
fileChoose.showOpenDialog(null);
File file1=fileChoose.getSelectedFile();
//使用InputSteam 和OutputStream 实现文件内容的复制和粘贴
FileInputStream inputStream=new FileInputStream(file);
OutputStream outputStream=new FileOutputStream(file1,false);
byte[]bytes=new byte[20000];//字节数组
int size=inputStream.available();
inputStream.read(bytes);
//对文件的内容进行读取输出
String content=null;
content=new String(bytes);
//System.out.println("文件"+file.getName()+"的内容为:"+content);
inputStream.close();
//进行文件的写入
outputStream.write(bytes);
//对文件进行关闭
outputStream.close();
long endTime=System.currentTimeMillis();
System.out.println("文件夹:"+filePath);
System.out.println("文件"+file.getName()+"的内容"+"复制到"+
"文件"+file1.getName()+"成功!");
return endTime-startTime;
}
public static long BufferedInputOutputStream() throws IOException
{
int i=0;
long startTime=System.currentTimeMillis();
JFileChooser fileChoose=new JFileChooser(filePath);
fileChoose.showOpenDialog(null);
File file=fileChoose.getSelectedFile();
fileChoose.showOpenDialog(null);
File file1=fileChoose.getSelectedFile();
FileInputStream inputStream=new FileInputStream(file);
OutputStream outputStream=new FileOutputStream(file1,false);
BufferedInputStream in=new BufferedInputStream(inputStream);
BufferedOutputStream out=new BufferedOutputStream(outputStream);
//对文件内容进行输出
int size = 0;
try {
size = in.available();
} catch (IOException e1) {
e1.printStackTrace();
}
byte []bytes=new byte[size];
in.read(bytes);
//System.out.println("文件"+file.getName()+
//"的内容为:"+new String(bytes));
//进行文件内容的复制
try {
out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
finally{
System.out.println("文件"+file.getName()+"的内容"+"复制到"+
"文件"+file1.getName()+"成功!");
}
// try {
// while((i=in.read())!=-1)
// {
// out.write(i);;
// }
// } catch (IOException e) {
//
// e.printStackTrace();
// }
// finally{
// System.out.println("文件"+file.getName()+"的内容"+"复制到"+
// "文件"+file1.getName()+"成功!");
// }
in.close();
inputStream.close();
out.close();
outputStream.close();
long endTime=System.currentTimeMillis();
return endTime-startTime;
}
/**
* 字符流的方式进行文件内容的复制粘贴功能
* @return
* @throws IOException
*/
public static long WriterAndReader() throws IOException
{
int i=0;
long startTime=System.currentTimeMillis();
JFileChooser fileChoose=new JFileChooser(filePath);
fileChoose.showOpenDialog(null);
File file=fileChoose.getSelectedFile();
fileChoose.showOpenDialog(null);
File file1=fileChoose.getSelectedFile();
Reader reader=null;
Writer writer=null;
try {
reader=new FileReader(file);
writer=new FileWriter(file1);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BufferedReader bReader=new BufferedReader(reader);
BufferedWriter bWriter=new BufferedWriter(writer);
//对文件的内容进行输出
System.out.println("文件"+file.getName()+"的内容为:");
StringBuffer content=new StringBuffer();
try {
while((i=bReader.read())!=-1)
{
bWriter.write(i);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
bReader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
try {
bWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
bWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
long endTime=System.currentTimeMillis();
return endTime-startTime;
}
}