当前位置:首页>开发>正文

java如何拷贝文件到另一个目录下 使用Java语言如何实现快速文件复制

2023-05-27 22:08:35 互联网 未知 开发

 java如何拷贝文件到另一个目录下 使用Java语言如何实现快速文件复制

java如何拷贝文件到另一个目录下

下面列举出4种方式:
1、使用FileStreams复制
这是最经典的方式将一个文件的内容复制到另一个文件中。 使用FileInputStream读取文件A的字节,使用FileOutputStream写入到文件B。正如你所看到的我们执行几个读和写操作try的数据,所以这应该是一个低效率的,下一个方法我们将看到新的方式。 这是第一个方法的代码: 

2、使用FileChannel复制
Java NIO包括transferFrom方法,根据文档应该比文件流复制的速度更快。 这是第二种方法的代码: 

3、使用Commons IO复制
Apache Commons IO提供拷贝文件方法在其FileUtils类,可用于复制一个文件到另一个地方。它非常方便使用Apache Commons FileUtils类时,您已经使用您的项目。基本上,这个类使用Java NIO FileChannel内部。 这是第三种方法的代码: 

4、使用Java7的Files类复制
如果你有一些经验在Java 7中你可能会知道,可以使用复制方法的Files类文件,从一个文件复制到另一个文件。 这是第四个方法的代码: 

使用Java语言如何实现快速文件复制

使用Java语言如何实现快速文件复制:
代码:
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.nio.channels.FileChannel
public class Test {
public static void main(String[] args){
long start = System.currentTimeMillis()
FileInputStream fileInputStream = null
FileOutputStream fileOutputStream = null
FileChannel inFileChannel = null
FileChannel outFileChannel = null
try {
fileInputStream = new FileInputStream(new File("C:\from\不是闹着玩的.flv"))
fileOutputStream = new FileOutputStream(new File("C:\to\不是闹着玩的.flv"))
inFileChannel = fileInputStream.getChannel()
outFileChannel = fileOutputStream.getChannel()
inFileChannel.transferTo(0, inFileChannel.size(), outFileChannel)//连接两个通道,从in通道读取数据写入out通道。
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
if(fileInputStream != null){
fileInputStream.close()
}
if(inFileChannel != null){
inFileChannel.close()
}
if(fileOutputStream != null){
fileOutputStream.close()
}
if(outFileChannel != null){
outFileChannel.close()
}
} catch (IOException e) {
e.printStackTrace()
}
}
long end = System.currentTimeMillis()
System.out.println("视频文件从“from”文件夹复制到“to”文件需要" (end - start) "毫秒。")
}
}

Java如何快速复制大文件

在Java编程中,复制文件的方法有很多,而且经常要用到。我以前一直是缓冲输入输出流来实现的(绝大多数人都是如此),近来在研究JDK文档时发现,用文件通道(FileChannel)来实现文件复制竟然比用老方法快了近三分之一。下面我就来介绍一下如何用文件通道来实现文件复制,以及在效率上的对比

用文件通道的方式来进行文件复制
/**
* 使用文件通道的方式复制文件
*
* @param s
* 源文件
* @param t
* 复制到的新文件
*/
public void fileChannelCopy(File s, File t) {
FileInputStream fi = null
FileOutputStream fo = null
FileChannel in = null
FileChannel out = null
try {
fi = new FileInputStream(s)
fo = new FileOutputStream(t)
in = fi.getChannel()//得到对应的文件通道
out = fo.getChannel()//得到对应的文件通道
in.transferTo(0, in.size(), out)//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
fi.close()
in.close()
fo.close()
out.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}

java怎么复制一个文件到另一个文件夹


主要是用到java里面的i/o流。代码例子如下:
import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.FileWriter
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader

/**
* java读写文件,复制文件
* 读取d:/1.txt文件内容,写入f:/text.txt文件中.
* @author young
*
*/
public class FileWriterTest {
// 读写文件
public static void rwFile(){
FileWriter fw = null
BufferedReader br = null
try {
fw = new FileWriter("f:\text.txt", true)
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\1.txt"), "UTF-8"))
String line = null
while ((line = br.readLine()) != null) {
System.out.println("文件内容: " line)
fw.write(line)
fw.flush()
}
br.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (fw != null) {
try {
fw.close()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
}
}

public static void main(String[] args) {
rwFile()
}
}
首先在D盘新建文件1.txt,输入任意内容。然后执行java代码即可。

java编程中怎么复制,粘贴

import java.awt.*import java.awt.event.*
import java.awt.datatransfer.*
public class Test extends Frame implements ActionListener
{ MenuBar menubar Menu menu
MenuItem copy,cut,paste
TextArea text1,text2
Clipboard clipboard=null
Test()
{ clipboard=getToolkit().getSystemClipboard()//获取系统剪贴板。
menubar=new MenuBar()
menu=new Menu("Edit") copy=new MenuItem("copy")
cut=new MenuItem ("cut") paste=new MenuItem ("paste")
text1=new TextArea(20,20) text2=new TextArea(20,20)
copy.addActionListener(this) cut.addActionListener(this)
paste.addActionListener(this)
setLayout(new FlowLayout())
menubar.add(menu)
menu.add(copy) menu.add(cut) menu.add(paste)
setMenuBar(menubar)
add(text1)add(text2)
setBounds(100,100,200,250) setVisible(true)pack()
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0)
}
})
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==copy) //拷贝到剪贴板。
{ String temp=text1.getSelectedText() //拖动鼠标选取文本。
StringSelection text=new StringSelection(temp)
clipboard.setContents(text,null)
}
else if(e.getSource()==cut) //剪贴到剪贴板。
{ String temp=text1.getSelectedText() //拖动鼠标选取文本。
StringSelection text=new StringSelection(temp)
clipboard.setContents(text,null)
int start=text1.getSelectionStart()
int end =text1.getSelectionEnd()
text1.replaceRange("",start,end) //从Text1中删除被选取的文本。
}
else if(e.getSource()==paste) //从剪贴板粘贴数据。
{ Transferable contents=clipboard.getContents(this)
DataFlavor flavor= DataFlavor.stringFlavor
if( contents.isDataFlavorSupported(flavor))
try{ String str
str=(String)contents.getTransferData(flavor)
text2.append(str)
}
catch(Exception ee){}
}
}
public static void main(String args[])
{ Test win=new Test()
}
}

java如何实现文件的复制粘贴?

package cn.yxbsz.InAndOutStream

import java.io.DataOutputStream
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStream

public class InAndOutStream {

/**
* @param args
*/
public void InAndOut()
{

try {
OutputStream outstream = null
String s = "D:" "\" "test.txt"
File file = new File(s)
System.out.println(s)
outstream = new FileOutputStream(file,true)
DataOutputStream dataoutstream = new DataOutputStream(outstream)
DataOutputStream dataoutstream1 = new DataOutputStream(System.out)
try {
dataoutstream.write("你好啊: ".getBytes())
dataoutstream.write("你也好! ".getBytes())
dataoutstream.write("今天天气不错。 ".getBytes())
dataoutstream.write("吃饭了吗? ".getBytes())
dataoutstream.flush()
dataoutstream1.write("你好啊: ".getBytes())
dataoutstream1.write("你也好! ".getBytes())
dataoutstream1.write("今天天气不错。 ".getBytes())
dataoutstream1.write("吃饭了吗? ".getBytes())
dataoutstream1.flush()
outstream.flush()
System.out.flush()
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new InAndOutStream().InAndOut()
}

}
Java,输入输出流使用示例,看不懂在追加提问好了。

最新文章