素材
str[0] = "我跟你什么仇什么怨!!!!"; str[1] = "別拉我,我項鏈2000多!!!!"; str[2] = "騙了我,一百塊錢都不給我,還在笑!!!!!!!!"; str[3] = "歡迎嘴強王者"; str[4] = "我只想安靜地做一個美男子"; str[5] = "我今天又沒吃藥,感覺自己萌萌噠!!!!!!!!"; str[6] = "贊我的每人一包辣條,就是這么任性!!!!!!!!"; str[7] = "買買買,按斤買,不買不是人"; str[8] = "叔叔帶你裝逼帶你飛!!!!!!!!"; str[9] = "那畫面太美我不敢看!!!!!!!!";View Code
全部代碼
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.PRintWriter;public class OpenNotePad { private Thread thread; private String[] str; public OpenNotePad() { thread = new Thread(new InterThread()); str = new String[10]; str[0] = "我跟你什么仇什么怨!!!!"; str[1] = "別拉我,我項鏈2000多!!!!"; str[2] = "騙了我,一百塊錢都不給我,還在笑!!!!!!!!"; str[3] = "歡迎嘴強王者"; str[4] = "我只想安靜地做一個美男子"; str[5] = "我今天又沒吃藥,感覺自己萌萌噠!!!!!!!!"; str[6] = "贊我的每人一包辣條,就是這么任性!!!!!!!!"; str[7] = "買買買,按斤買,不買不是人"; str[8] = "叔叔帶你裝逼帶你飛!!!!!!!!"; str[9] = "那畫面太美我不敢看!!!!!!!!"; } public static void main(String[] args) { OpenNotePad object = new OpenNotePad(); object.startThread(); } class InterThread implements Runnable{ @Override public void run() { openLocalNotepad(800,1700); } } private void startThread(){ thread.start(); } private void closedThread(Thread thread){ if(thread.isAlive() && thread != null){ try { thread.join(); } catch (InterruptedException e) { e.printStackTrace(); } } } //設置打開次數 private int setTimes(int times){ return times; } //創建txt文件 private void createTxtFile(String name){ String filePath = "c://"+name+".txt"; File file = new File(filePath); if(!file.exists()){ try { file.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } private void deleteFile(String name){ String filePath = "c://"+name+".txt"; File file = new File(filePath); if(file.exists()){ file.delete(); } } public boolean writeTxtFile (String newStr,String name) throws IOException { // 先讀取原有文件內容,然后進行寫入操作 boolean flag = false; String filein = newStr + "/r/n/r/n"; String temp = ""; FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; FileOutputStream fos = null; PrintWriter pw = null; try { // 文件路徑 File file = new File("c://"+name+".txt"); // 將文件讀入輸入流 fis = new FileInputStream(file); isr = new InputStreamReader(fis); br = new BufferedReader(isr); StringBuffer buf = new StringBuffer(); // 保存該文件原有的內容 for (int j = 1; (temp = br.readLine()) != null; j++) { buf = buf.append(temp); // System.getProperty("line.separator") // 行與行之間的分隔符 相當于“/n” buf = buf.append(System.getProperty("line.separator")); } buf.append(filein); fos = new FileOutputStream(file); pw = new PrintWriter(fos); pw.write(buf.toString().toCharArray()); pw.flush(); flag = true; } catch (IOException e1) { // TODO throw e1; } finally { if (pw != null) { pw.close(); } if (fos != null) { fos.close(); } if (br != null) { br.close(); } if (isr != null) { isr.close(); } if (fis != null) { fis.close(); } } return flag; } //參數可以加上時間延遲 private void openLocalNotepad(long openDelay,long destroyDelay){ int openTimes = 0; Process process = null; while(openTimes < 10){ try { Thread.sleep(openDelay); createTxtFile(str[openTimes]); writeTxtFile (str[openTimes],str[openTimes]); process = Runtime.getRuntime().exec("notepad " +"c://"+str[openTimes]+".txt"); Thread.sleep(destroyDelay); process.destroy(); deleteFile(str[openTimes]); } catch (Exception e) { e.printStackTrace(); } openTimes++; } closedThread(thread); }}View Code
涉及內部類,線程,文件等內容。
讓記事本君也來買買萌,每隔2秒左右自動打開。
如下
具體可以下載下邊的jar包,直接執行,當然需要Java運行環境。也可以編譯上邊的源碼。
源碼純屬雕蟲小技,也有很多不規范,一笑而過。
打包的Jar文件地址:點我下載Jar包
新聞熱點
疑難解答