素材
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包
新闻热点
疑难解答