it give an access denied<java.io.filepermission Item.txt write> error

any suggestion for a simple id generator?

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.lang.*;
public class Shoes extends Applet implements ActionListener, ItemListener
{
	Font SansSerif = new Font ("SansSerif", Font.BOLD, 12);
	Button create = new Button("Create a Record");
	Button edit = new Button("Edit a Record");
	Button delete = new Button("Delete a Record");
	Button display = new Button("Display a Record");
	Button save = new Button("Save");
	TextField size = new TextField(3);
	TextField quantity = new TextField(3);
	TextField pid = new TextField(3);
	CheckboxGroup design = new CheckboxGroup();
	Checkbox high = new Checkbox("High Cut", design , true);
	Checkbox low = new Checkbox("Low Cut", design , false);
	Choice brand = new Choice();
	String [] brands = {"Adidas", "DC" , "Dunlop" , "Nike" , "Pony", "Vans"};
	Choice color = new Choice();
	String [] colorlist = {"Red","Magenta","Blue","Cyan","Green","Yellow","White","Gray","Black"};
	InputStreamReader ir=new InputStreamReader(System.in);
	BufferedReader br;
	FileOutputStream fos;
	PrintStream ps;
	FileReader frs;
	String s;
	String id;

	public void init()
	{
		add(create);
		addNewLine();
		add(edit);
		addNewLine();
		add(delete);
		addNewLine();
		add(display);
		create.addActionListener(this);
	}

	public boolean checkFile(String filename)
	{
		File myFile=new File(filename);
		boolean b=myFile.exists();
		return b;
	}

	public String generate_id()
		{
			String thisLine;
			String[] fullText = new String[200];
			String[] lastLine;
			int counter=0,nid=0,l=0,z=0;

			boolean b=checkFile("Item.txt");

			if(b==false)
			{
				id="01";
			}
			else
			{
				try
				{
					frs=new FileReader("Item.txt");
				 	br=new BufferedReader(frs);
					while((thisLine=br.readLine()) != null)
					{
						counter=counter+1;
						fullText[counter] = thisLine;
					}
					frs.close();
	 				lastLine=fullText[counter].split(",");
	 				nid=Integer.parseInt(lastLine[0]);
	 				nid=nid+1;
	 				l=(String.valueOf(nid)).length();
	 				z=2-l;
	 				for(int i=1;i<=z;i++)
	 				{
	 					id=id+"0";
	 				}
	 				id=id+String.valueOf(nid);
	 				id=id.substring(4);
	 				System.out.println(counter);
	 				System.out.println(nid);
	 				System.out.println(l);
	 				System.out.println(z);
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null,"Could not generate the Item ID...!");
				}
			}
			return id;
	}

	public void arecord()
		{


			for (int i = 0; i < brands.length; i++)
			    {
					brand.add(brands[i]);
				}
			for (int i = 0; i < colorlist.length; i++)
			    {
					color.add(colorlist[i]);
				}
			setFont(SansSerif);
			create.setVisible(false);
			edit.setVisible(false);
			delete.setVisible(false);
			display.setVisible(false);
			addNewLine();
			add(new Label("Size (4-12): "));
			add(size);
			addNewLine();
			add(new Label("Design: "));
			add(high);
			add(low);
			addNewLine();
			add(new Label("Brand: "));
			add(brand);
			addNewLine();
			add(new Label("Color: "));
			add(color);
			color.setBackground (Color.red);
			color.addItemListener (this);
			addNewLine();
			add(new Label("Price: "));
			addNewLine();
			add(new Label("Quantity: "));
			add(quantity);
			addNewLine();
			add(new Label("Product Number: "));
			add(pid);
			pid.setEnabled(false);
			addNewLine();
			add(save);
			validate();
			save.addActionListener(this);
		}
	public void actionPerformed(ActionEvent e)
		{
			if (e.getSource() == create)
			{
				id=generate_id();
				arecord();
				pid.setText(id);
			}
			if (e.getSource() == save)
			{
				isave();
			}
		}
	public void isave()
	{
		try
			{
				br=new BufferedReader(ir);
				System.out.println(id);
				s = String.valueOf(size.getText());
				fos=new FileOutputStream("Item.txt",true);
				ps=new PrintStream(fos);
				ps.println(id);
				fos.close();
				JOptionPane.showMessageDialog(null,"Successfully added one product...");
			}
		catch(Exception ex)
			{
				JOptionPane.showMessageDialog(null,"Error in accepting item specifications...");
				System.out.println(ex.getMessage());
 			}
	}

	public void itemStateChanged(ItemEvent e)
	{
		    int Selection;
		    Selection = color.getSelectedIndex();
		    if (Selection == 0) {
		      color.setBackground (Color.red);
		    } else if (Selection == 1) {
		      color.setBackground (Color.magenta);
		    } else if (Selection == 2) {
		      color.setBackground (Color.blue);
		    } else if (Selection == 3) {
		      color.setBackground (Color.cyan);
		    } else if (Selection == 4) {
		      color.setBackground (Color.green);
		    } else if (Selection == 5) {
		      color.setBackground (Color.yellow);
		    } else if (Selection == 6) {
		      color.setBackground (Color.white);
		    } else if (Selection == 7) {
		      color.setBackground (Color.gray);
		    } else if (Selection == 8) {
		      color.setBackground (Color.black);
		  	}
    }

	public void addHorizontalLine(Color c)
		{
			Canvas line = new Canvas( );
			line.setSize(10000,1);
			line.setBackground(c);
			add(line);
		}

	public void addNewLine( )
		{
		    addHorizontalLine(getBackground( ));
		}
}

if you want to write to a file, you need to use BufferedWriter and FileWriter

im using textpad , how dow i use the policy?

I put the permission policy to read and write for all files using the policytool but it still gives me the access denied error..

is it a read-only file you're trying to write to?

no , just a normal .txt

it give an access denied<java.io.filepermission Item.txt write> error

are you not admin trying to use a file on the c drive. it looks like you dont have permission to write there.

In your program, the location of output file is the same location where your Java file is. You are trying to append to the file "Item.txt." Does the file exist? Also, what is the read/write/execution permission of the file? If the file exist but does not have write permission, you need to change the file permission first.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.