判断文件是否存在的写法:
File file = new File(m_WorkDirectory + "菜单树.json");
if (!file.exists()) m_WorkDirectory = m_WorkDirectory_base;
System.out.println(m_WorkDirectory);
package sample;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileDo {
public static void WriteBin(String OutputBinName, byte[] bin) {
if (bin == null) {
System.out.println("Error: bin file is null!!!");
return;
}
try {
FileOutputStream fos = new FileOutputStream(OutputBinName);
fos.write(bin);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void WriteBin(String OutputBinName, String bin) {
// String line22 = bin;
// if (line22 != null && line22.length() > 0){
// System.out.printf("WriteBin : ");
// for (int m=0; m<8&&m<line22.length(); m++){
// System.out.printf("%02X ", (int)line22.charAt(m) & 0xFF);
// }
// System.out.printf("\r\n");
// }
byte[] buf = new byte[bin.length()];
for (int i = 0; i < bin.length(); i++) {
buf[i] = (byte) bin.charAt(i);
}
WriteBin(OutputBinName, buf);
}
public static byte[] ReadBin(String path) {
byte[] FileBuf = null;
FileInputStream fis = null;
try {
// 创建读取文件的字节流
fis = new FileInputStream(path);
//System.out.printf("Input file <<%s>> size is %dk\n", path, fis.available()/1024);
FileBuf = new byte[fis.available()];
fis.read(FileBuf);
fis.close();
} catch (Exception e) {
e.printStackTrace();
System.out.printf("Error:Input file <<%s>> \n", path);
}
// if (FileBuf.length > 8){
// System.out.printf("ReadBin: ");
// for (int i=0; i<8; i++){
// System.out.printf("%02X ", FileBuf[i]);
// }
// System.out.printf("\r\n");
// }
return FileBuf;
}
public static List<String> ReadTxt(String fileName) {
// 编码格式简介:ASCII码、ANSI、GBK、GB2312、GB18030和Unicode、UTF-8,BOM头
//return ReadTxt(fileName, "GB2312");
return ReadTxt(fileName, "UTF-8");
// 有时候,不知道目标文件的编码是什么,这时候,有什么办法可以确定 Encoding 是多少呢?
// 很简单,用 Notepad++ 打开目标文件,点击“编码”菜单,就可以看到编码的格式啦。
}
public static List<String> Readline(String fileName) {
List<String> ret = new ArrayList<>();
System.out.printf("Readline = %s\r\n", fileName);
byte[] bin = ReadBin(fileName);
if (bin == null) {
return ret;
}
String str = "";
str += (char) bin[0];
System.out.printf("bin size = %d\r\n", bin.length);
for (int i = 1; i < bin.length; i++) {
if (bin[i - 1] == 0x0d && bin[i] == 0x0a) {
str = str.substring(0, str.length() - 1);
ret.add(str);
str = "";
} else {
str += (char) bin[i];
}
}
str = str.substring(0, str.length() - 1);
ret.add(str);
System.out.printf("ret size = %d\r\n", ret.size());
if (ret.size() > 0) {
String line = ret.get(0);
if (line != null && line.length() > 0) {
System.out.printf("ReadLine: ");
for (int i = 0; i < 8 && i < line.length(); i++) {
System.out.printf("%02X ", (int) line.charAt(i) & 0xFF);
}
System.out.printf("\r\n");
}
}
return ret;
}
// 缺点:大于128的字符无法读进来,造成 ISO8859-2 编码下,读不到大于128的字符,因为该编码有大于128的字母。
public static List<String> ReadTxt(String fileName, String Encoding) {
List<String> ret = new ArrayList<>();
File file = new File(fileName);
BufferedReader reader = null;
try {
//System.out.println("以行为单位读取文件内容,一次读一行");
//reader = new BufferedReader(new FileReader(file));
// 这样该后可以读各种编码。
InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), Encoding/*"GB2312"*/);
reader = new BufferedReader(isr);
String tempString = null;
int line = 1;
//一次读一行,读入null时文件结束
while ((tempString = reader.readLine()) != null) {
//把当前行号显示出来
//System.out.println("line " + line + ": " + tempString);
ret.add(tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return ret;
}
public static void WriteTxt(String path, String contain) throws IOException {
Write_txt_with_encode(path, contain, "UTF-8");
}
public static void Write_txt_with_encode(String path, String content, String encoding) throws IOException {// 参考: https://www.cnblogs.com/gojava/p/3555575.html
File file = new File(path);
file.delete();
file.createNewFile();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file), encoding));
writer.write(content);
writer.close();
}
public static String read_txt_with_encode(String path, String encoding) throws IOException { // 参考: https://www.cnblogs.com/gojava/p/3555575.html
String content = "";
File file = new File(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
String line = null;
while ((line = reader.readLine()) != null) {
content += line + "\n";
}
reader.close();
return content;
}
// 参考: https://blog.csdn.net/qq_41939020/article/details/107539300
public static long m_TickOld = -1;
public static boolean Is_Modify(String path_in){
File file = new File(path_in);
long TickCurr = file.lastModified();
boolean ret = TickCurr == m_TickOld ? false : true;
m_TickOld = TickCurr;
return ret;
}
}
读文本的目录应该这样写:
List<String> WaveTxt_FatBurn = FileDo.ReadTxt("out\\production\\Analyze\\Analyze\\Fat.txt");
文件也可以放在源文件目录下就行,编译时,自然会把文件复制到可执行文件目录里。