我想重命名一个文件,但不工作,我不知道哪里出错了 [英] i want to rename a file but don't work ,i don't know where is wrong
本文介绍了我想重命名一个文件,但不工作,我不知道哪里出错了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
public class FileAndDirectoryInfo{
public static void main(String[]args)throws IOException {
Scanner input = new Scanner(System.in);
System.out.println("Enter file or directory name");
Path path = Paths.get(input.nextLine());
if(Files.exists(path)){
// display file (or directory)information
System.out.printf("%n%s", path.getFileName());
System.out.printf("Path:%n" , path);
System.out.printf("%s,a directory%n", Files.isDirectory(path) , "Is:" +" Is not");
System.out.printf("Last modified: %s%n" , Files.getLastModifiedTime(path));
System.out.printf("Size:%s%n",Files.size(path));
if(Files.isDirectory(path))
{
System.out.printf("%nDirectory contents:%n");
DirectoryStream<path> directoryStream = Files.newDirectoryStream(path);
for (Path p : directoryStream)
System.out.println(p);
}
}
else
{
System.out.printf("%s does not exist%n" , path);
}
boolean success = false;
System.out.println("Enter path of directory to create");
String dir = input.nextLine();
// Creating new directory in Java,if it doesn't exists
File directory = new File(dir);
if(directory.exists() && directory.isDirectory()){
System.out.println("Directory already exists ...");
}else{
System.out.println("Directory not exists,creating now");
success= directory.mkdir();
if(success)
{
System.out.printf("Successfully created new directory:%s%n",dir);
}else{
System.out.printf("Failed to create new directory:%s%n",dir );
}
}
System.out.println("Enter file name to be renamed");
String filename = input.nextLine();
File f = new File(filename);
if(f.exists()&& f.isFile())
{
System.out.println("Rename File");
success = f.renameTo(f);
System.out.println("The name is:%n");
String rename = input.nextLine();
File h = new File(filename);
if(h.exists() && f.renameTo(f))
{
System.out.println("The folder was renamed");
}
if(success)
{
System.out.printf("Successfully created new name :%s%n", f);
}else{
System.out.printf("Failed to created new name :%s%n",f);
}
}
input.close();
}
}
推荐答案
你声明了main方法所在的类。
you declare the class public where main method is present.
这篇关于我想重命名一个文件,但不工作,我不知道哪里出错了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文