Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Want to create a file with name provided by user

$
0
0
the Question is : Write a program that reads a String from the keyboard. The program then creates a file named: xx.txt (replace xx with the String that is read from keyboard)

How can I create a New File then rename it using the keyboard !!
I've tried this code but its not working !!


public class Problem1 {

    public static void main(String[] args) {

        Scanner Read = new Scanner(System.in);
        System.out.print("Enter a new name: ");
        String newName = Read.next();
        File file = new File ("xx.txt");
        File newFile = new File (newName);
        file.renameTo(newFile);

        
    }
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>