文件仅在放置在将从jar中删除的位置时才可访问

时间:2017-09-01 16:03:55

标签: java netbeans jar classpath file-access

我是NetBeans IDE的新手,在构建jar文件后我正在努力访问文件。在阅读了关于此主题的许多帖子后,我决定尝试以下代码:

BufferedReader read = new BufferedReader(new InputStreamReader(getClass()。getResourceAsStream(" /file.txt")));

当我的文件放在" build" .class文件所在的项目文件夹,但当然这是一个问题,因为它在"清理和构建"创建jar文件时的进程。我已经尝试将它放在src文件夹中,在一个单独的"资源"包,并在目录的根目录中。我也尝试用" file.txt"调用getResourceAsStream()。和" /src/file.txt,"但是当文件带有.class文件时,它仅适用于上述配置。任何提示将不胜感激!

2 个答案:

答案 0 :(得分:0)

为什么不将文件夹放在tomcat bin中并从代码中引用该目录。所以maven clean不会改变文件,你可以删除,更新文件而无需重启应用程序。 (这里我有文件内容等)

public class UrunAdapter extends ArrayAdapter<Urunler> {

ArrayList<Urunler> urunlist;
private static LayoutInflater vi;
int Resource;
ViewHolder holder;
private Context context;
public ArrayList<UrunlerSecilen> secilenUrunler=new ArrayList<UrunlerSecilen>();;


public UrunAdapter(Context context, int resource, ArrayList<Urunler> objects) {
    super(context, resource, objects);
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    urunlist = objects;
    this.context = context;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (convertView == null) {
        convertView = vi.inflate(R.layout.row, parent, false);
        ViewHolder holder = new ViewHolder();
        //setting up the Views
        convertView.setTag(holder);
    }

    ViewHolder holder = (ViewHolder) convertView.getTag();


    holder.tvUrunAdi=(TextView)convertView.findViewById(R.id.tvUrunAdi);




    holder.tvFiyat=(TextView)convertView.findViewById(R.id.tvFiyat);

    holder.imgUrunFoto=(ImageView)convertView.findViewById(R.id.imgUrunFoto);

    holder.btnArttir=(TextView)convertView.findViewById(R.id.btnArttir);

    holder.btnEksilt=(TextView)convertView.findViewById(R.id.btnEksilt);


    final EditText edtAdet=(EditText)convertView.findViewById(R.id.edtAdet);



    holder.btnArttir.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String strAdetVal=edtAdet.getText().toString();


            int adetValue=Integer.parseInt(strAdetVal);



            if (adetValue>=20)
            {

                adetValue=20;
            }

            else
            {
                adetValue=adetValue+1;
            }
            edtAdet.setText(Integer.toString(adetValue));


        }
    });



    holder.btnEksilt.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String strAdetVal=edtAdet.getText().toString();


            int adetValue=Integer.parseInt(strAdetVal);



            if (adetValue<=0)
            {

                adetValue=0;
            }

            else
            {
                adetValue=adetValue-1;
            }

            edtAdet.setText(Integer.toString(adetValue));


        }
    });





    holder.tvUrunAdi.setText(urunlist.get(position).getUrunAdi());


    Picasso.with(context).load(urunlist.get(position).getFoto()).fit().into(holder.imgUrunFoto);



    holder.tvFiyat.setText(urunlist.get(position).getFiyat());




    return convertView;

}


static class ViewHolder {

    public TextView tvUrunAdi;
    public TextView tvFiyat;
    public ImageView imgUrunFoto;
    public EditText edtAdet;
    public TextView btnArttir;
    public TextView btnEksilt;
    public TextView btnEkle;
    public TextView lblAdet;
    public TextView tvUrunId;



}


private static class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {


    ImageView bmImage;


    public DownloadImageTask(ImageView bmImage) {



        this.bmImage = bmImage;




    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);

    }

}

我有方法readFile来解析一些数据并在上面的例子中构建ArrayList。

了解System Properties

答案 1 :(得分:0)

原来解决方案非常简单......我一直在尝试手动创建资源文件夹,但是在构建jar时内容仍然被删除。相反,我创建了一个资源包,并将文件放入src文件夹中的自动生成的文件夹中,该文件夹将文件打包到jar中。感谢大家!