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.
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(); } }
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.
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
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.
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.
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.
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(); }
At the if statement, you're testing the first token of <curLine> to itself. What exactly do you want to test it to?
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