每当新文件被重命名,它们是否可以放入一个文件夹中?是否可能?

huangapple 未分类评论45阅读模式
英文:

Whenever new files will get renamed it should come in one Folder,is it possible?

问题

package BrokenLink;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class newTestFile {

    public static void main(String[] args) {
        CreateFileRenameExisting("PCPAutomation.xlsx");
    }

    // Rename an existing file and create a new file
    public static void CreateFileRenameExisting(String filename) {
        // Define the folder path where you want the output to be placed
        String outputFolderPath = "C:\\path\\to\\output\\folder\\"; // Replace with your desired folder path

        // Create a new file
        File outputFile = new File(outputFolderPath + Validatedate() + "_" + filename);

        try {
            if (!outputFile.exists()) {
                Workbook wb1 = new XSSFWorkbook();
                FileOutputStream fileOut1 = new FileOutputStream(outputFile);
                wb1.write(fileOut1);
                fileOut1.close();
                System.out.println("File is created");
            } else {
                System.out.println("File already exists");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String Validatedate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH_mm_ss");
        Date date = new Date();
        String date1 = dateFormat.format(date);
        return date1;
    }
}

Please replace "C:\\path\\to\\output\\folder\\" with the actual path where you want the output file to be placed. This modified code ensures that the output file is created in the specified folder with the current date and time appended to the filename.

英文:

Below is the code for Renaming the existing Excel file,
The output is coming anywhere in the project but i want in a particular Folder the Output should come with current date and time using Selenium web driver/java

package BrokenLink;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
	public class newTestFile {

	    public static void main(String[] args)
	    {
	        CreateFileRenameExisting("PCPAutomation.xlsx");

when trying to add the folder details getting this Exception-->C:\Users\skumari1\eclipse-workspace\AlarmTest\genericFiles\PCPAutomation.xlsx (The system cannot find the path specified)-->Not able to to give the Folder address

	    }

	    //Rename an existing file and create a new file
	    public static void CreateFileRenameExisting(String filename)
	    {
	        //get current project path
	        String filePath=System.getProperty("user.dir");
	        //create a new file
	        File file=new File(filePath+"\\"+filename);
	        try {
	            if(!file.exists()) {
	            	Workbook wb1 = new XSSFWorkbook();

	            	FileOutputStream fileOut1 = new FileOutputStream(filename);

	            	wb1.write(fileOut1);

	            	fileOut1.close();
	                //file.createNewFile();
	                System.out.println("File is created");
	            }
	            else
	            {
	                File backupFile=new File(filePath+"\\"+ Validatedate()  +  file.getName());
	                System.out.println("File already exist and backup file is created");
	                file.renameTo(backupFile);
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }

public static String Validatedate() {
			SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH_mm_ss");
			Date date = new Date();
			String date1= dateFormat.format(date);
			// System.out.println("Current date and time is " +date1);
			 return date1;
		}
    }

Kindly help me with the above code so that i can give the Folder address in the above code, Thank you in advance

答案1

得分: 0

我的第一个想法是文件夹尚不存在。对于某些人来说,这可能有点不直观,但是Java不会自动创建文件夹来存放新文件。您必须确保在将文件放入文件夹之前,该文件夹已经存在,就像这样:

void writeToFile(File file) throws IOException {
    File folder = file.getParentFile();
    if (!folder.exists()) {
        folder.mkdirs();
    }
    //... 创建文件并写入其中
}

另外,File::mkdirs 方法会返回一个布尔值,如果创建失败则返回 false。因此,我通常会手动抛出 IOException 异常:

if (!folder.mkdirs()) throw new IOException("无法创建文件夹:" + folder.getAbsolutePath());
英文:

My first thought is that the folder is not existing yet. For some it might be a bit counter-intuitive, but Java does not create the folder to place the new file into it. You must make sure, that the folder exists before placing the file into it, like:

void writeToFile(File file) throws IOException {
    File folder = file.getParentFile();
    if (!folder.exists()) {
        folder.mkdirs();
    }
    //... create file and write into it
}

Also, File::mkdirs method returns boolean, so false if failed to create. For that reason I usually throw IOException manually:

if (!folder.mkdirs()) throw new IOException("Failed to create folder: " + folder.getAbsolutePath());

huangapple
  • 本文由 发表于 2020年7月27日 17:36:25
  • 转载请务必保留本文链接:https://java.coder-hub.com/63112541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定