0

#

SOLVED 20170607 -It turns out this was an IDE installation error. I system restored to when I was using an older Eclipse and Java 6. I then installed the latest Eclipse and Java 8 and the file saved with no errors.

#

What's going on here? Google as well as many sites such as this do not seem to enlighten me.

I using Eclipse 4, Windows 7 SP 1 and Java 8 (121 64 bit)

I have looked at Access is denied java.io.FileNotFoundException

It says the issue is due to file permissions. I can read txt files with no trouble and I can write to new or existing files as long as they do not have .txt as their extension. This is explained in the v1 of my question. Even so, I gave admin and the currently logged on user (me), full permissions to the director and the exception's still thrown. I can load files with no problems (see the question section titled #####LOADING FILES#####)

I'm trying to work out how to save files. The code I have created works with the extensions .exe, .mp3, .foo, .doc, .exe.

When I use .txt. and IOExcepton, access denied is thrown. link to image: http://design.paulyeatman.net/wp-content/uploads/2017/04/IOException-savefile-20170602.png

Here's the program code. I've included everything (so methods not yet written etc, the one I'm interesting in is "private void saveFile(String fileName, String sample)"

/* This program is to teach myself to save a file using as an example, a program that maintains a list of DVD's along with release date and whether or not has been watched (so a DVD name and and 2 associated variables I'll probably track with an ArrayList).
 * Where a DVD has been watched, user should be able to modify existing entry to this effect.
 * The list of DVD's, release date and watch state are all saved into a persistent file for future editing.
 * 
 * 
 * might be insightful http://alvinalexander.com/java/java-file-utilities-open-read-write-copy-files as has some pre coded classes
 */

import acm.program.*;
import java.util.*; 
import java.io.*;
import java.nio.*;  // not used
import java.nio.charset.Charset; // not used

public class saveData extends ConsoleProgram implements saveDataConstants    {

    public void run() {
        // guts of program here
        chooseFile();
        addToDataList();
//      saveFile(fileName, text);s
}


/* this asks the user if the have a file or if they want to create a new file */

    private void chooseFile() {
        println ("Choose your file type");
        println("1. You want to create a new file.");
        println("2. You want to open an existing file.");
        int fileChoice = readInt ("");
        println("You chose: " + fileChoice);
        if (fileChoice == 1) {
            createNewFile();
        }
        if (fileChoice == 2) {
            existingFile();
        }

    }

/* an existing file is read into a bufferedReader */

    private void existingFile() {
        println("existingFile method launched");
    }

/* a new file is created by assigning it a file name which is held in memory until the save option is launched */

    private void createNewFile() {
    fileName = readLine ("Enter in you filename: ");
    fileName = fileName + FILE_EXTENSION;

}

/* this add to the file in active memory */

private void addToDataList() {
    println("This is the addToDataList method");
    println("We want arrays to handle Movie Title, Release Date, Watched (or not)");
//      println("A sentinel of Q is planned to be used to enter the saveFile method");

    //the next line throws an IOException.  Why, file path?  something else???  Nah, the .txt extension. .doc, .mp3. foo. exe. .png all used and work with no IOEx

    saveFile(fileName, sample);
//      saveFile();
}

/* this has test string for the file */

private String testString(String text) {
    String sample = "This should text should wind up in the text file the program should create";
    return (sample);
}

/* this saves the new data to the file (hopefully, just appends, will it overwrite?  who cares?) 
 * 
 * The fileName is remembered as it should be.  Here we assume it will save to some default location.
 * 
 * The test string is correctly gathered from the testString(String text) method.
 * */


private void saveFile(String fileName, String sample) // adapted from http://alvinalexander.com/java/java-file-save-write-text-binary-data
//  throws IOException
    {
    String textToSave = testString(sample);

    Charset charset = Charset.forName("US-ASCII");

    println("This is the saveFile method.  Presumably it saves the file.");
    println("");
    println("the filename is remembered as: " + fileName);
    println("");
    println("the text to save is: " + textToSave);
    println("");
    File fileNameSaved = new File (fileName);

    try{
        BufferedWriter file = new BufferedWriter(new FileWriter (fileNameSaved));  // true will add to the file and not overwrite

//      BufferedWriter out = new BufferedWriter(new FileWriter (file, true));  // true will add to the file and not overwrite
        // #############################
        // Note, the BufferedWriter line saves the file name as "file". and the txt is put into it.
        // When I remove the quotes, an IOException results despite file = the correct name.  Since tracked error down to extension on .txt

        // see if this helps: https://docs.oracle.com/javase/tutorial/essential/io/file.html

        file.write(textToSave);
        file.close();
    }
    catch (IOException ex) {
        ex.printStackTrace(); // from https://www.tutorialspoint.com/java/io/bufferedreader_readline.htm
        System.err.println();
    }
    println("The file has saved if the program gets this far."); // even if IOEx thrown, this displays.
}

/* Private variables follow */
String fileName;
String textToSave;
String sample;
}

File 2 just has the constants and they are feed into the methods correctly.

LOADING FILES

I created a file called existing.txt. That is read into the program by a buffered reader with no problem at all. Calling

println("Should be x1 Element, so the ref 0 element is: " + workingData.get(0));

Gives the output as "Should be x1 Element, so the ref 0 element is: This has been loaded from an existing text file called "existing.txt" "

What I'm really asking

Should my code work and Windows has an issue with Eclipse saving text files (seems to not be the case as not the same as Access is denied java.io.FileNotFoundException ), is there something else I need to learn about this function of Java or is my code lacking something?

/\ Based on my testing, Windows must have had an issue with Eclipse trying to save files, so yes in the end permission errors.

  • The file must already exist, and have file permissions set such that you can't update it from that process. – Elliott Frisch Jun 02 '17 at 02:58
  • 1
    Possible duplicate of [Access is denied java.io.FileNotFoundException](https://stackoverflow.com/questions/19561386/access-is-denied-java-io-filenotfoundexception) – Asif Raza Jun 02 '17 at 03:01
  • Thanks for the reply. Is this something specific to txt files? Eg. if I type in "existing" a file called existing.foo is created. If I make a file and give it the foo extension, then the code above over writes the file with the new existing.foo and includes the sample text. – Paul Yeatman Jun 02 '17 at 03:05
  • Also Possible duplicate of [java.io.FileNotFoundException: (Access is denied)](https://stackoverflow.com/questions/4281143/java-io-filenotfoundexception-access-is-denied) – Asif Raza Jun 02 '17 at 08:41
  • I've tested the above code on a snapshot of my existing VM running Eclipse 3.2.1 and Java 6 Update 2 and the 8.txt file is created and saved successfully. So, in conclusion, the code works with Eclipse 3.2.1 and Java 6 Update 2 The code does not work with: Eclipse 4.6.3, Windows 7 SP 1 and Java 8 (121 64 bit). – Paul Yeatman Jun 04 '17 at 22:55

0 Answers0