導航:首頁 > 編程語言 > java文件剪切

java文件剪切

發布時間:2022-11-25 22:55:36

java中文件移動問題,file.move

沒看懂你的代碼,java API是支持文件剪切的,如下
//移動e:/abc.txt 到e:/abc/abc.txt
java.io.File file = new java.io.File("e:/abc.txt");
file.renameTo(new java.io.File("e:/abc/abc.txt"));

⑵ java 問題

下面是我以前寫的,發給你了,可以直接拿來用!
覺得可以,給我加點分吧,我太需要了!
剪貼其實就是復制,再刪除!!呵呵

/**
* @author CaoShun
* @see 根據傳遞過來路徑名,建立文件夾
* */
public static boolean createFolderbyName(String path)
{
boolean createOK=true;
File dirFile=null;
dirFile = new File(path);
if( dirFile.exists() == true ){
createOK=false;
}
else{
boolean bFile = dirFile.mkdir();
if( bFile == true ){
createOK=false;
}
else{
createOK=true;
}
}
return createOK;
}

/**
* @author CaoShun
* @see 復制文件夾包括文件夾里內容
* */
public static void Folder(String oldPath, String newPath) {//復制文件夾
try {
(new File(newPath)).mkdirs(); //如果文件夾不存在 則建立新文件夾
File a=new File(oldPath);
String[] file=a.list();
File temp=null;
for (int i = 0; i < file.length; i++) {
if(oldPath.endsWith(File.separator)){
temp=new File(oldPath+file[i]);
}
else{
temp=new File(oldPath+File.separator+file[i]);
}
if(temp.isFile()){
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(newPath + "/" +(temp.getName()).toString());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b))!= -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if(temp.isDirectory()){//如果是子文件夾
Folder(oldPath+"/"+file[i],newPath+"/"+file[i]);
}
}
}
catch (Exception e) {
System.out.println("復制整個文件夾內容操作出錯");
e.printStackTrace();
}
}

/**
* @author CaoShun
* @see 刪除文件夾及裡面子文件
* */
public static void delAllFile(String path) {//刪除所有文件
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
}
else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path+"/"+ tempList[i]);//先刪除文件夾裡面的文件
delFolder(path+"/"+ tempList[i]);//再刪除空文件夾
}
}
}
public static void delFolder(String folderPath) {//刪除目錄
try {
delAllFile(folderPath); //刪除完裡面所有內容
String filePath = folderPath;
filePath = filePath.toString();
java.io.File myFilePath = new java.io.File(filePath);
myFilePath.delete(); //刪除空文件夾
}
catch (Exception e) {
//System.out.println("刪除文件夾操作出錯");
e.printStackTrace();
}
}

⑶ 在配置Java環境變數時,原來系統有Path,應該怎麼設置

JAVA程序能夠順利編譯需要兩個環境變數,一個是PATH(path),一個是CLASSPATH(classpath)。那麼這兩個環境變數代表什麼呢?或者說究竟在JAVA程序編譯中有什麼作用呢?這就要涉及到WINDOWS的基本知識了。

在WINDOWS環境中,每一條能用的指令都保存在硬碟的「某個角落」,比如說指令NOTEPAD(WINDOWS的記事本指令,在C: WINNT目錄下),那麼在DOS命令提示符下直接輸入NOTEPAD就可以打開記事本了。但是一旦我們把C:WINNT目錄下的

NOTEPAD.EXE 這個可執行文件移開(比如說我們現在把它「剪切」到D盤根目錄下)我們再在DOS提示符下直接輸入NOTEPAD時就無法再打開記事本了,但是我們在DOS下改變路徑輸入 D:NOTEPAD(或者先輸入cd D: 在輸入NOTEPAD也行)就可以打開記事本了。

這就是說在我們直接輸入NOTEPAD的時候,其實其默認路徑就是C:WINN。假設我們要運行一個JAVA程序(hello.java)就要先把它的class文件移動到JAVA里的bin目錄下然後打開DOS,把路徑改到JAVA的BIN目錄下執行java hello.class,一次無所謂但是每次都要這樣去做未免太麻煩。

