Eclipse で Web 動的プロジェクトを開発しています。1 つのファイル、「users.txt」という名前のファイルは、classes フォルダー (WEB-INF/classes/users.txt) の下にあります。
クラス(サーブレットクラスではなく、基本クラス)でこのファイルの相対パスを取得するにはどうすればよいですか? そのパスを使用して、数行のテキストを追加します。
クリスチャンこれは、読み取りに役立つコードです。問題は、相対パスを使用して同じファイルに書き込み (出力) 用のオブジェクトを作成する方法がわからないことです。
public Products(){
    try{
        InputStream in = getClass().getClassLoader().getResourceAsStream("products.txt");    
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        readProducts(br);
        in.close();
            br.close();
    }catch(Exception e){
        System.out.println("File doesn't exists");
    }
    }
    public void readProducts(BufferedReader in) throws NumberFormatException, IOException{
        String id="";
        String name="";
        double price=0;
        StringTokenizer st;
        String line;
        while((line=in.readLine())!=null){
            st=new StringTokenizer(line,";");
            while(st.hasMoreTokens()){
                id=st.nextToken();
                name=st.nextToken();
                price=Double.parseDouble(st.nextToken());           
            }           
            products.put(id,new Product(id,name,price));
        }
    }