View Single Post
Skilled contributor
3Sep2011,20:13  
ManzZup's Avatar
herez the corrected version of code with some addition, more suitable error handling and example of implementing

Code:
import java.io.*; 
class Test{
static BufferedWriter out = null;
	public static void main(String... args){
		try{
		addUsernameToFile("ManZzup");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try{
				out.close();
			}catch(Exception e){}
		}
	}

public static void addUsernameToFile(String Name) throws IOException {
  out = new BufferedWriter(new FileWriter("TrustedPlayers.txt", true));
  out.newLine();
  out.write(Name);
 }

 }
as for the errors, there were several,
you had a try block opened but never closed, i guess you havent put the bottom part of the code
you havent caught the BufferedWriter.close() throwing exceptio in the finally block
it is always good to have finally after all
and the best pratice would be to simply throw the error out of the method body and handle them all at one place
and BufferedReader.write method has a simple 'w'

hope this code will help