I am writing some data to a file using FileWriter and BufferedWriter. I want that while writing in the file if somebody else wants to open the file in MS Excel, it should open only in read only mode

Here is a snippet of my FileWriter and BufferedWriter

while(true)
                    {
                        String str=Sinput.readLine();
                        String[] arr = str.split(";");
                        System.out.println(str);
                        if(arr[0].compareTo("$$")==0)
                        {
                            File file = new File(arr[1]+".txt");
                            FileWriter fstream = new FileWriter(file, true);
                            BufferedWriter out = new BufferedWriter(fstream);
                            dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
                            Date date = new Date();
                            out.write("\n"+dateFormat.format(date));
                            for(int i=1;i<6;i++)
                            {
                                out.write("\t"+arr[i]);
                            }
                            out.close();
                            }
                        }

Currently, When I try to open the file in MS Excel it opens in normal mode and due to which my program is not able to write in file and gives an excepton. What should I do?

Try FileLock, but no guarantees, of course.

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.