-2

I have program that read from a file. Now I created another task, that aims to update/write this file into same files. My problem now is when I generate my project's distributable file, during running and try to update my file, it does not write/update my changes. If I run it directly on my IDE, it works fine. This is wha I did so far:

private void tbleAddressMouseClicked(java.awt.event.MouseEvent evt) {                                           
    if(tbleAddress.getSelectedColumn()==3){
        AddressUtil util = new AddressUtil();
        List<AddressUtil> lists = util.getAddresses();
        Address address = lists.get(tbleAddress.getSelectedRow());
        if(tbleAddress.getValueAt(tbleAddress.getSelectedRow(), 0)!=null){
            address.setRegion("\""+tbleAddress.getValueAt(tbleAddress.getSelectedRow(), 0).toString()+"\"");
        }
        lists.set(tbleAddress.getSelectedRow(), address);
    try {
        FileWriter fw;
        fw = new FileWriter("./src/address.csv"); // This is where I doubt, if my **jar** file reads this directory
        for(Address a:lists){
            fw.append(a.getRegion);
            fw.append(",");
            fw.append(a.getAddressName());
            fw.append("\n");
            fw.flush();
        }
        fw.close();
    } ...

My getAddresses is defined as:

public List<Address> getAddresses() {
    List<Address> addresses = new ArrayList<>();
    BufferedReader br = null;
    InputStream in = this.getClass().getResourceAsStream("/address.csv"); 
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        while((line = reader.readLine())!=null){
            String[] result = line.split(",");
            Address address = new Address();
            address.setRegion(result[0]);
            address.setAddressName(result[1]);
            addresses.add(address);
        }...

My address.csv is of form:

"Region I","Sample St., Sample Address"
"Region II","Sample St., Sample Address 2"
...

Any help is much appreciated

  • "./src/address.csv" is a relative path so do you have a "src" directory in the directory where your distributable is located? Do you have an "address.csv" in that "src" directory? You should be logging exceptions or at least printing them to stdout so that you can track down issues more easily. When you run your distributable file run it with the java command line so that you can see stdout. Also, take a look at https://docs.oracle.com/javase/tutorial/essential/io/fileio.html – D.B. Mar 10 '18 at 07:23
  • If I put ./src/address.csv on my distributable file directory will my **getAddresses()** method read my ./src/address.csv file? – Alexander Paudak Mar 10 '18 at 07:28
  • It would have a better chance, there are any number of things that can go wrong with file reading/writing so I can't say for sure. You could try it and see what happens though. – D.B. Mar 10 '18 at 07:29
  • I tried, but failed to read my file. What is the better way to do it, without saving it on database? I mean, the file i read is also the file which i am going to modify but instead of it reside on csv file. – Alexander Paudak Mar 10 '18 at 07:41
  • What is the error or exception you see when you run the program? Also, "better way to do it" could mean anything. There are many different approaches you could use such as using a configuration file to specify the path to the file you want to write, allowing the user to choose the path to the file at runtime, using program arguments to pass the file path when the program begins, and others. – D.B. Mar 10 '18 at 21:15

1 Answers1

0

Your issue with writing to the file is most likely due to the relative path you're using when you define the FileWriter. The dot in your path means "current working directory" so if your program is located at path C:\myProgram and you run your program such that it uses this path as the working directory then it's going to look for C:\myProgram\src\address.csv

So depending on the requirements for your program a relative path might be appropriate, in which case you need to determine what the correct path will be and ensure the file exists at that location, or you may to use some other mechanism to find the file.

I also notice that you're using getResourceAsStream to get an InputStream to your file. You should know that this only works if the file is available on the classpath of your program.

D.B.
  • 4,523
  • 2
  • 19
  • 39
  • Thanks for your help, is it possible for the **dist** folder to read a directory outside of it? for example on directory "C:\MyDocument\MyProject\address.csv", I just solved it by putting the address list into database but I think I am not satisfied because my aim is just to read from a file and manipulate. – Alexander Paudak Mar 13 '18 at 18:12
  • Yes, of course. Your program can read from anywhere on the file system as long as it has appropriate permission to do so and there isn't some kind of error that occurs. The type of path you gave is an absolute path. If your program is hard-coded to read from that exact path all users of your program must store the file in that location. By hardcoded I mean that it is written into the program itself rather than being externally configurable. Note that if you did use a Windows path you're assuming all users are using Windows unless you allow the user to configure the path. – D.B. Mar 14 '18 at 00:51