Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package wang.switchy.hin2n.tool;


import android.text.TextUtils;
import android.util.Log;
import com.chad.library.adapter.base.BaseQuickAdapter;
Expand All @@ -11,26 +10,18 @@ public class IOUtils {
public static String readTxt(String txtPath) {
File file = new File(txtPath);
if (file.isFile() && file.exists()) {
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream);
bufferedReader = new BufferedReader(inputStreamReader);
try (FileInputStream fileInputStream = new FileInputStream(file);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
StringBuilder stringBuilder = new StringBuilder();
String text = null;
String text;
while ((text = bufferedReader.readLine()) != null) {
stringBuilder.append(text);
stringBuilder.append("\n");
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(fileInputStream);
close(inputStreamReader);
close(bufferedReader);
}
}
return "";
Expand All @@ -39,87 +30,54 @@ public static String readTxt(String txtPath) {
public static String readTxtLimit(String txtPath, int size) {
File file = new File(txtPath);
if (file.exists() && file.isFile()) {
RandomAccessFile randomAccessFile = null;
try {
randomAccessFile = new RandomAccessFile(file, "r");
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
long length = randomAccessFile.length();
long start = 0;
if (length > size) {
start = length - size;
}
long start = length > size ? length - size : 0;
randomAccessFile.seek(start);
StringBuilder stringBuilder = new StringBuilder();
String text = null;
String text;
while ((text = randomAccessFile.readLine()) != null) {
stringBuilder.append(text);
stringBuilder.append("\n");
}
return stringBuilder.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
close(randomAccessFile);
}
}
return "";
}

private volatile static RandomAccessFile randomAccessFile = null;
private volatile static boolean isNeedShow = false;
//isNeedShow = true 当第一个线程在运行while时,之后的线程进来只会重置线程共享的RandomAccessFile状态;
//isNeedShow = false 第一个线程结束while,之后的线程不走while。关闭RandomAccessFile
public static void readTxtLimits(boolean showLog, String txtPath, int size, BaseQuickAdapter mAdapter) {
// Log.d("readTxtLimits", Thread.currentThread() + "");
isNeedShow = showLog;
try {
File file = new File(txtPath);
if (file.exists() && file.isFile()) {
if (randomAccessFile == null) {
randomAccessFile = new RandomAccessFile(file, "r");
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
long length = randomAccessFile.length();
long start = 0;
if (length > size) {
start = length - size;
}
long start = length > size ? length - size : 0;
randomAccessFile.seek(start);
String text = null;
String text;
while (isNeedShow) {
// Log.d("readTxtLimits-1", Thread.currentThread() + "");
text = randomAccessFile.readLine();
if (!TextUtils.isEmpty(text)) {
String finalText = text;
ThreadUtils.mainThreadExecutor(new Runnable() {
@Override
public void run() {
if (mAdapter.getData().size() > 200) {
mAdapter.getData().remove(0);
mAdapter.notifyItemRemoved(0);
}
mAdapter.getData().add(finalText);
int last = mAdapter.getData().size() - 1;
mAdapter.notifyItemChanged(last);
mAdapter.getRecyclerView().scrollToPosition(last);
ThreadUtils.mainThreadExecutor(() -> {
if (mAdapter.getData().size() > 200) {
mAdapter.getData().remove(0);
mAdapter.notifyItemRemoved(0);
}
mAdapter.getData().add(finalText);
int last = mAdapter.getData().size() - 1;
mAdapter.notifyItemChanged(last);
mAdapter.getRecyclerView().scrollToPosition(last);
});
}
}
} else if (isNeedShow) {
randomAccessFile = new RandomAccessFile(file, "r");
long length = randomAccessFile.length();
long start = 0;
if (length > size) {
start = length - size;
}
randomAccessFile.seek(start);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (!isNeedShow) {
close(randomAccessFile);
randomAccessFile = null;
}
}
}

Expand Down