還好WINDOWS提供了一個PATH環境變數,他的作用就是現在這個變數的值路徑中去找,如果找到了要運行的EXE就運行,否則失敗。如果我們把JAVA里的bin目錄這個路徑賦給path那麼下次我在運行java這個命令的時候,即便是直接輸入DOS也能夠在path變數中找到JAVA的bin目錄下的java指令了。

⑷ java將一個文件復制到剪切板

首先要明白 「剪切板」 裡面能放什麼內容。 剪切板只能放 「文本內容」或者「文件路徑」, 如果你選中文字,然後復制,那麼放在剪切板就是文字內容;如果你選擇一個文件,復制,那放在剪切板裡面是 文件的路徑。

你說的 文件復制到剪切板 是不合理的。

Java 通過 java.awt.datatransfer.Clipboard 訪問剪切板。

⑸ Java中你常用到哪些快捷鍵

Eclipse的編輯功能非常強大,掌握了Eclipse快捷鍵功能,能夠大大提高開發效率。Eclipse中有如下一些和編輯相關的快捷鍵。
1. 【ALT+/】
此快捷鍵為用戶編輯的好幫手,能為用戶提供內容的輔助,不要為記不全方法和屬性名稱犯愁,當記不全類、方法和屬性的名字時,多體驗一下【ALT+/】快捷鍵帶來的好處吧。
2. 【Ctrl+O】
顯示類中方法和屬性的大綱,能快速定位類的方法和屬性,在查找Bug時非常有用。
3. 【Ctrl+/】
快速添加註釋,能為游標所在行或所選定行快速添加註釋或取消注釋,在調試的時候可能總會需要注釋一些東西或取消注釋,現在好了,不需要每行進行重復的注釋。
4. 【Ctrl+D】
刪除當前行,這也是筆者的最愛之一,不用為刪除一行而按那麼多次的刪除鍵。
5. 【Ctrl+M】
窗口最大化和還原,用戶在窗口中進行操作時,總會覺得當前窗口小(尤其在編寫代碼時),現在好了,試試【Ctrl+M】快捷鍵。
查看和定位快捷鍵
在程序中,迅速定位代碼的位置,快速找到Bug的所在,是非常不容易的事,Eclipse提供了強大的查找功能,可以利用如下的快捷鍵幫助完成查找定位的工作。
1. 【Ctrl+K】、【Ctrl+Shift+K】
快速向下和向上查找選定的內容,從此不再需要用滑鼠單擊查找對話框了。
2. 【Ctrl+Shift+T】
查找工作空間(Workspace)構建路徑中的可找到Java類文件,不要為找不到類而痛苦,而且可以使用「*」、「?」等通配符。
3. 【Ctrl+Shift+R】
和【Ctrl+Shift+T】對應,查找工作空間(Workspace)中的所有文件(包括Java文件),也可以使用通配符。
4. 【Ctrl+Shift+G】
查找類、方法和屬性的引用。這是一個非常實用的快捷鍵,例如要修改引用某個方法的代碼,可以通過【Ctrl+Shift+G】快捷鍵迅速定位所有引用此方法的位置。
5. 【Ctrl+Shift+O】
快速生成import,當從網上拷貝一段程序後,不知道如何import進所調用的類,試試【Ctrl+Shift+O】快捷鍵,一定會有驚喜。
6. 【Ctrl+Shift+F】
格式化代碼,書寫格式規范的代碼是每一個程序員的必修之課,當看見某段代碼極不順眼時,選定後按【Ctrl+Shift+F】快捷鍵可以格式化這段代碼,如果不選定代碼則默認格式化當前文件(Java文件)。
7. 【ALT+Shift+W】
查找當前文件所在項目中的路徑,可以快速定位瀏覽器視圖的位置,如果想查找某個文件所在的包時,此快捷鍵非常有用(特別在比較大的項目中)。
8. 【Ctrl+L】
定位到當前編輯器的某一行,對非Java文件也有效。
9. 【Alt+←】、【Alt+→】
後退歷史記錄和前進歷史記錄,在跟蹤代碼時非常有用,用戶可能查找了幾個有關聯的地方,但可能記不清楚了,可以通過這兩個快捷鍵定位查找的順序。
10. 【F3】
快速定位游標位置的某個類、方法和屬性。
11. 【F4】
顯示類的繼承關系,並打開類繼承視圖。
調試快捷鍵
Eclipse中有如下一些和運行調試相關的快捷鍵。
1. 【Ctrl+Shift+B】:在當前行設置斷點或取消設置的斷點。
2. 【F11】:調試最後一次執行的程序。
3. 【Ctrl+F11】:運行最後一次執行的程序。
4. 【F5】:跟蹤到方法中,當程序執行到某方法時,可以按【F5】鍵跟蹤到方法中。
5. 【F6】:單步執行程序。
6. 【F7】:執行完方法,返回到調用此方法的後一條語句。
7. 【F8】:繼續執行,到下一個斷點或程序結束。
常用編輯器快捷鍵
通常文本編輯器都提供了一些和編輯相關的快捷鍵,在Eclipse中也可以通過這些快捷鍵進行文本編輯。
1. 【Ctrl+C】:復制。
2. 【Ctrl+X】:剪切。
3. 【Ctrl+V】:粘貼。
4. 【Ctrl+S】:保存文件。
5. 【Ctrl+Z】:撤銷。
6. 【Ctrl+Y】:重復。
7. 【Ctrl+F】:查找。
8. 【Ctrl+H】:選中你需要查找字元的文件。
其他快捷鍵
Eclipse中還有很多快捷鍵,無法一一列舉,用戶可以通過幫助文檔找到它們的使用方式,另外還有幾個常用的快捷鍵如下。
1. 【Ctrl+F6】:切換到下一個編輯器。
2. 【Ctrl+Shift+F6】:切換到上一個編輯器。
3. 【Ctrl+F7】:切換到下一個視圖。
4. 【Ctrl+Shift+F7】:切換到上一個視圖。
5. 【Ctrl+F8】:切換到下一個透視圖。
6. 【Ctrl+Shift+F8】:切換到上一個透視圖。

