0

I have used this reference to read a file on my project. but i need to store likewise i don't know how to do this? please help me.

for reading a file i have taken the code from the link

InputStream csv = 
             SomeClassInTheSamePackage.class.getResourceAsStream("filename.csv");

Like this can anyone help me with writing a file

currently I'm using this code:

 Writer output = null;
 output = new BufferedWriter(new FileWriter("./filename.csv"));

But it throws FileNotFound Exception at runtime

Issue is with locating file path. it works fine if i give absolute path. but it needs to be run in any computer

Community
  • 1
  • 1
FreshBits
  • 1,003
  • 4
  • 13
  • 25

4 Answers4

0

The FileWriter will create files as required. It doesn't have to exist already.

The only way you can get this error is if the current working directory no longer exists or you don't have permission to create a file in the directory.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Check your permissions:

Writer output = null;
if (new File("./filename.csv").canWrite()) {
   System.out.println("You have not permissions");
} else {
   output = new BufferedWriter(new FileWriter("./filename.csv"));
   ...
}
isvforall
  • 8,768
  • 6
  • 35
  • 50
  • Issue is with locating file path. it works fine if i give absolute path. but it needs to run in any computer – FreshBits Jan 31 '13 at 17:47
0

OK..Use the following code to get the path of file:

String path = System.getProperty("user.dir");
FileWriter fw = new FileWriter(path+File.separator+"filename.csv");

EDIT

Here is a Demo Code that is creating file "pqr.txt" in package myPackage. Given that I am executing the class file using command java myPackage.AccessCheck i.e from one directory above the mypackage.

package myPackage;
import java.io.FileWriter;
import java.io.File;
public class AccessCheck
{
    protected void callme()
    {

        try
        {
            String name = System.getProperty("user.dir")+File.separator+AccessCheck.class.getPackage().getName()+File.separator+"pqr.txt";
            System.out.println(name);
            FileWriter fw = new FileWriter(name);
        }catch(Exception ex){ex.printStackTrace();}
    }
    public static void main(String st[])
    {
        AccessCheck asc = new AccessCheck();
        asc.callme();
    }
}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • I don't get File.separator – FreshBits Jan 31 '13 at 18:03
  • `File.separator` gives The `system-dependent` default `name-separator` String. For example : On Microsoft Windows systems it is "\" `(c:\abc)` .. Whereas in On UNIX systems the value of this field is "/" . Watch here for more information on this : http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#separator – Vishal K Jan 31 '13 at 18:08
  • Tell me the name of your `package` and the absolute path of this `file`. And also tell me that from which directory are you executing `java` command. – Vishal K Jan 31 '13 at 18:18
  • @VishalK `"./filename.csv"` is nothing but `"filename.csv"` that means he/she is storing file in the root folder, which is working fine for me. I mean both are working fine. – Smit Jan 31 '13 at 18:19
0

Instead of calling getResourceAsStream(), call class.getResource() or classLoader.getResource(). These functions return a URL giving the location of the resource. If the resource is a plain file on the filesystem, the URL will be a file: URL.

Once you have the URL, you can convert it to a URI and pass it to the File constructor which takes a URI. Then you can use the File object to open the file.

Note that getResource() can return URLs that the File constructor can't deal with. For example, if the resource is found in a jar file, then the URL might look like this:

jar:file:/C:/path/to/JDK/lib/vm.jar!/java/lang/Object.class

The File constructor can't deal with URLs like this. This may or may not be a concern for you.

Kenster
  • 23,465
  • 21
  • 80
  • 106