从文件中删除用户的方法

时间:2017-06-22 04:34:36

标签: java client

此代码可以找到客户端,但无法将其删除。客户名称 - "拉斐尔" 我只是不能删除客户端,我不知道为什么,我尝试了一些代码,但没有工作,你们可以帮助我吗?

//这会删除客户

public void EliminarUtilizador(){
              ArrayList<ArrayList<String>> dados = new ArrayList<ArrayList<String>>();
            dados = _ficheiro.lerDados();
            // the line is an array that haves date stored on the file
            //0-nome 1-pass ...
            for(ArrayList<String> linha : dados){
                Utilizador a = new Utilizador(linha);
                if("Rafael".equals(linha.get(0))){
                    System.out.println("Client found");

                }
            }  
          }


    ----------------------
    //This reads the file

      public ArrayList<ArrayList<String>> lerDados(){
            ArrayList<ArrayList<String>> dados = new ArrayList<ArrayList<String>>();
            Scanner s;
            try{
            s = new Scanner(new File(nomeFicheiro));
            }catch(FileNotFoundException e){
                return dados;
            }
            String dado;

            while(s.hasNextLine()){
                dado = s.nextLine();
                dados.add(new ArrayList<>(Arrays.asList(dado.split(","))));
            }
            s.close();
            return dados;
        }

1 个答案:

答案 0 :(得分:-1)

您需要通过列表使用迭代器到迭代器。找到该项后,可以使用迭代器方法删除。以下是示例代码。

List<String> list = new ArrayList<String>();
    Iterator<String> itr  = list.iterator();
    while(itr.hasNext()){
        String str = itr.next();
        //remove item based on some condition.
        itr.remove();
    }