Results 1 to 6 of 6

Thread: Path Operations and File Operations in Java

  1. #1
    Join Date
    Aug 2006
    Posts
    209

    Path Operations and File Operations in Java

    Hi friends,
    I have done Java programming language to some extent. I have recently finished with the topic of an I/O Streams. After that I am studying about the path and file operations. But I am not able to understand the contents of that maybe because I don't know anything about it. So thought that someone can help me on this topic.!! Please tell me about the Path Operations and File Operations in Java. Hope that I will get the response sooner.!!
    Blessings to you

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Path Operations and File Operations in Java

    Path Operations and File Operations are the types of Path class. You can describe path class as a programmatic representation of a path in the file system. The file name and directory list used to construct the path are contained by the Path object. The two types of Path class are described as follows :
    1. Path operations - This type of operations contains methods for returning parts of a path. The part of the path consists of root, name, or parent directory. The path operation also contains methods for manipulating a path.
    2. File operations - This operations contains methods for opening a file for I/O, creating a file, creating a directory, deleting a file, copying a file, etc.

  3. #3
    Join Date
    Nov 2005
    Posts
    1,323

    Re: Path Operations and File Operations in Java

    A Path instance contains the information used to specify the location of a file or directory. You will have to provide a path with a series of one or more names. A Path may consist of just a single directory or file name. Creating a path is not a tough task. You can easily create that by using get methods from the Paths helper class. The following sample may help you in creating the path :
    Code:
    Path pa1 = Paths.get("/tmp/fear");
    Path pa2 = Paths.get(args[0]);
    Path pa3 = Paths.get("file:///Users/Hike/PathExample.java");

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Path Operations and File Operations in Java

    In many scenarios you need to join two paths. You can join two different paths by using the resolve method. For doing that, you need to pass in a partial path. This path does not include a root element, and that partial path is appended to the original path. The following sample of coding may help you in understanding this :
    Code:
        Path pa1 = Paths.get("/home/Hike/fear");       
        System.out.format("%s%n", p1.resolve("bar"));  
        or
        Path pa1 = Paths.get("C:\\home\\Hike\\fear");    
        System.out.format("%s%n", p1.resolve("bar")); 
    // Passing an absolute path to the resolve method returns the passed-in path. 
        Paths.get("fear").resolve("/home/Hike");

  5. #5
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Path Operations and File Operations in Java

    The Path class offers a rich set of methods for reading, writing, and manipulating files and directories. You should be knowing that all methods that access the file system can throw an IOException. The best way to catch these exceptions is by writing the coding these methods into a try block and to catch any exceptions in a catch block. I have provided you with the code snippet by which you can get an idea of using the getFile method :
    Code:
    try {
        ...    
    } catch (NoSuchFileException x) {
        System.err.format("%s this file does not exist\n", x.getFile());
    }

  6. #6
    Join Date
    Jul 2006
    Posts
    442

    Re: Path Operations and File Operations in Java

    I am providing sample code for some basic File Operations using java.io package, which may be much useful for you.
    Java example code to create a new file :
    Code:
    try 
        {
            File f = new File("path and file_name");
            boolean success = f.createNewFile();
        }
        catch (IOException e)    {   }
    Java example code to read text from a file :
    Code:
    try 
        {
            BufferedReader br = new BufferedReader(new FileReader("path and file_name "));
            String st;
            while ((st = in.readLine()) != null) 
            {
                process(st);
            }
            br.close();
        } 
        catch (IOException e) 
        {
        }
    Java code for creating a File :
    Code:
    try {
            File f = new File("filename");
        
            boolean success = f.createNewFile();
            if (success) {
            } else {
            }
        } catch (IOException e) {
        }
    "When they give you ruled paper, write the other way..." J.R.J.

Similar Threads

  1. How to make nautilus queue up the file operations in Ubuntu?
    By Raj Desai in forum Operating Systems
    Replies: 5
    Last Post: 07-02-2012, 06:51 PM
  2. Operations management
    By RAJni-Gandha in forum Education Career and Job Discussions
    Replies: 4
    Last Post: 18-11-2010, 04:56 PM
  3. Dial a Job extends its operations to Pune
    By Job-finder in forum Education Career and Job Discussions
    Replies: 2
    Last Post: 06-05-2010, 01:23 PM
  4. Operations on matrices
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 01:34 AM
  5. Replies: 3
    Last Post: 18-03-2008, 10:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,570,154.39220 seconds with 17 queries