⑹ java 切割wav音頻

1.首先應該得到音樂文件的時長---這個可以網路,我也不會,但應該可以實現

2.然後得到文件大小,除以文件總分鍾數
3.根據得到的大小切割文件(用二進制的方式讀取文件)

⑺ java的系統剪切板如何獲取一個文本的內容

需要導入包:import java.awt.*;
import java.awt.datatransfer.*; 將內容寫入剪貼板:String temp1=text1.getSelectedText();
StringSelection ss = new StringSelection(temp1);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); 讀取剪貼板:
Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);String readText = (String)t.getTransferData(DataFlavor.stringFlavor);

⑻ 用java編寫記事本程序,可以實現新建、打開、保存、退出、復制、粘貼、剪切、全選。

import javax.swing.*;
import javax.swing.filechooser.FileFilter;

import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Hashtable;

public class MyText extends JFrame implements ActionListener{
private JLabel lb1;
private JMenuBar mb;
private JMenu 文件, 編輯, 格式, 幫助;
private JMenuItem 新建, 打開, 保存, 退出, 復制, 粘貼, 剪切, 全選, 字體, 關於;
private JTextArea editorArea;
private boolean isDirty = false;
private String strFileName = "未命名";
private static final String EDITOR_NAME = "MyText";

public MyText() {
super();
mb = new JMenuBar();
文件 = new JMenu("文件");
編輯 = new JMenu("編輯");
格式 = new JMenu("格式");
幫助 = new JMenu("幫助");
新建 = new JMenuItem("新建");
新建.addActionListener(this);
打開 = new JMenuItem("打開");
打開.addActionListener(this);
保存 = new JMenuItem("保存");
保存.addActionListener(this);
退出 = new JMenuItem("退出");
退出.addActionListener(this);
復制 = new JMenuItem("復制");
復制.addActionListener(this);
粘貼 = new JMenuItem("粘貼");
粘貼.addActionListener(this);
剪切 = new JMenuItem("剪切");
剪切.addActionListener(this);
全選 = new JMenuItem("全選");
全選.addActionListener(this);
字體 = new JMenuItem("字體");
字體.addActionListener(this);
關於 = new JMenuItem("關於");
關於.addActionListener(this);
mb.add(文件);
mb.add(編輯);
mb.add(格式);
mb.add(幫助);
文件.add(新建);
文件.add(打開);
文件.add(保存);
文件.add(退出);
編輯.add(復制);
編輯.add(粘貼);
編輯.add(剪切);
編輯.add(全選);
格式.add(字體);
幫助.add(關於);
setJMenuBar(mb);

Container container = getContentPane();
editorArea = new JTextArea();
editorArea.setLineWrap(true);
editorArea.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(!isDirty()){
setDirty(true);
}
}
});
JScrollPane scrollPane = new JScrollPane(editorArea);
container.add(scrollPane);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
dispose();
}
});
setTitle(formatEditorTitle());
setSize(600, 400);
setVisible(true);
}

