已解决20170607 - 事实证明这是一个IDE安装错误。当我使用较旧的Eclipse和Java 6时,系统恢复到了。然后我安装了最新的Eclipse和Java 8,并保存了没有错误的文件。
#这里发生了什么?谷歌以及诸如此类的许多网站似乎都没有启发我。
我使用Eclipse 4,Windows 7 SP 1和Java 8(121 64位)
我看过Access is denied java.io.FileNotFoundException
它说问题是由文件权限引起的。我可以毫无问题地阅读txt文件,只要他们没有.txt作为扩展名,我就可以写入新文件或现有文件。这在我的问题的v1中解释。即使这样,我给了管理员和当前登录的用户(我),对导演的完全权限和仍然抛出的异常。我可以毫无问题地加载文件(参见标题为##### LOADING FILES #####的问题部分)
我试图找出如何保存文件。我创建的代码使用扩展名.exe,.mp3,.foo,.doc,.exe。
当我使用.txt时。和IOExcepton,抛出访问被拒绝。链接到图片:http://design.paulyeatman.net/wp-content/uploads/2017/04/IOException-savefile-20170602.png
这是程序代码。我已经包含了所有内容(所以尚未编写的方法等,我感兴趣的是" 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;
}
文件2只有常量,它们正确地输入方法。
加载文件我创建了一个名为existing.txt的文件。这是通过缓冲读取器读入程序,完全没有问题。致电
println("Should be x1 Element, so the ref 0 element is: " + workingData.get(0));
输出为"应为x1元素,因此ref 0元素为:这已从现有的文本文件中加载,该文件名为" existing.txt" "
我真正想问的是什么我的代码是否有效且Windows存在Eclipse保存文本文件的问题(似乎与Access is denied java.io.FileNotFoundException不同),我还需要了解Java的这个功能吗?我的代码缺少什么?
/ \基于我的测试,Windows必须遇到Eclipse试图保存文件的问题,所以最后是权限错误。