0
main Klasse zu übernehmen, die Benutzereingaben als Dateinamen akzeptiert.verwenden Sie Konstruktoren, um eine Textdatei zu erstellen und Benutzereingaben für den Dateinamen
public class Main {
public static void main(String args[]) throws Exception{
FileOperator fileObject = new FileOperator();
System.out.println(Strings.userMenu);
@SuppressWarnings("resource")
Scanner scan= new Scanner(System.in);
String userInput = scan.next();
if(userInput.isEmpty()){
System.out.println(Strings.inputExpected);
}
else{
fileObject.fileOperator(userInput);
}
}
}
/* It is a generic file which takes user input as a file name and saves the file with that name.*/
public class FileOperator {
/*
* The Below Method fileOperator will access filename as a input from user.
* Checks if the file is available in given path.
* If File is available then file exist message will be printed.
* Else new file with that name will b created.
* If user enters nothing then error message will be popped up.
*/
public void fileOperator(String userInputFileName) throws Exception {
File newFileName = new File(userInputFileName);
if(newFileName.exists() && !newFileName.isDirectory()) {
System.out.println(Strings.fileExists);
}
else if (newFileName.createNewFile()){
System.out.println(Strings.fileCreated);
}
else if(newFileName.equals("")){
System.out.println("");
}
else{
System.out.println(Strings.errorForFileNotCreated);
}
}
}
Aber das Problem ist, ich möchte ein Dateiobjekt mit einem Konstruktor erstellen. Ich bin sehr neu in Java, also bitte helfen Sie mit.
Verwandte: http://stackoverflow.com/questions/579445/java-constructors – 0aslam0
Sie bitte Ihre Frage auszuarbeiten. Verwenden Sie den Konstruktor nicht bereits in 'File newFileName = new File (userInputFileName);'? Hab deine Frage nicht verstanden. –
Ich möchte dies mit Java Coding Standards tun, wenn ich Konstruktoren in verschiedenen Klassen oder Paketen erstellen muss. wie ist das gemacht ??? –