private boolean isDirty() {
return isDirty;
}

private void setDirty(boolean isDirty) {
this.isDirty = isDirty;
setTitle(formatEditorTitle());
}

public static void main(String args[]) {
@SuppressWarnings("unused")
MyText app = new MyText();
}

public void actionPerformed(ActionEvent e) {
JMenuItem item = (JMenuItem)e.getSource();
if(item.equals(新建)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件內容已經變動,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
clearEditorArea();
setDirty(false);
}else if(item.equals(打開)){
if(isDirty()){
int ret = JOptionPane.showConfirmDialog(getContentPane(), "文件內容已經變動,是否保存?", "MyText", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
if(ret == JOptionPane.OK_OPTION){
saveFile();
}else if(ret == JOptionPane.CANCEL_OPTION || ret == JOptionPane.CLOSED_OPTION){
return;
}
}
openFile();
}else if(item.equals(保存)){
saveFile();
}else if(item.equals(退出)){
dispose();
}else if(item.equals(復制)){
editorArea.();
}else if(item.equals(剪切)){
editorArea.cut();
}else if(item.equals(粘貼)){
editorArea.paste();
}else if(item.equals(全選)){
editorArea.selectAll();
}else if(item.equals(字體)){
FontDialog font = new FontDialog(this, editorArea.getFont());
editorArea.setFont(font.getSelectedFont());
}else if(item.equals(關於)){
AboutDialog about = new AboutDialog(this);
about.setVisible(true);
}
}

private String getFileName() {
return strFileName;
}

private void setFileName(String strFileName) {
this.strFileName = strFileName;
}

public String formatEditorTitle(){
StringBuffer strFileNm = new StringBuffer(getFileName());
strFileNm.append(isDirty()?"*":"");
strFileNm.append(" - ");
strFileNm.append(EDITOR_NAME);
return strFileNm.toString();
}

private void clearEditorArea(){
editorArea.selectAll();
editorArea.replaceSelection("");
}

private void openFile(){
JFileChooser openDialog = new JFileChooser();
openDialog.setFileFilter(new TxtFileFilter());
if(openDialog.showOpenDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = openDialog.getSelectedFile();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String buff = br.readLine();
clearEditorArea();
while(buff != null){
editorArea.append(buff);
editorArea.append("\n");
buff = br.readLine();
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(br != null)
br.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}

private void saveFile(){
JFileChooser saveDialog = new JFileChooser();
saveDialog.setFileFilter(new TxtFileFilter());
if(saveDialog.showSaveDialog(getContentPane()) == JFileChooser.APPROVE_OPTION){
File file = saveDialog.getSelectedFile();
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
String buff = editorArea.getText();
bw.write(buff);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally{
try{
if(bw != null)
bw.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
}
}
}

class TxtFileFilter extends FileFilter{

@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt");
}

@Override
public String getDescription() {
return "*.txt(文本文件)";
}

}

class FontDialog extends JDialog{
private JComboBox cb_FontSize;
private JComboBox cb_FontStyle;
private JComboBox cb_FontNm;
private Font font;
Hashtable<Integer, String> style = new Hashtable<Integer, String>();

public FontDialog(){
this(null, null);
}

public FontDialog(Frame owner, Font font){
super(owner);
this.font = font == null?getFont():font;
setTitle("字體選擇框");
setModal(true);
setResizable(false);
setSize(326, 164);
getContentPane().setLayout(null);

final JLabel lb_FontNm = new JLabel();
lb_FontNm.setText("字體名稱");
lb_FontNm.setBounds(10, 10, 66, 16);
getContentPane().add(lb_FontNm);

cb_FontNm = new JComboBox();
cb_FontNm.setBounds(10, 28, 133, 25);
getContentPane().add(cb_FontNm);

cb_FontStyle = new JComboBox();
cb_FontStyle.setBounds(169, 28, 66, 25);
getContentPane().add(cb_FontStyle);

cb_FontSize = new JComboBox();
cb_FontSize.setBounds(258, 28, 53, 25);
getContentPane().add(cb_FontSize);

final JButton btn_OK = new JButton();
btn_OK.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
int styleCode = 0;
for(Enumeration<Integer> i = style.keys();i.hasMoreElements();){
styleCode = i.nextElement();
if(style.get(styleCode).equals(cb_FontStyle.getSelectedItem()))
break;
}
Font font = new Font(cb_FontNm.getSelectedItem().toString(), styleCode, ((Integer)cb_FontSize.getSelectedItem()).intValue());
setSelectedFont(font);
dispose();
}
});
btn_OK.setText("確定");
btn_OK.setBounds(58, 83, 76, 26);
getContentPane().add(btn_OK);

final JButton btn_Cancel = new JButton();
btn_Cancel.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});
btn_Cancel.setText("取消");
btn_Cancel.setBounds(169, 83, 76, 26);
getContentPane().add(btn_Cancel);

