質問

I use a filewriter to write an arraylist of objects to a file, as strings, in a kind- of CSV like format. Anyway I had problems getting it to work, but now I get a FileNotFound exception and it says the file created is read-only in the exception. The file is created, as I checked, but apparently cannot be written to. However I actually want to overwrite the contents anyway, but get this error.

        ///Like a CSV file. 
        try{
            FileWriter writer_file = new FileWriter("PeopleDetailsFile");

            String filestring = ""; ///initializes filestring, which is written to the file.
            for(PeopleDetails person : peopledetails_file){
                String person_detail_string = "";
                person_detail_string = person.name + "," + person.number;
                filestring = filestring + person_detail_string + ";";

            }
            writer_file.write(filestring);
            writer_file.close();
        }
            catch (IOException e) {
                Log.e("ERROR", e.toString());

        }finally{
            ///Hopefully won't get an error here.
            Intent switch_menu = new Intent(this, MenuList.class);
            startActivity(switch_menu);
        }
役に立ちましたか?

解決 2

Call this method with fileName and the List as Argument.

public void fileWrite(String fileName,ArrayList<PeopleDetails> person){
        File           f ;
        FileWriter     fw = null;
        BufferedWriter bw = null;
        try {
            f  = new File(fileName);
            fw = new FileWriter(f.getAbsoluteFile());
            bw = new BufferedWriter(fw);
            for(PeopleDetails i:person){
                bw.write(i.name+","+i.number+";");
                bw.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                if(bw!=null)
                    bw.close();
                if(fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }   
    }

他のヒント

Try to use the PrintWriter. I am providing you some brief guidelines below, but modifications will be required. Moreover, note that I am saving the name and the number in different lines, so when you read the details you should use the same approach.

 public void writeToFile(String fileName, ArrayList<PeopleDetails> peopledetails) {
        try {
            /*
             * Create a FileWriter object that handles the low-level details of
             * writing
             */
            FileWriter theFile = new FileWriter(fileName);

            /* Create a PrintWriter object to wrap around the FileWriter object */
            /* This allows the use of high-level methods like println */
            PrintWriter fileOut = new PrintWriter(theFile);

            /* Print some lines to the file using the println method */
            for (int i = 0; i < peopledetails.size(); i++) {
                fileOut.println(peopledetails.get(i).getName());
                fileOut.println(peopledetails.get(i).getNumber());
            }
            /* Close the file so that it is no longer accessible to the program */
            fileOut.close();
        }

        /* Handle the exception thrown by the FileWriter methods */
        catch (IOException e) {
            System.out.println("Problem writing to the file");
        }
    } /* End of method writeToFile */

You could try deleting the file every time you wan't to rewrite it. try{

        File file = new File("PeopleDetailsFile");

        if(file.delete()){
            System.out.println(file.getName() + " is deleted!");
        }else{
            System.out.println("Delete operation is failed.");
        }

    }catch(Exception e){

        e.printStackTrace();

    }

Alternatively: you could make the FileWriter to where it appends to the current document:

 FileWriter writer_file = new FileWriter("PeopleDetailsFile", true); //true means append = true;

Use the other constructor of FileWriter that takes append flag: FileWriter(File file, boolean append)

You might be trying to write to the 'root' folder. You should be following Android Context openFileOutput instead of FileWriter to write to the application's directory (instead of the root).

Per the documentation:

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top