2010-10-12 53 views
0

首先,我使用的是Ubuntu! :)在Java应用程序中使用“pathdir”

我需要一些帮助这里, 我建了一个Java应用程序,我想设置默认路径树,就像这样:

> cd /anyDirectory/meuRestaurante
> ls
bin/ data/
> cd bin
> ls
meuRestaurante.jar
> cd ..
> cd data
> ls
Cartoes.txt Clientes.txt

我想,我的Java应用程序保存这些TXT的文件上/data目录,同时是/bin目录。 Exctaly,应该怎么如果我使用这些功能来读取/保存TXT的文件:

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; 
    } 
} 

如何应该是fileName

任何人有小费?

回答

0

试试这个:

data/Cartoes.txt 
0

如果这些文件旨在为只读,把它们放在一个罐,该罐添加到应用程序的运行时类路径和使用的getResource访问它们()。

如果要将文件写入(读取/写入),请避免尝试将文件保存在相对于应用程序可执行文件(1)的位置。这将导致以后无法终结挫折。

而是将数据文件放在$ {user.home}的子目录中。子目录将帮助确保应用程序不会覆盖其他文件。子目录可能基于应用程序主要类的包名,这对于您的组织来说应该是唯一的。

(1)

  • 无论小应用程序,也不应用程序。使用Java Web Start启动可以发现应用程序的位置。存储在本地磁盘上。

  • 微软一直告诉开发人员多年不要将程序设置保存在“Program Files”文件夹中。

  • 虽然一个应用程序。几乎可以保证能够向用户家中写入/读取,但近期安全性的提高意味着其他位置的可靠性要低得多。