當前位置:首頁 » 編程語言 » 粘貼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 瀏覽:705
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:968
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:676
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:828
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:737
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1076
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:308
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:188
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:875
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:829