开发者

Using "pathdir" on Java Applications

First, i'm using Ubuntu! :)

I need some help here, I built a Java App, i want to set a default path tree, like this:

> cd /anyDirectory/meuRestaurante

> ls

bin/ data/

> cd bin

> ls

meuRestaurante.jar

> cd ..

> cd data

> ls

Cartoes.txt Clientes.txt

I want that my Java App save these txt's files on /data directory, while is on /bin开发者_运维问答 directory. Exctaly, how should be if i use these functions to read/save the txt's files.:

public static String readFile(String fileName) {
    try {
        File file = new File("."+fileName);
        FileReader reader = new FileReader(file);
        BufferedReader in = new BufferedReader(reader);
        String string;
        String returnString = "";
        while ((string = in.readLine()) != null) {
            returnString = "" + returnString + string;
        }
        in.close();
        return returnString;
    } catch (IOException e) {
        System.err.println("readFile " + e.getMessage());
        return null;
    }
}

public static boolean writeFile(String fileName, String newContent){
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
        out.write(newContent);
        out.close();
        return true;
    }catch(IOException e){
        System.err.println("writeFile " + e.getMessage());
        return false;
    }
}

How should be the fileName?

Anyone has a tip ?


Try this:

data/Cartoes.txt


If these files are intended to be read only, put them in a Jar, add the Jar to the applications run-time class-path and access them using getResource().

If the files are intended to be written to (read/write), avoid attempting to save files in a location relative to the application executable(1). This will lead to no end of frustration later.

Instead, put the data files in a sub-directory of ${user.home}. The sub-directory is to help ensure that applications do not overwrite each others files. The sub-directory might be based on the package name of the main class of the app., which should be unique to your organization.

(1)

  • Neither applets nor apps. launched using Java Web Start are able to discover the location the app. is stored on the local disk.

  • Microsoft has been telling developers for years not to save program settings in the 'Program Files' folder.

  • While an app. is almost guaranteed of being able to write to/read from user home, the tightening of security in recent times means that other locations are much less reliable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