What Is Abstract Path Name in Java?
Java applications can read data from external resources such as files. The File class in Java models an abstract file, including its name and location. Using this in conjunction with other Java classes, developers can create programs in which they can read, write and process file data. An abstract pathname is a key element in the File class, specifying the path to a particular file.
-
File Class
-
The File class allows developers to specify the details of a particular file, including its location. Application programming code can use the File class constructor method to create an object instance of the class. The following sample code demonstrates:
File dataFile = new File("my_data.txt");The code passes a text string parameter representing a file. In this case, the file is stored in the default application directory and is saved as "my_data.txt" for plain text content. When the Java program processes this line, it resolves the passed parameter into an abstract pathname. The constructor method can alternatively take two parameters representing parent and child elements of the pathname, as follows:
File dataFile = new File("/data", "my_data.txt");This represents the file stored at the following location:
data/my_data.txtThe text file is stored inside a folder named "data" which is also in the default application directory. (See References 1, 2)
Uses
-
The File class forms a major element in many applications, as it allows code to access external resources. Some Java applications use databases and some use data stored in files. These files can hold formatted data such as XML markup code. The File class models a file location, but to actually read from or write to a file, programs need to use additional classes. For example, BufferedReader and InputStream provide methods for reading individual data items such as bytes from specific file locations.
-
Prefix
-
The abstract pathname in Java applications using the File class consists of two parts: the prefix and a sequence of names. The prefix is not always necessary, but provides the means to cope with different operating systems. Operating systems model and represent directories in different ways. The prefix part of a pathname may include a representation of a particular disk drive or a series of one or more slashes indicating directories.
Names
-
The name elements in an abstract pathname comprise text strings. These names can represent files or folders. In the "dataFile" example, the directory is represented using the string "data" and the file is represented using the string "my_data" together with the file format extension. The relationships between directories and files are represented using slashes. The fact that Java evaluates abstract pathnames from passed code parameters means that programs can function in multiple operating systems and environments.
-
References
Resources
- Photo Credit Jupiterimages/Photos.com/Getty Images