Hi there! got some problems.
I've developing a multiplayer game there's a class called userServer, that use a file
to handle all incoming information.
but I can't write to the file for some reasons. here's the code:
/**
* @(#)userServer.java
*
*
* @author
* @version 1.00 2011/2/16
*/
import java.awt.*;
import java.io.*;
import java.lang.Integer;
import java.util.ArrayList;
public class userServer {
public ArrayList<user> Users = new ArrayList<user>();
public int cnt;
game3 parent;
public void load(){
try{
File file = new File("userlist.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
DataInputStream reader = new DataInputStream(bis);
String args[];
int x, y;
while(reader.available()!=0){
args = reader.readLine().split(" ");
x = new Integer(args[3]).intValue();
y = new Integer(args[4]).intValue();
Users.add(new user(this, args[0], args[1], args[2], x, y));
}
reader.close();
bis.close();
fis.close();
}catch(FileNotFoundException e){
}catch(IOException e){
}
}
public void printBack(){
try{
FileWriter txt = new FileWriter("userlist.txt");
BufferedWriter writer = new BufferedWriter(txt);
for (int i=0; i<Users.size(); i++){
writer.write(Users.get(i).getstr());
}
writer.close();
}catch(IOException e){
}
}
public void Update(){
printBack();
Users = new ArrayList<user>();
load();
}
public userServer(game3 par) {
parent = par;
load();
}
public boolean connect(String User, String password){
boolean bool=false;
String u, p;
int xx, yy;
for (int i=0; i<Users.size(); i++){
if (Users.get(i).username == User && Users.get(i).password == password){
bool=true;
xx = Users.get(i).x;
yy = Users.get(i).y;
u = Users.get(i).username;
p = Users.get(i).password;
Users.set(i, new user(this, u, p, "connected", xx, yy));
Update();
break;
}
}
return bool;
}
String n, p, s;
public void SetXY(String User, int x, int y){
for (int i=0; i<Users.size(); i++){
if (Users.get(i).username == User && Users.get(i).status == "connected"){
n = Users.get(i).username;
p = Users.get(i).password;
s = Users.get(i).status;
Users.set(i, new user(this, n, p, s, x, y));
Update();
break;
}
}
}
public boolean Registry(String Username, String password){
boolean nameAlreadyExist = false;
boolean isRegistred = false;
for (int i=0; i<Users.size(); i++){
if (Users.get(i).username == Username){
nameAlreadyExist = true;
break;
}
}
if (!nameAlreadyExist){
Users.add(new user(this, Username, password, "connected", 35, 20));
Update();
isRegistred = true;
}
return isRegistred;
}
public void print(Graphics g){
for (int i=0; i<Users.size(); i++){
if (Users.get(i).status == "connected"){
Users.get(i).print(g);
}
}
}
public void diconnect(String username){
for (int i=0; i<cnt;i++){
if (Users.get(i).username == username){
user use = Users.get(i);
use.status = "disconnected";
Users.set(i, use);
break;
}
}
}
}
when I running it, this is comming up:
--------------------Configuration: game3 - JDK version 1.6.0_20 <Default> - <Default>--------------------
Exception in thread "AWT-EventQueue-1" java.security.AccessControlException: access denied (java.io.FilePermission userlist.txt write)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:323)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkWrite(SecurityManager.java:962)
at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
at java.io.FileWriter.<init>(FileWriter.java:46)
at userServer.printBack(userServer.java:44)
at userServer.Update(userServer.java:56)
at userServer.Registry(userServer.java:111)
at game3.mouseDown(game3.java:166)
at java.awt.Component.handleEvent(Component.java:6492)
at java.awt.Component.postEvent(Component.java:4922)
at java.awt.Component.dispatchEventImpl(Component.java:4646)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
I can't understand it, why haven't I access to the file? is it any wrong with my code?
:?: