java - 我的主循环中的步骤有问题
问题描述
基本上,这个程序接收一个新动漫剧集的对象数组列表,将它们下载到一个文件夹,utorrent 将自动检测其中的 .torrent 文件并将它们作为输出下载到另一个文件夹,同时还将 .torrent 输出到“完成”目录。该程序将下载,等待下载完成,重命名并移动文件,然后检查下一个并从下一个重新开始。
但是我遇到了一个问题,由于某种原因要下载多个项目,它只会跳过“等待下载&&重命名/移动文件”,并且在第一次迭代工作正常后结束,它仍然会下载实际的项目 2文件,但在开始后忽略其余的 do/while,并在实际代码中打印出我的调试 sysoutprint,表明程序正在执行这些步骤,但在第一次之后忽略 do/while。我已经尝试在每次迭代中启动 Mover 和 DownloadProgressTracker,而不是之前和仍然相同的情况。我确信使用线程/易失性布尔值我可以使它工作,但现在宁愿避免它。
测试Main.java
public class TestingMain
{
public static void main(String[] args)
{
Program p = new Program();
}
}
程序.java
public class Program
{
private final String Torrent_Working_Dir = "torrent working path";
private final String Torrent_Finished_Dir = "torrent finished path";
private final String Completed_Downloads_Dir = "output files of torrent path";
public boolean Downloading_Still;
public boolean Renaming_Still;
private ArrayList<ToDownload> Items_To_Download = new ArrayList<ToDownload>();
private ToDownload dls;
private Mover mv;
private DownloadProgressTracker Prog;
public Program()
{
FillArray();
PrintArray();
DownloadThese(Items_To_Download);
}
public void FillArray() // putting fake elements into array as examples
{
dls = new ToDownload("AnimeOne", "linkOne", "RawAnimeOne", "formatedTitleOne", "DirOne");
Items_To_Download.add(dls);
dls = new ToDownload("AnimeTwo", "linkTwo", "RawAnimeTwo", "formatedTitleTwo", "DirTwo");
Items_To_Download.add(dls);
}
public void PrintArray() // debugging
{
for(ToDownload t : Items_To_Download)
{
System.out.println("Title: " + t.getTitle() + "\nLink: " + t.getLink() + "\nRaw: "
+ t.getRaw() + "\nFinal: " + t.getFinal() + "\nRoot: " + t.getRoot() + "\n\n");
}
}
public void DownloadThese(ArrayList<ToDownload> dls)
{
ArrayList<ToDownload> temp;
int target = dls.size(); // the amount of downloads to do
int count = 0; // keeps track of how many are done
mv = new Mover();
Prog = new DownloadProgressTracker(Torrent_Finished_Dir);
do
{
for(ToDownload dl : dls)
{
// step 1
// download the torrent file into torrent working dir
//
Downloading_Still = true;
Renaming_Still = true;
try
{
// step 2
// wait for utorrent to finish downloading and put the torrent into it's finished dir
do{Prog.waitForDownload(this);}while(Downloading_Still != false);
// step 3
// rename and move the file to the folder it belongs in from utorrents file output folder
do{mv.checkExist(this, dl.getTitle(), dl.getFinal(), Completed_Downloads_Dir, dl.getRoot());}while(Renaming_Still != false);
}
catch(IOException e)
{
}
// step 4
// count = 0, count++, count now = 1
count++;
}
}
// step 5
// if count is not == target go back to step 1
while(count != target);
}
}
是的,我忽略了实际的下载部分。在这种情况下,这不是问题。只需从 dls 中的当前 dl 获取信息并将 .torrent 文件下载到指定目录。
下载ProgressTracker.java
public class DownloadProgressTracker
{
private String Path;
private File Root;
private File[] Root_Folder;
private boolean Still_Downloading;
public DownloadProgressTracker(String path)
{
Path = path;
}
public void waitForDownload(Program p) throws IOException
{
Still_Downloading = true; // set true before checking
Root = new File(Path); // initiate folder step 1
Root_Folder = Root.listFiles(); // initiate folder step 2
while(Root_Folder.length == 0) // if folder is empty just update folder
{
Root_Folder = Root.listFiles(); // update folder until not empty
}
int length = Root_Folder.length; // so that next while can be updated by first
while(length != 0) // if folder contains something
{
for(File f : Root_Folder) // for each File in folder do something
{
try{f.delete();}catch(Exception e){} // delete the file
}
}
Still_Downloading = false;
p.Downloading_Still = Still_Downloading; // return false
}
}
下载.java
public class ToDownload
{
private String Title, Link, Raw_Title, Final_Title, Root_Folder;
public ToDownload(String title, String link, String raw, String finalT, String root)
{
Title = title;
Link = link;
Raw_Title = raw;
Final_Title = finalT;
Root_Folder = root;
}
public String getTitle()
{
return Title;
}
public String getLink()
{
return Link;
}
public String getRaw()
{
return Raw_Title;
}
public String getFinal()
{
return Final_Title;
}
public String getRoot()
{
return Root_Folder;
}
}
移动器.java
public class Mover
{
private File from, to, Finished_Dls;
private File[] start;
private String toDest, toOther, fromDest;
private boolean Moving_File, Renaming_File, Renaming_Still;
public void checkExist(Program p, String Title, String finalTitle, String FromDest, String ToDest)
{
Finished_Dls = new File(FromDest);
start = Finished_Dls.listFiles();
toDest = ToDest + "\\";
toOther = toDest;
fromDest = Finished_Dls.getPath() + "\\";
Renaming_Still = true;
Moving_File = true;
Renaming_File = true;
for(File f : start)
{
if(f.getName().contains(Title))
{
fromDest += f.getName();
toDest += f.getName();
do{MoveFileTo(fromDest, toDest);}while(Moving_File);
do{RenameFile(toOther, toDest, finalTitle);}while(Renaming_File);
}
}
Renaming_Still = false;
p.Renaming_Still = Renaming_Still;
}
public void MoveFileTo(String from, String to)
{
this.from = new File(from);
this.to = new File(to);
try
{
Files.copy(this.from.toPath(), this.to.toPath());
this.from.delete();
Moving_File = false;
}
catch(IOException e)
{
System.out.println("Failed To Move File");
}
}
public void RenameFile(String from, String to, String finalTitle)
{
this.from = new File(to);
this.to = new File(from + "\\" + finalTitle);
try
{
this.from.renameTo(this.to);
Renaming_File = false;
}
catch(Exception e)
{
System.out.println("Failed To Rename File");
}
}
}
解决方案
推荐阅读
- javascript - 简单的语法问题,不知道为什么函数返回 undefined
- python - 将行转换为列标题
- sql - 如何在sql中转换为pivot
- spring-boot - 如何在不更改 jdbc url 的情况下更改 H2 的默认模式?
- spring-boot - 有没有办法在一定时间后使 spring websocket 连接过期?
- c# - GrapeCity Active Reports Group Header 强制重复
- datetime - 如何根据标签或值获取条形的日期
- vue.js - 滚动屏幕时如何关闭vue弹出窗口
- javascript - Django 和 Javascript - Like 按钮不起作用
- node.js - Instagram 爬虫 nodejs