工具类,该类实现了对文件进行了读写key value,实际改写,而不是内存改写。效率上待优化因为加了synchronize
package sample;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class Utils {
/**
* 2022年8月30日09:55:16
* 使用Properties读取配置文件,当前读取key为空的时候,自动添加key=0
* @param key 要读取的key
* @return 读取key对应的value值
* @throws IOException
*/
private static String propertiesRead(String key) throws IOException {
Properties sysConfig = new Properties();
InputStream inputStream = Utils.class.getResourceAsStream("value.properties");
sysConfig.load(inputStream);
if(sysConfig.get(key)==null){
write(key,"0");
return "0";
}
return sysConfig.get(key).toString();
}
/**
* 2022年8月30日09:55:57
* @param key 要写入的key
* @param value 要写入的value
* @throws IOException
*/
private static void propertiesWrite(String key, String value) throws IOException {
Properties sysConfig = new Properties();
InputStream inputStream = Utils.class.getResourceAsStream("value.properties");
sysConfig.load(inputStream);
sysConfig.setProperty(key, value);
}
/**
* 向指定文件中写入key和value,相当于set,在有key的时候会写入一对默认的key和value r1=r1用户做覆盖用,这里以后再做优化
* @param key 被写入的key值
* @param value 被写入的value值
* @throws IOException
*/
public synchronized static void write(String key, String value) throws IOException {
String read = fileRead();
if ("".equals(read.trim())) {
System.out.println("为空直接写入");
fileWrite(key, value, false);
} else {
String[] readArray = read.split("\n");
System.out.println("有几行数据:" + readArray.length);
//先看是否key在里面
boolean isexist = false;
for (int i = 0; i < readArray.length - 1; i++) {
String[] keyvalueArray = readArray[i].split("=");
if (key.equals(keyvalueArray[0])) {
isexist = true;
break;
}
}
if (!isexist) {
fileWrite(key, value, true);
} else {
//处理存在值的问题
Map<String, String> existMap = new HashMap<>();
//减1的目的是取出最后一行是空行的问题
for (int i = 0; i < readArray.length - 1; i++) {
String[] keyvalueArray = readArray[i].split("=");
if (key.equals(keyvalueArray[0])) {
existMap.put(key, value);
} else {
existMap.put(keyvalueArray[0], keyvalueArray[1]);
}
}
//再次以覆盖的形式写入
fileWrite("r1", "r1", false);
for (Map.Entry entry : existMap.entrySet()) {
if(!"r1".equals((String) entry.getKey())){
fileWrite((String) entry.getKey(), (String) entry.getValue(), true);
}
}
}
}
}
/**
* 2022年8月30日12:08:35
* 从文件内容中读取指定的key的value值
* @return 文件中的字符串
* @throws IOException
*/
public static String readKey(String key) throws IOException {
String read = fileRead();
String value="";
if ("".equals(read.trim())) {
value="";
} else {
String[] readArray = read.split("\n");
boolean isexist = false;
for (int i = 0; i < readArray.length - 1; i++) {
String[] keyvalueArray = readArray[i].split("=");
if (key.equals(keyvalueArray[0])) {
value=keyvalueArray[1];
break;
}
}
}
return value;
}
/**
* 2022年8月30日09:12:07
* 从文件读取中读取字符串
* @return 文件中的字符串
* @throws IOException
*/
private static String fileRead() throws IOException {
// 使用nio从本地读取一个文件
//File file = new File();
FileInputStream fileInputStream = new FileInputStream("src/sample/value.properties");
// 获取文件相应的channel,channel中会有相关数据
FileChannel channel = fileInputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(256);
// 将channel的数据读取到buffer
channel.read(buffer);
//System.out.println(buffer);
// System.out.println("读取的数据为"+new String(buffer.array()));
fileInputStream.close();
return new String(buffer.array());
}
/**
* 2022年8月30日09:53:39
* 向文件中写入指定的key和value,是否以覆盖的模式或是追加的模式
* @param key 要写入的key
* @param value 要写入value
* @param doappend 是覆盖写,还是覆盖
* @throws IOException
*/
private static void fileWrite(String key, String value, boolean doappend) throws IOException {
// 将数据写入到文件中
//创建一个输出流
File file = new File("src/sample/value.properties");
FileOutputStream fileOutputStream = new FileOutputStream(file, doappend);
// 获取相应的channel
FileChannel channel = fileOutputStream.getChannel();
// 将字符串放入缓存区
ByteBuffer byteBuffer = ByteBuffer.allocate(256);
//将字符串以字节形式放入buffer中
byteBuffer.put((key + "=" + value + "\n").getBytes());
//开始读取
byteBuffer.flip();
//从buffer中读取到文件
channel.write(byteBuffer);
fileOutputStream.close();
}
}
rannable
package sample;
import org.omg.CORBA.TRANSACTION_MODE;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
public class testRunnable implements Runnable {
boolean mark=false;
String markstr;
public testRunnable(String markstr) {
this.markstr = markstr;
}
@Override
public void run() {
SimpleDateFormat sdf = new SimpleDateFormat("yymmdd");
int count = 0;
System.out.println("Thread start:" + sdf.format(new Date()));
for (int i = 0; i < 1000; i++) {
String flagg = null;
try {
flagg = Utils.readKey(markstr);
System.out.println("flagg:"+flagg);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("mark:"+mark);
if (mark) {
try {
System.out.println(markstr+"写1了");
Utils.write(markstr, "1");
mark = false;
} catch (IOException e) {
e.printStackTrace();
}
}
if(flagg == null || "".equals(flagg)){
}else{
if(Integer.valueOf(flagg) == 1){
System.out.println(markstr+"跳出");
break;
}
}
count++;
try {
Thread.sleep(1000);
System.out.println(count + "");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Thread end :" + sdf.format(new Date()));
}
}
调用
package sample;
import java.io.IOException;
class propertyT{
static volatile boolean flag = true;
public static void main(String[] args) throws InterruptedException, IOException {
// Utils.write("r3","22");
// Utils.write("r5","55");
testRunnable tr = new testRunnable("tr3");
new Thread(tr).start();
testRunnable tr1 = new testRunnable("tr3");
new Thread(tr1).start();
testRunnable tr2 = new testRunnable("tr3");
new Thread(tr2).start();
testRunnable tr3 = new testRunnable("tr5");
new Thread(tr3).start();
testRunnable tr4 = new testRunnable("tr5");
new Thread(tr4).start();
Thread.sleep(5000);
tr.mark = true;
}
}