你好我試圖追加一些變量到一個目錄來創建一個新的子目錄,我在我目錄中,但我不知道如何做到這一點,我知道.mkdir( )允許我創建一個新目錄,但我不知道如何追加變量,然後創建新目錄,以下是我的嘗試: package movefile;附加字符串到目的地來創建新的目錄
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MoveFile
{
static String newfile;
public static void main(String[] args)
{
File srcFolder = new File("/Users/francis/Desktop/folder1");
File destFolder = new File("/Users/francis/Desktop/folder2");
//make sure source exists
if(!srcFolder.exists()){
System.out.println("Directory does not exist.");
//just exit
System.exit(0);
}else{
try{
copyFolder (srcFolder,destFolder);
}catch(IOException e){
e.printStackTrace();
//error, just exit
System.exit(0);
}
}
System.out.println("Done");
}
public static void copyFolder(File src, File dest)
throws IOException{
if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdir();
System.out.println("Directory copied from "
+ src + " to " + dest);
}
//list all the directory contents
String files[] = src.list();
FilenameFilter filter = new FilenameFilter()
{
@Override public boolean accept(File dir, String name)
{
return name.endsWith(".pdf");
}
};
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file); //needed for moving
//third attempt
File f = null;
File f1 = null;
String vendor;
String orderno;
String desc;
String v, v1, p;
boolean bool = false;
try {
f = new File("/Users/francis/Desktop/folder1/1234567898.pdf");
f1 = new File("/Users/francis/Desktop/folder2/");
v = f.getName();
v1 = f1.getName();
v = v.length() > 9 ? v.substring(0,8) : v;
p = "c3269";
vendor = "VendorName";
v = "file_" + p + " - " + v + ".pdf";
File destFile = new File(dest, v); //get dest and insert (append) project number so it creates new folder
copyFolder(srcFile,destFile);
File appendProject = new File("/Users/francis/Desktop/folder2/");
boolean successfull = appendProject.mkdir();
if (successfull)
{
System.out.println("New directory was created:");
}
else
System.out.println("Failed to create new directory");
bool = f.exists();
if(bool)
{
System.out.println("File name:" + v);
}
bool = f1.exists();
if (bool)
{
System.out.println("Folder name:" + v1);
}
}catch(Exception e){
e.printStackTrace();
}
}
}else{
//if file, then copy it
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
System.out.println("File copied from " + src + " to " + dest);
}
}
}
這是檢查.pdf文件附加P和V值的名稱的文件夾的那一刻,修剪串8個字符,將文件移動到一個新的文件夾,但我希望他們能夠被移動和它會根據位於變量p內的項目編號創建新的目錄。
任何幫助將不勝感激,謝謝一堆!
請從格式化代碼開始,以減少誤解。如果方法太大,例如添加缺少的變量和方法或添加說明。什麼是'srcFile','dest'等以及什麼是'copyFolder(srcFile,destFile);'意味着什麼? – Thomas
srcFile聲明從其中複製項目的源文件,dest是目標文件,這些項目也將被複制到哪裏,複製文件夾是我的課程的名稱,它是爲了移動目的,但我沒有'我想將我的整個程序源代碼粘貼到 –
將它添加到您的帖子,請不要將問題和評論中的信息結合起來使其更難。 – Thomas