【IntelliJ IDEA 2019.2】java 自写的log打印及读写文件
声明:(其实,不用关心声明的问题,IDEA会自动补上)
import java.io.FileInputStream;
import java.io.FileOutputStream;
清空日志:
static public void ClearDstFile(){
try {
FileOutputStream fos = new FileOutputStream("log_command.txt", false);
String str = "";
fos.write(str.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
追加日志:
static public void AppendStringToFile(String format, Object... args) {
try {
String str = new Formatter().format(format, args).toString();
System.out.print(str);
FileOutputStream fos = new FileOutputStream("log_command.txt", true);
fos.write(str.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
附件一:java 读写文件
读:
try {
// 创建读取文件的字节流
FileInputStream fis = new FileInputStream(path);
System.out.printf("Input file <<%s>> size is %dk\n", path, fis.available()/1024);
byte[] FileBuf = new byte[fis.available()];
fis.read(FileBuf);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
写:
try {
FileOutputStream fos = new FileOutputStream(OutputBinName);
fos.write(RGB16tab);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}