From 3fb067f7094c3e26c4dbfe76e679a6a4a5c23a79 Mon Sep 17 00:00:00 2001 From: ehlxr Date: Thu, 21 Dec 2017 17:18:54 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AB=99=E7=82=B9=E6=9B=B4=E6=96=B0=EF=BC=9A20?= =?UTF-8?q?17-12-21=2017:18:54?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/me/ehlxr/ContentReplace2.java | 48 +++++++++ src/main/java/me/ehlxr/DumpStack.java | 68 +++++++++++++ src/main/java/me/ehlxr/Modify.java | 103 ++++++++++++++++++++ src/main/java/me/ehlxr/test.txt | 2 + 4 files changed, 221 insertions(+) create mode 100644 src/main/java/me/ehlxr/ContentReplace2.java create mode 100644 src/main/java/me/ehlxr/DumpStack.java create mode 100644 src/main/java/me/ehlxr/Modify.java create mode 100644 src/main/java/me/ehlxr/test.txt diff --git a/src/main/java/me/ehlxr/ContentReplace2.java b/src/main/java/me/ehlxr/ContentReplace2.java new file mode 100644 index 0000000..24c06d3 --- /dev/null +++ b/src/main/java/me/ehlxr/ContentReplace2.java @@ -0,0 +1,48 @@ +package me.ehlxr; + +import java.io.*; + +@SuppressWarnings("ALL") +public class ContentReplace2 { + public static void main(String[] args) { + File file = new File("/Users/ehlxr/ehlxr/blog/hugoBlog/content/post"); + File[] files = file.listFiles(); + + for (File f : files) { + operationFile(f); + } + } + + private static void operationFile(File file) { + File tmpfile = new File(file.getParentFile().getAbsolutePath() + "\\" + file.getName() + ".tmp"); + try ( + BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile))) { + + boolean flag = false; + String line; + while ((line = reader.readLine()) != null) { + + if (line.startsWith("title")) { + System.out.println(line); + // StringBuilder sb = new StringBuilder(); + // line = sb.append("title: \"").append(line.substring(line.indexOf("title") + 7)).append("\"").toString(); + line = line.replaceAll("'", ""); + System.out.println(line); + + flag = true; + } + writer.write(line + "\n"); + } + + if (flag) { + file.delete(); + tmpfile.renameTo(new File(file.getAbsolutePath())); + } else { + tmpfile.delete(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/me/ehlxr/DumpStack.java b/src/main/java/me/ehlxr/DumpStack.java new file mode 100644 index 0000000..9e99b99 --- /dev/null +++ b/src/main/java/me/ehlxr/DumpStack.java @@ -0,0 +1,68 @@ +package me.ehlxr; + +import java.io.*; +import java.lang.management.*; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Code which writes a stack dump for all threads to a file. + */ +public class DumpStack { + + // directory where the stack files are written + private static final String STACK_DUMP_DIR = "/var/tmp"; + + // here for testing + public static void main(String[] args) throws Exception { + dumpStacks(); + } + + private static void dumpStacks() throws IOException { + ThreadMXBean mxBean = ManagementFactory.getThreadMXBean(); + MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean(); + RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean(); + + + ThreadInfo[] threadInfos = mxBean.getThreadInfo(mxBean.getAllThreadIds(), 0); + Map threadInfoMap = new HashMap<>(); + for (ThreadInfo threadInfo : threadInfos) { + threadInfoMap.put(threadInfo.getThreadId(), threadInfo); + } + + // choose our dump-file + File dumpFile = new File(STACK_DUMP_DIR, "stacks." + System.currentTimeMillis()); + try (Writer writer = new BufferedWriter(new FileWriter(dumpFile))) { + dumpTraces(mxBean, threadInfoMap, writer); + } + } + + private static void dumpTraces(ThreadMXBean mxBean, Map threadInfoMap, Writer writer) + throws IOException { + Map stacks = Thread.getAllStackTraces(); + writer.write("Dump of " + stacks.size() + " thread at " + + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date(System.currentTimeMillis())) + "\n\n"); + for (Map.Entry entry : stacks.entrySet()) { + Thread thread = entry.getKey(); + writer.write("\"" + thread.getName() + "\" prio=" + thread.getPriority() + " tid=" + thread.getId() + " " + + thread.getState() + " " + (thread.isDaemon() ? "deamon" : "worker") + "\n"); + ThreadInfo threadInfo = threadInfoMap.get(thread.getId()); + if (threadInfo != null) { + writer.write(" native=" + threadInfo.isInNative() + ", suspended=" + threadInfo.isSuspended() + + ", block=" + threadInfo.getBlockedCount() + ", wait=" + threadInfo.getWaitedCount() + "\n"); + writer.write(" lock=" + threadInfo.getLockName() + " owned by " + threadInfo.getLockOwnerName() + + " (" + threadInfo.getLockOwnerId() + "), cpu=" + + (mxBean.getThreadCpuTime(threadInfo.getThreadId()) / 1000000L) + ", user=" + + (mxBean.getThreadUserTime(threadInfo.getThreadId()) / 1000000L) + "\n"); + } + for (StackTraceElement element : entry.getValue()) { + writer.write(" "); + writer.write(element.toString()); + writer.write("\n"); + } + writer.write("\n"); + } + } +} \ No newline at end of file diff --git a/src/main/java/me/ehlxr/Modify.java b/src/main/java/me/ehlxr/Modify.java new file mode 100644 index 0000000..4cf477a --- /dev/null +++ b/src/main/java/me/ehlxr/Modify.java @@ -0,0 +1,103 @@ +package me.ehlxr; + +import java.io.*; + +/* + * 替换文件(如果该文件含有子目录,则包括子目录所有文件)中某个字符串并写入新内容(Java代码实现). + * + *原理:逐行读取源文件的内容,一边读取一边同时写一个*.tmp的文件。 + *当读取的行中发现有需要被替换和改写的目标内容‘行’时候,用新的内容‘行’替换之。 + *最终,删掉源文件,把*.tmp的文件重命名为源文件名字。 + * + *注意!代码功能是逐行读取一个字符串,然后检测该字符串‘行’中是否含有替换的内容,有则用新的字符串‘行’替换源文件中该处整个字符串‘行’。没有则继续读。 + *注意!替换是基于‘行’,逐行逐行的替换! + * + * */ +public class Modify { + + private final String target; + private final String newContent; + private String path; + + public Modify(String path, String target, String newContent) { + // 操作目录。从该目录开始。该文件目录下及其所有子目录的文件都将被替换。 + this.path = path; + // target:需要被替换、改写的内容。 + this.target = target; + // newContent:需要新写入的内容。 + this.newContent = newContent; + + operation(); + } + + public static void main(String[] args) { + //代码测试:假设有一个test文件夹,test文件夹下含有若干文件或者若干子目录,子目录下可能也含有若干文件或者若干子目录(意味着可以递归操作)。 + //把test目录下以及所有子目录下(如果有)中文件含有"hi"的字符串行替换成新的"hello,world!"字符串行。 + new Modify("/Users/ehlxr/WorkSpaces/java-utils/src/main/java/me/ehlxr/test.txt", "hi", "hello,world!"); + } + + private void operation() { + File file = new File(path); + opeationDirectory(file); + } + + private void opeationDirectory(File file) { + if (file.isFile()) { + operationFile(file); + } + if (file.isDirectory()) { + File[] files = file.listFiles(); + if (files != null) { + for (File f : files) { + opeationDirectory(f); + } + } + } + + } + + private void operationFile(File file) { + + try { + InputStream is = new FileInputStream(file); + BufferedReader reader = new BufferedReader( + new InputStreamReader(is)); + + String filename = file.getName(); + // tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。 + File tmpfile = new File(file.getParentFile().getAbsolutePath() + "\\" + filename + ".tmp"); + + BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile)); + + boolean flag = false; + String str; + while (true) { + str = reader.readLine(); + + if (str == null) + break; + + if (str.contains(target)) { + writer.write(newContent + "\n"); + + flag = true; + } else + writer.write(str + "\n"); + } + + is.close(); + + writer.flush(); + writer.close(); + + if (flag) { + file.delete(); + tmpfile.renameTo(new File(file.getAbsolutePath())); + } else { + tmpfile.delete(); + } + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/me/ehlxr/test.txt b/src/main/java/me/ehlxr/test.txt new file mode 100644 index 0000000..becb76a --- /dev/null +++ b/src/main/java/me/ehlxr/test.txt @@ -0,0 +1,2 @@ +hello,world! +hello,world!