另一个文件使用的文件尝试移动文件时出错-Java 8-停止

时间:2018-06-25 21:48:28

标签: java java-8 filereader

很抱歉,如果这太基础了,那么我对Java 8还是陌生的。在Java中执行一些文件操作后,我试图移动文件位置。但是我收到一条错误消息,说该文件无法被其他程序访问,因此无法移动。但事实并非如此。我的代码在下面。

void Main() {
    var workPattern = new List<DayState> { DayState.MorningShift, DayState.AfternoonShift, DayState.NightShift, DayState.OffDay, DayState.OffDay };
    var startDate = new DateTime(2018, 1, 1);

    var queryDate = new DateTime(2018, 1, 4);
    Console.WriteLine($"{queryDate:d} is {StateOfDate(startDate, workPattern, queryDate)}");

    queryDate = new DateTime(2018, 1, 21);
    Console.WriteLine($"{queryDate:d} is {StateOfDate(startDate, workPattern, queryDate)}");

    queryDate = new DateTime(2018, 2, 6);
    Console.WriteLine($"{queryDate:d} is {StateOfDate(startDate, workPattern, queryDate)}");

    queryDate = new DateTime(2018, 3, 4);
    Console.WriteLine($"{queryDate:d} is {StateOfDate(startDate, workPattern, queryDate)}");
}

// Define other methods and classes here
public enum DayState {
    MorningShift, AfternoonShift, NightShift, OffDay
};

public DayState StateOfDate(DateTime startDate, List<DayState> workPattern, DateTime queryDate) {
    var numDays = Math.Floor((queryDate-startDate).TotalDays);
    var patternOffset = ((int)numDays) % workPattern.Count;
    return workPattern[patternOffset];
}

我尝试在流和for循环之后移动文件,但仍然给我相同的错误。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

对于一个文件,我将执行以下操作:

public void moveFile(String sourcePath, String targetPath) throws Exception {
    Files.move(Paths.get(sourcePath), Paths.get(targetPath);
}

您认为如何在循环中使用此功能?