Read file...

Discussion in 'Plugin Development' started by wannezz, Apr 6, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    wannezz

    Hello, I want to read a text file, I need to know if the text file contains the sender's name,
    Here's the code where the player's name gets written to the file...


    Code:
    File file = new File("plugins/myPlugin/Players.yml");
                    BufferedWriter out = null;
                   
                    try
                    {
                        out = new BufferedWriter(new FileWriter(file, true));
                        out.write(sender.getName());
                        out.newLine();
                    }
                    catch (FileNotFoundException ex) { ex.printStackTrace();}
                    catch (IOException ex) {ex.printStackTrace();}
                    finally
                    {
                        try
                        {
                            if (out != null)
                            {
                                out.flush();
                                out.close();
                            }
                        } catch (IOException ex) {ex.printStackTrace();}
                }
    Can anyone help me?
     
  2. I think its
    Code:java
    1.  
    Code:java
    1.  
    2. [FONT=Consolas] File file = new File("plugins/myPlugin/Players.yml");[/FONT]
    3. [FONT=Consolas] LineNumberReader in = null;[/FONT]
    4. [FONT=Consolas]Array<String> lines = new ArrayList<String>();[/FONT]
    5. [FONT=Consolas] try[/FONT]
    6. [FONT=Consolas] {[/FONT]
    7. [FONT=Consolas] in = new LineNumberReader(new BufferedReader(new FileReader(file, true)));[/FONT]
    8. [FONT=Consolas] String line;[/FONT]
    9. [FONT=Consolas]while((line = in.readLine()) != null)[/FONT]
    10. [FONT=Consolas]{[/FONT]
    11. [FONT=Consolas]lines.add(line);[/FONT]
    12. [FONT=Consolas]} } catch (FileNotFoundException ex) { ex.printStackTrace();} catch (IOException ex) {ex.printStackTrace();}[/FONT]
    13. [FONT=Consolas] finally[/FONT]
    14. [FONT=Consolas] {[/FONT]
    15. [FONT=Consolas] try[/FONT]
    16. [FONT=Consolas] {[/FONT]
    17. [FONT=Consolas] if (in != null)[/FONT]
    18. [FONT=Consolas] {[/FONT]
    19. [FONT=Consolas] in.close();[/FONT]
    20. [FONT=Consolas] }[/FONT]
    21. [FONT=Consolas] } catch (IOException ex) {ex.printStackTrace();}[/FONT]
    22. [FONT=Consolas] }[/FONT]


    EDIT, WTF are you doing editor
     
  3. Offline

    wannezz



    Can you explain what this code is doing?
    I can't see anything that has something to do with a Player...
     
  4. Offline

    Njol

    You can check whether the player is in the file with lines.contains(player.getName()). Also you should only read the file when your plugin is loaded and not everytime you want to see if it contains a certain player. You can also easily add and remove palyer to/from the list so you don't have to write to the file everytime you add a player. You'll have to save the whole list when your plugin is disabled o.c. or your plugin will forget them.
     
Thread Status:
Not open for further replies.

Share This Page