java.io.FileWriter: Get the path of File created
Asked Answered
L

2

5

I have created an CSV file using java.io.FileWriter and its creating the file in my workspace but I want to display the location (absolute path) of the path where file is created. I know we can use file.getAbsolutePath() if we have created file using FILE, but since I have created the CSV file using FileWriter, I am not sure how to get absolute path of its created file. I tried converting it to String and then assigning it to FILE but still not able to get the location of the file. How to get the absolute Path of the file created using FileWriter?

Luke answered 23/7, 2017 at 2:44 Comment(0)
L
3
import java.io.File;    

public class Main {

    private static String FILE_NAME = "file.csv";
    
    public static void main(String[] args) {
        
        try {
            //create the file using FileWriter
            FileWriter fw = new FileWriter(FILE_NAME);
            //create a File linked to the same file using the name of this one;
            File f = new File(FILE_NAME);
            //Print absolute path
            System.out.println(f.getAbsolutePath());
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
    }
}
Lite answered 23/7, 2017 at 3:1 Comment(3)
FileWriter is creating the file, so why you used File f again ? isnt it not enough using fw.getAbsolutePath();Edgington
How could this be the accepted answer? OP said he knew how to get the path using FILE and asked to get the path using FILEWRITER; here you are basically declaring both and getting the path with the former.Anhydrous
BATMAN happines, my happinesDryasdust
L
4

Even if you are not creating a new instance of a file writer by passing in a file it is a easy change and will make your issue easy to solve Use this:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        try {
            File file = new File("res/example.csv");
            file.setWritable(true);
            file.setReadable(true);
            FileWriter fw = new FileWriter(file);
            file.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Limber answered 23/7, 2017 at 3:0 Comment(0)
L
3
import java.io.File;    

public class Main {

    private static String FILE_NAME = "file.csv";
    
    public static void main(String[] args) {
        
        try {
            //create the file using FileWriter
            FileWriter fw = new FileWriter(FILE_NAME);
            //create a File linked to the same file using the name of this one;
            File f = new File(FILE_NAME);
            //Print absolute path
            System.out.println(f.getAbsolutePath());
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
    }
}
Lite answered 23/7, 2017 at 3:1 Comment(3)
FileWriter is creating the file, so why you used File f again ? isnt it not enough using fw.getAbsolutePath();Edgington
How could this be the accepted answer? OP said he knew how to get the path using FILE and asked to get the path using FILEWRITER; here you are basically declaring both and getting the path with the former.Anhydrous
BATMAN happines, my happinesDryasdust

© 2022 - 2024 — McMap. All rights reserved.