'Java how to print filename?
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class hello {
public static void main(String[] args) {
try {
FileReader fin = new FileReader("c:\\windows\\system.ini");
Scanner scn = new Scanner(fin);
while (scn.hasNext()) {
String tmp = scn.nextLine();
System.out.println(tmp);
}
fin.close();
scn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
how can i print c:\windows\system.ini (path? file?name). Is there any way to print the path?
Solution 1:[1]
Check this out:
try {
File file = new File("c:\\windows\\system.ini");
FileReader fileReader = new FileReader(file);
System.out.println(file.getName());
System.out.println(file.getPath());
Scanner scn = new Scanner(fileReader);
while (scn.hasNext()) {
String tmp = scn.nextLine();
System.out.println(tmp);
}
fileReader.close();
scn.close();
}catch (Exception e) {
}
you can use File then pass it to FileReader.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aref behboodi |
