Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 read and write files
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
wonsleved




PostPosted: Sun Dec 03, 2006 3:44 pm   Post subject: read and write files

Hey, im having a problem with writing and reading data to a file. my code seems to work fine when im running it in eclipse, but when i try and run the html file it cant read or write anything. plz help.
Sponsor
Sponsor
Sponsor
sponsor
cool dude




PostPosted: Sun Dec 03, 2006 4:34 pm   Post subject: (No subject)

can you post your code and the html file please. Are you sure your saving the html file in the same folder as your program? what is the error message?
wonsleved




PostPosted: Sun Dec 03, 2006 9:05 pm   Post subject: (No subject)

yeah its in the same folder. there is no error at all when im running it in eclipse, and when i run it in html it doesnt show an error, it just doesnt work, if i try writing to a file there will be nothing inside of it. someone told me that applets just dont have the capability of writing to a file in html but im reluctant ro believe that.
//sample writing code

try
{
PrintWriter pWriter = new PrintWriter(new FileWriter("save1.txt"));
pWriter.println("" + randomInteger);
pWriter.flush();
pWriter.close();

}
catch(Exception e)
{
}
//sample reading code

BufferedReader fReader;
String buf = "";
try
{

fReader = new BufferedReader(new FileReader("save1.txt"));
buf = fReader.readLine();
randomInteger = (Integer.parseInt(buf));

}
fReader.close();
//HTML code
<html >
<applet code=FileReader.class width="800" height="600" >
</applet>
</html>
gsquare567




PostPosted: Sun Dec 03, 2006 9:17 pm   Post subject: (No subject)

a) ur forgetting a second catch statement, and b) looks like all that does is rewrite the first line in the file :S
wonsleved




PostPosted: Sun Dec 03, 2006 9:24 pm   Post subject: (No subject)

yeah sorry i was kinda rushing when i wrote that, and its just samples of code of what it will do, im just trying to show what i used in my program.
//sample writing code
try
{
PrintWriter pWriter = new PrintWriter(new FileWriter("save1.txt"));
pWriter.println("" + randomInteger);
pWriter.flush();
pWriter.close();

}
catch(Exception e)
{
}
//sample reading code

BufferedReader fReader;
String buf = "";
try
{

fReader = new BufferedReader(new FileReader("save1.txt"));
buf = fReader.readLine();
randomInteger = (Integer.parseInt(buf));
fReader.close();

}
catch(Exception e)
{
}
wtd




PostPosted: Mon Dec 04, 2006 12:38 am   Post subject: (No subject)

Please use code tags in the future.
wtd




PostPosted: Mon Dec 04, 2006 12:40 am   Post subject: (No subject)

gsquare567 wrote:
a) ur forgetting a second catch statement


A try block may be used without an accompanying "catch." Whether or not there's any point is a whole other matter.

I should note that the use of catch is extremely ill-advised. It is bad to silently dismiss exceptions.
cool dude




PostPosted: Mon Dec 04, 2006 3:08 pm   Post subject: (No subject)

wtd wrote:
gsquare567 wrote:
a) ur forgetting a second catch statement


A try block may be used without an accompanying "catch." Whether or not there's any point is a whole other matter.

I should note that the use of catch is extremely ill-advised. It is bad to silently dismiss exceptions.


I second that! There is no point of catching an error if your not going to do anything about it. Your better off using throws Exception which may i add is just as bad.
Sponsor
Sponsor
Sponsor
sponsor
wonsleved




PostPosted: Mon Dec 04, 2006 4:05 pm   Post subject: (No subject)

okkk so what should i write instead
wonsleved




PostPosted: Mon Dec 04, 2006 4:22 pm   Post subject: (No subject)

ok i output the error onto the scree, (thanks for tellin me to do that), and it said java.security.AccessControlException. whats that mean and how do i get rid of it?
wtd




PostPosted: Mon Dec 04, 2006 4:27 pm   Post subject: (No subject)

Use the documentation. It is your friend.

http://java.sun.com/j2se/1.4.2/docs/api/java/security/AccessControlException.html
wonsleved




PostPosted: Mon Dec 04, 2006 4:40 pm   Post subject: (No subject)

i looked over that sight, but it only tells you what it is, how would u use it, or modefy it, or something to help get permission
Aziz




PostPosted: Tue Dec 05, 2006 9:23 am   Post subject: (No subject)

Quote:
This exception is thrown by the AccessController to indicate that a requested access (to a critical system resource such as the file system or the network) is denied.

The reason to deny access can vary. For example, the requested permission might be of an incorrect type, contain an invalid value, or request access that is not allowed according to the security policy. Such information should be given whenever possible at the time the exception is thrown.


That means you can't access the file. You don't have permission to. Like trying to modify something in the school's computers. Why you don't have permission beats me, though.
wonsleved




PostPosted: Tue Dec 05, 2006 3:34 pm   Post subject: (No subject)

so if i cant get the permission to read and write files, then is there any way i could step around that?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 14 Posts ]
Jump to:   


Style:  
Search: