当前位置:首页 » 编程语言 » 粘贴Java

粘贴Java

发布时间: 2024-07-19 03:55:13

⑴ 在java中如何实现复制,粘贴,剪切

要用到java.awt.datatransfer包中的Clipboard类
import java.awt.*;import java.awt.event.*;
import java.awt.datatransfer.*;
public class Test extends Frame implements ActionListener
{ MenuBar menubar; Menu menu;
MenuItem ,cut,paste;
TextArea text1,text2;
Clipboard clipboard=null;
Test()
{ clipboard=getToolkit().getSystemClipboard();//获取系统剪贴板。
menubar=new MenuBar();
menu=new Menu("Edit"); =new MenuItem("");
cut=new MenuItem ("cut"); paste=new MenuItem ("paste");
text1=new TextArea(20,20); text2=new TextArea(20,20);
.addActionListener(this); cut.addActionListener(this);
paste.addActionListener(this);
setLayout(new FlowLayout());
menubar.add(menu);
menu.add(); 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()==) //拷贝到剪贴板。
{ 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文件复制粘贴

复制粘贴实际上是文件的流读取和写入可以通过如下方法实现:
读写是两个不同的分支,通常都是分开单独使用的。
可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
BufferedReader bre = null;
try {
String file = "D:/test/test.txt";
bre = new BufferedReader(new FileReader(file));//此时获取到的bre就是整个文件的缓存流
while ((str = bre.readLine())!= null) // 判断最后一行不存在,为空结束循环
{
System.out.println(str);//原样输出读到的内容
};
备注: 流用完之后必须close掉,如上面的就应该是:bre.close(),否则bre流会一直存在,直到程序运行结束。
可以通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行存储,举例:
OutputStreamWriter pw = null;//定义一个流
pw = new OutputStreamWriter(new FileOutputStream(“D:/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:569
制作脚本网站 发布:2025-10-20 08:17:34 浏览:857
python中的init方法 发布:2025-10-20 08:17:33 浏览:556
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:738
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:659
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:980
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:231
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:89
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:781
python股票数据获取 发布:2025-10-20 07:39:44 浏览:685