File file = new File("C:/Users/John/Desktop/myfile.txt"); String filename = file.toString(); System.out.println("Filename: " + filename);
File dir = new File("C:/Users/John/Desktop"); File[] files = dir.listFiles(); for (File file : files) { if (file.isFile()) { String filename = file.toString(); System.out.println("Filename: " + filename); } }This code creates a File object for a directory located at "C:/Users/John/Desktop". Then, the listFiles() method is called on the File object to get an array of File objects representing the files and directories contained within the directory. The code then loops through each file in the array and checks if it is a file (as opposed to a directory). If it is a file, the toString() method is called on the File object and its returned value is stored in a string variable. Finally, the string representation of each file path is printed to the console. The File class is part of the java.io package, which is used for handling input and output operations.