站点更新:2017-12-21 17:18:54
parent
ec9f154d68
commit
3fb067f709
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
hello,world!
|
||||
hello,world!
|
Loading…
Reference in New Issue