final JLabel lb_FontStyle = new JLabel();
lb_FontStyle.setText("字體樣式");
lb_FontStyle.setBounds(169, 10, 66, 16);
getContentPane().add(lb_FontStyle);

final JLabel lb_FontSize = new JLabel();
lb_FontSize.setText("字體大小");
lb_FontSize.setBounds(258, 10, 66, 16);
getContentPane().add(lb_FontSize);

init();
setVisible(true);
}

private void init(){
GraphicsEnvironment gg=GraphicsEnvironment.getLocalGraphicsEnvironment();
String ss[]=gg.getAvailableFontFamilyNames();
for(String s : ss)
cb_FontNm.addItem(s);
if(font != null)
cb_FontNm.setSelectedItem(font.getFamily());

style.put(Font.PLAIN, "標准");
style.put(Font.BOLD, "粗體");
style.put(Font.ITALIC, "斜體");
style.put(Font.BOLD+Font.ITALIC, "粗體&斜體");

cb_FontStyle.addItem(style.get(Font.PLAIN));
cb_FontStyle.addItem(style.get(Font.BOLD));
cb_FontStyle.addItem(style.get(Font.ITALIC));
cb_FontStyle.addItem(style.get(Font.BOLD+Font.ITALIC));

if(font != null)
cb_FontStyle.setSelectedItem(style.get(font.getStyle()));

for(int i=8;i<23;i++)
cb_FontSize.addItem(i);
if(font != null)
cb_FontSize.setSelectedItem(font.getSize());

}

public Font getSelectedFont() {
return font;
}

public void setSelectedFont(Font font) {
this.font = font;
}
}

class AboutDialog extends JDialog{
public AboutDialog(JFrame owner){
super(owner);
setTitle("關於");
setSize(new Dimension(322, 163));
getContentPane().setLayout(null);

final JLabel version = new JLabel();
version.setText("MyText 1.0");
version.setBounds(74, 37, 66, 16);
getContentPane().add(version);

final JLabel right = new JLabel();
right.setText("Copyright (C) 2010");
right.setBounds(74, 59, 188, 16);
getContentPane().add(right);

final JSeparator separator = new JSeparator();
separator.setBounds(70, 90, 210, 2);
getContentPane().add(separator);

final JButton okButton = new JButton();
okButton.setBounds(235, 95, 50, 26);
getContentPane().add(okButton);
okButton.setText("Ok");
okButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
dispose();
}
});
}
}
}

⑼ Java 里用什麼代碼可以把一個 String 的內容復制到 Windows 剪切板

java 中的剪切板
通過操作系統的剪切板,可以實現在不同的程序中拷貝和粘貼數據。一些用Java寫的程序例如很多開發環境也可以訪問到剪切板,本文就是研究如何在Java程序中讀寫系統剪切板的數據。

Java中使用java.awt.datatransfer.Clipboard類來描述剪切板,並把剪切板分為兩種類型:本地和系統,本地剪切板使用 Clipborad cp = new Clipboard("clip1"); 來構造;系統剪切板通過

Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();

獲取,下面我們給出幾個常用的方法用於讀寫剪切板中的文本數據以及圖像數據

1. 從指定的剪切板中獲取文本內容

protected static String getClipboardText(Clipboard clip) throws Exception{
// 獲取剪切板中的內容
Transferable clipT = clip.getContents(null);
if (clipT != null) {
// 檢查內容是否是文本類型
if (clipT.isDataFlavorSupported(DataFlavor.stringFlavor))
return (String)clipT.getTransferData(DataFlavor.stringFlavor);
}
return null;
}

2. 往剪切板寫文本數據

protected static void setClipboardText(Clipboard clip, String writeMe) {
Transferable tText = new StringSelection(writeMe);
clip.setContents(tText, null);
}

3. 從剪切板讀取圖像

public static Image getImageFromClipboard() throws Exception{
Clipboard sysc = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable cc = sysc.getContents(null);
if (cc == null)
return null;
else if(cc.isDataFlavorSupported(DataFlavor.imageFlavor))
return (Image)cc.getTransferData(DataFlavor.imageFlavor);
return null;
}

4. 寫圖像到剪切板

protected static void setClipboardImage2(final Image image) {
Transferable trans = new Transferable(){
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[] { DataFlavor.imageFlavor };
}
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.imageFlavor.equals(flavor);
}
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
if(isDataFlavorSupported(flavor))
return image;
throw new UnsupportedFlavorException(flavor);
}

};
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(trans, null);
}

有了這四個方法,你下面可以自己寫一些程序來進行測試,利用它來實現與其他程序結合測試對剪切板數據的操作。這里給出一個用於顯示圖像的類,只要把Image實例作為參數傳入即可。

/*
* Created on 2004-12-23
* 查看圖形的窗口
*/
package javayou.clipboard;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* @author Winter Lau 用於顯示圖形的窗口
*/
public class ImageViewer extends Frame {

private Image image;

/**
* 顯示一個圖像
* @param viewMe
*/
public ImageViewer(Image viewMe) {
image = viewMe;
MediaTracker mediaTracker = new MediaTracker(this);
mediaTracker.addImage(image, 0);
try {
mediaTracker.waitForID(0);
} catch (InterruptedException ie) {
ie.printStackTrace();
System.exit(1);
}
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
//窗口適應圖像大小
setSize(image.getWidth(null), image.getHeight(null));
//窗口標題
setTitle("Viewing Image from Clipboard");
setVisible(true);
}

public void paint(Graphics graphics) {
graphics.drawImage(image, 0, 0, null);
}

/**
* 用於讀取圖像文件並生成Image對象
*/
public static Image getImageFromFile(String fileName) {
Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage(fileName);
return image;
}

}

閱讀全文

與java文件剪切相關的資料

熱點內容
打開加密軟體的方法 瀏覽:156
雲存儲伺服器可靠嗎 瀏覽:967
2核1g的雲伺服器能帶動游戲嘛 瀏覽:898
逆命20解壓碼 瀏覽:146
徐州辦犬證需要下載什麼app 瀏覽:1002
百保盾是什麼樣的app 瀏覽:699
文件和文件夾的命名規格 瀏覽:798
java命令行運行java 瀏覽:664
搜索pdf內容 瀏覽:499
程序員裝機必備的軟體 瀏覽:12
php微信第三方登錄demo 瀏覽:538
上海php工具開發源碼交付 瀏覽:793
哪裡有求購黃頁的源碼 瀏覽:194
商城礦機源碼礦場系統 瀏覽:198
單片機的led燈熄滅程序 瀏覽:224
洛陽python培訓 瀏覽:704
小鍵盤命令 瀏覽:194
單片機c語言返回主程序 瀏覽:816
dockerpythonweb 瀏覽:972
程序員演算法有多強 瀏覽:718