站点更新:2017-12-21 17:18:54

dev
ehlxr 2017-12-21 17:18:54 +08:00
parent ec9f154d68
commit 3fb067f709
4 changed files with 221 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -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<Long, ThreadInfo> 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<Long, ThreadInfo> threadInfoMap, Writer writer)
throws IOException {
Map<Thread, StackTraceElement[]> 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<Thread, StackTraceElement[]> 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");
}
}
}

View File

@ -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();
}
}
}

View File

@ -0,0 +1,2 @@
hello,world!
hello,world!