readingtxt files

Discussion in 'Plugin Development' started by 1337, Jan 23, 2011.

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

    1337

    i know this has nothing to do with bukkit but i cant find the ansewer anywhere
    how would i read a txt file like this?
    playername:50:400:3939
    i need to read all the numbers seperatly and the player name. i would like to do this in a txt file

    anybody now how?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 6, 2016
  2. Offline

    McAndze

    I use BufferedReader. I'll edit in a code example.

    EDIT:

    Here it is. I'm using BufferedReader to read the file, and StringTokenizer to split the String.

    Code:
    public <RETURN_TYPE> reader(String playerName, String filename){
        try {
            BufferedReader br = new BufferedReader(new FileReader(filename));
            String curLine;
    
            while ((curLine = br.readLine()) != null){
                StringTokenizer strTok = new StringTokenizer(playerName, ":");
                if (strTok.nextToken().equals(playerName)){
                    // Here you can do whatever you want with the rest of the tokens. Just use strTok.nextToken() to get the rest of the token, split by colons.
                }
            }
        // This catches all the exceptions. You can go more in detail here if you want.
        } catch(Exception e){
            e.printStackTrace();
        }
    }
     
  3. Offline

    1337

    thank you also how would you add a new line to a txt file without overwriting the whole thing?
     
  4. Offline

    McAndze

    You mean directly from the code?

    Here is an example.:

    Code:
    public void writer(String filename, String whatToWrite){
            try {
                BufferedWriter bw = new BufferedWriter(new FileWriter(filename, /* True if you wanna append */ true));
                bw.write(whatToWrite + "\n"); // The \n is to make a new line.
                bw.flush();
                bw.close();
            } catch (Exception e){
                e.printStackTrace();
            }
        }

    That piece of code should work.
     
  5. Offline

    1337

    when using you method i get this error
    Code:
     java.util.NoSuchElementException
        at java.util.StringTokenizer.nextToken(Unknown Source)
        at Test.main(Test.java:20)
    this is what im using ** are in place to keep what i am doing secret i dont want to be copyed :)
    Code:
        public static void main (String [] args){
    
              try {
                   BufferedReader br = new BufferedReader(new FileReader("******"));
                   String curLine;
    
                   while ((curLine = br.readLine()) != null){
                    StringTokenizer strTok = new StringTokenizer(name, ":");
                    if (strTok.nextToken().equals(name)){
                       String ****  = strTok.nextToken();
                    **** = Integer.parseInt(****);
                    String Nextlvl = strTok.nextToken();
                   **** = Integer.parseInt(*****);
                    String ****** = strTok.nextToken();
                    ***** = Integer.parseInt(******);
                    // Here you can do whatever you want with the rest of the tokens. Just use strTok.nextToken() to get the rest of the token, split by colons.
                    System.out.println(***** + " " + ***** + " " + ******);
                       }
                   }
               // This catches all the exceptions. You can go more in detail here if you want.
               } catch(Exception e){
                   e.printStackTrace();
               }
    
        }
        
    and here is the txt file
    Code:
    aplayer:0:500:0
    1337:40:1000:3
    --- merged: Jan 23, 2011 1:21 PM ---
    ps that is for reading it not writing hav not tryed writing yet
     
  6. Offline

    McAndze

    I think it's here. You didn't define <name>:
    StringTokenizer strTok = new StringTokenizer(name, ":")
     
  7. Offline

    1337

    i have a string called name that can be read from any method
    String name; then when a player logs in it will make name = to there username so it cant be that thank you for you help btw also with the writing one how would i make it do it only if there is not already a line with there username in it?

    anyone?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 6, 2016
  8. Offline

    McAndze

    Why did you put <name> in the StringTokener then?
    As far as I can see, you want to split the line you are reading from the file. In that case you have to make it <curLine> instead.
     
  9. Offline

    1337

    so i dont need name?
     
  10. Offline

    McAndze

    Well from what I can see you are trying to split the String you read from the line, and in that case you wanna put <curLine> into the StringTokenizer instead.
     
  11. Offline

    1337

    like this?
    Code:
    try {
                   BufferedReader br = new BufferedReader(new FileReader("******"));
                   String curLine;
    
                   while ((curLine = br.readLine()) != null){
                    StringTokenizer strTok = new StringTokenizer(curline, ":");
                    if (strTok.nextToken().equals(curline)){
                       String ****  = strTok.nextToken();
                    **** = Integer.parseInt(****);
                    String Nextlvl = strTok.nextToken();
                   **** = Integer.parseInt(*****);
                    String ****** = strTok.nextToken();
                    ***** = Integer.parseInt(******);
                    // Here you can do whatever you want with the rest of the tokens. Just use strTok.nextToken() to get the rest of the token, split by colons.
                    System.out.println(***** + " " + ***** + " " + ******);
                       }
                   }
               // This catches all the exceptions. You can go more in detail here if you want.
               } catch(Exception e){
                   e.printStackTrace();
               }
    
     
  12. Offline

    McAndze

    At the if statement, you're testing the first token of <curLine> to itself. What exactly do you want to test it to?
     
  13. Offline

    1337

    do you mean whats at the start of the line? then i want the playername
     
  14. Offline

    Tazzernator

    Read up on my pkugin MCDocs. I also host the source on my git hub. I handl files exactly as described in the OP

    See my signature
     
Thread Status:
Not open for further replies.

Share This Page