All pastes #2120984 Raw Edit

Buffered Read/Write (Noob Way)

public text v1 · immutable
#2120984 ·published 2012-02-23 08:01 UTC
rendered paste body
public class ListMaker extends Main{
	static ArrayList<Playerloc> tempList2;
	private static Playerloc p;
     public static List<Playerloc> readDataFromFile() {
    	 List<Playerloc> listofusers = new ArrayList<Playerloc>();
         FileInputStream fstream = null; 
    	 try
     {

         fstream = new FileInputStream("src/list.txt");
         BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
         String strLine = "";
         String[] tokens = strLine.split(", ");
         //Read file line by line
         while ((strLine = br.readLine()) != null)   {
           // Copy the content into the array
           tokens = strLine.split(", ");
           listofusers.add(new Playerloc(tokens[0],tokens[1],tokens[2],tokens[3]));
         }
     }
     catch (IOException e) {
       e.printStackTrace();
     }
     finally {
         try { fstream.close(); tempList2 = (ArrayList<Playerloc>) listofusers;}
         catch ( Exception ignore ) {}
     }
     return listofusers;
     }
     

public void insertPlayerlocRow(String name, String x, String y, String z) {
    List<Playerloc> list = readDataFromFile();
    Playerloc pLoc = new Playerloc(name, x, y, z);
    // Add row to ArrayList
   list.add(pLoc);
    // Update file
    updateFile("src/list.txt", list);
}


public static void updateFile(String filename, List<Playerloc> userDataList) {
   BufferedWriter bufferedWriter = null;
    try {
        bufferedWriter = new BufferedWriter(new FileWriter(filename));
    } catch (IOException ex) {
      ex.printStackTrace();
    }
   Playerloc pLoc;
   String row;
   for(int i=0; i<userDataList.size(); i++) {
       pLoc = userDataList.get(i);
       row = pLoc.name + ", " + pLoc.Xx + ", " + pLoc.Yy + ", " + pLoc.Zz + "/n";
        try {
            bufferedWriter.write(row);
            bufferedWriter.newLine();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    //Close the BufferedWriter
    try {
          if (bufferedWriter != null) {
              bufferedWriter.flush();
              bufferedWriter.close();
          }
    } catch (IOException ex) {
          ex.printStackTrace();
    }
}

public static void print(){
	if (tempList2.contains(p)){
   	 tempList = tempList2;
    }
}
}