java程序默认是单线程,Java单线程,频繁操作文件有关问题

Java code//线程调用的方法

public static boolean checkLogFileInfos(String path, String fileName) {

boolean bool = false;

// 判断fileName在Path中是否存在

isExistTxtFile(path, fileName);

File file = new File(path + "\\" + fileName);

// 如果打印信息的日志文件的大小大于1024KB

if ((file.length() / 1024) >=12)) {

// 首先建立临时文件夹

System.out.println("创建临时文件夹" + path + "\\" + "TEMPFILES");

File tempFile = new File(path + "\\" + "TEMPFILES");

tempFile.mkdir();

// 将文件转移到临时文件夹中

System.out.println("将文件" + file.getName() + "转移到"

+ tempFile.getName() + "中");

copyFiletoPath(path, path + "\\"

+ properties.getProperty("LOG_TEMP_FILE_NAME"), fileName,

"");

// 将临时文件夹中的文件进行重命名,将临时文件夹中的文件转移到上一级目录中。

File tempFileTxt = new File(path + "\\"

+ properties.getProperty("LOG_TEMP_FILE_NAME") + "\\"

+ fileName);

String newNameStr = newName(fileName);

File newTempFileTxt = new File(path + "\\" + newNameStr);

System.out.println("开始重命名" + tempFileTxt.getPath());

bool = tempFileTxt.renameTo(newTempFileTxt);

if (bool) {

System.out.println(tempFileTxt.getName() + "重命名成功" + bool);

} else {

System.out.println(tempFileTxt.getName() + "重命名失败" + bool);

}

// 建立新的文件

boolean bool1 = file.delete();

System.out.println("删除" + file.getPath() + "的结果是" + bool1);

System.out.println("开始创建新的文件" + file.getPath());

try {

file.createNewFile();

} catch (IOException e) {

System.out.println("创建" + file.getName() + "失败" + bool);

e.printStackTrace();

}

// 删除临时文件夹

System.out.println("删除" + tempFile.getPath());

boolean bool2 = tempFile.delete();

System.out.println("删除" + tempFile.getPath() + "结果是" + bool2);

boolean bool3 = tempFileTxt.delete();

System.out.println("删除" + tempFileTxt.getPath() + "结果是" + bool3);

}

return bool;

}

//相关的方法:

private static String newName(String fileName) {

Date date = new Date();

SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");

String dateStr = format.format(date);

return dateStr + "" + fileName;

}

private static void isExistTxtFile(String path, String fileName) {

boolean bool = false;

File file = new File(path);

File[] files = file.listFiles();

if (files.length > 0) {

for (int i = 0; i < files.length; i++) {

if (files[i].getName().equals(fileName)) {

bool = true;

}

}

if (!bool) {

File newFile = new File(path + "\\" + fileName);

try {

boolean isOkNewFile = newFile.createNewFile();

if (isOkNewFile) {

System.out.println("文件创建成功");

} else {

System.out.println("文件创建失败");

}

} catch (Exception e) {

e.printStackTrace();

}

}

} else {

bool = false;

File newFile = new File(path + "\\" + fileName);

try {

boolean isOkNewFile = newFile.createNewFile();

if (isOkNewFile) {

System.out.println("文件创建成功");

} else {

System.out.println("文件创建失败");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

public static String returnStardardTime() {

String stardardTime = "";

Date date = new Date();

SimpleDateFormat simpleDateFormat = new SimpleDateFormat(properties

.getProperty("TIME_TYPE"));

stardardTime = simpleDateFormat.format(date);

return stardardTime;

}

public static void copyFiletoPath(String fromPath, String toPath,

String fileName, String newFileName) {

int bytesum = 0;

int byteread = 0;

if (fileName.equals("") || fileName != null) {

File oldFile = new File(fromPath + "/" + fileName);

File newFile = null;

if (newFileName.equals("") || newFileName == null) {

newFile = new File(toPath + "/" + fileName);

} else {

newFile = new File(toPath + "/" + newFileName);

}

try {

if (oldFile.exists()) {

FileInputStream fileInputStream = new FileInputStream(

oldFile);

FileOutputStream fileOutputStream = new FileOutputStream(

newFile);

byte[] buffer = new byte[1024];

while ((byteread = fileInputStream.read(buffer)) != -1) {

bytesum += byteread;

fileOutputStream.write(buffer, 0, byteread);

}

fileInputStream.close();

fileOutputStream.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

private static void isExistTxtFile(String path, String fileName) {

boolean bool = false;

File file = new File(path);

File[] files = file.listFiles();

if (files.length > 0) {

for (int i = 0; i < files.length; i++) {

if (files[i].getName().equals(fileName)) {

bool = true;

}

}

if (!bool) {

File newFile = new File(path + "\\" + fileName);

try {

boolean isOkNewFile = newFile.createNewFile();

if (isOkNewFile) {

System.out.println("文件创建成功");

} else {

System.out.println("文件创建失败");

}

} catch (Exception e) {

e.printStackTrace();

}

}

} else {

bool = false;

File newFile = new File(path + "\\" + fileName);

try {

boolean isOkNewFile = newFile.createNewFile();

if (isOkNewFile) {

System.out.println("文件创建成功");

} else {

System.out.println("文件创建失败");

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值