Jump to content

Writing to file

Go to solution Solved by Tech N Gamer,

Okay, after a ton of yawns and being drowsy, I've figured it out, I think. I think it's because Eclipse made it say

System.clearProperties("user.home")

instead of 

System.getProperties("user.home")

. Here is the new code if anyone want's to see it:

	private void checker() {
	        String dirPath = System.getProperty("user.home") + "\\AppData\\Roaming\\ClassMaker";
	        String filePath = System.getProperty("user.home") + "\\AppData\\Roaming\\ClassMaker\\settings.ini";
	        System.out.println(dirPath + "\n" + filePath);
	        if (System.getProperty("os.name").equals("Windows 10")) {
	            if (new File(dirPath).exists()) {
	                if (new File(filePath).exists()) {
	                    try {
	                        Scanner fileOpener = new Scanner(new File(filePath));
	                        String line = fileOpener.nextLine();
	                        String[] varibles = line.split("=");
	                        if (varibles[1].equals("true")) {
	                            alreadyRan = true;
	                        }
	                    } catch (FileNotFoundException e1) {
	                        JOptionPane.showMessageDialog(null,
	                                "An error has occured and the program failed to read a file."
	                                        + "\nPlease send this error to the dev so he can figure out a fix.\n"
	                                        + e1.toString(),
	                                "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	                        System.exit(ABORT);
	                    }
	                } else {
	                    createNewFile(filePath);
	                }
	            } else {
	                if (!new File(dirPath).mkdir()) {
	                    JOptionPane.showMessageDialog(null,
	                            "An error has occured and the program failed to create a directory."
	                                    + " The most likly cause of this is the program doesn't have proper" +
	                                    "read and write permission.",
	                            "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	                    System.exit(ABORT);
	                } else {
	                    createNewFile(filePath);
	                }
	            }
	        }
	    }
	    private void createNewFile(String path) {
	        File file = new File(path);
	        try {
	            file.createNewFile();
	        } catch (IOException e) {
	            JOptionPane.showMessageDialog(null,
	                    "An error has occured and the program failed to create a file."
	                            + "\nPlease send this error to the dev so he can figure out a fix.\n" +
	                            e.toString(),
	                    "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	            System.exit(ABORT);
	        }
	        try {
	            PrintWriter printToFile = new PrintWriter(file.getAbsolutePath());
	            printToFile.print("alreadyRan=true");
	            printToFile.close();
	        } catch (IOException e) {
	            JOptionPane.showMessageDialog(null,
	                    "An error has occured and the program failed to read a file."
	                            + "\nPlease send this error to the dev so he can figure out a fix.\n"
	                            + e.toString(),
	                    "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	            System.exit(ABORT);
	        }
	    }

I am trying to write to a file that I created yet it's not doing anything, not even creating a file. It acts like the file was created but never does. Here is my code that deals with it:

 

	private void checker() {
	        if (System.getProperty("os.name") == "Windows 10" || System.getProperty("os.name") == "Windows 7") {
	            if (new File(System.getProperty("os.name") + "\\AppData\\Roaming\\ClassMaker").exists()) {
	                if (new File(System.clearProperty("user.home") + 
	                        "\\AppData\\Roaming\\ClassMaker\\settings.ini").exists()) {
	                    try {
	                        Scanner fileOpener = new Scanner(new File(
	                                System.clearProperty("user.home") + 
	                                "\\AppData\\Roaming\\ClassMaker\\settings.ini"));
	                        String line = fileOpener.nextLine();
	                        String[] varibles = line.split("=");
	                        if (varibles[1].equals("true")) {
	                            alreadyRan = true;
	                        }
	                    } catch (FileNotFoundException e1) {
	                        JOptionPane.showMessageDialog(null,
	                                "An error has occured and the program failed to read a file."
	                                        + "\nPlease send this error to the dev so he can figure out a fix.\n"
	                                        + e1.toString(),
	                                "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	                        System.exit(ABORT);
	                    }
	                } else {
	                    createNewFile();
	                }
	            } else {
	                if (!new File(System.clearProperty("user.home") + "\\AppData\\Roaming\\ClassMaker").mkdir()) {
	                    JOptionPane.showMessageDialog(null,
	                            "An error has occured and the program failed to create a directory."
	                                    + " The most likly cause of this is the program doesn't have proper" +
	                                    "read and write permission.",
	                            "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	                    System.exit(ABORT);
	                } else {
	                    createNewFile();
	                }
	            }
	        }
	    }
	    private void createNewFile() {
	        File file = new File(System.clearProperty("user.home") + 
	                "\\AppData\\Roaming\\ClassMaker\\settings.ini");
	        try {
	            file.createNewFile();
	        } catch (IOException e) {
	            JOptionPane.showMessageDialog(null,
	                    "An error has occured and the program failed to create a file."
	                            + "\nPlease send this error to the dev so he can figure out a fix.\n" +
	                            e.toString(),
	                    "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	            System.exit(ABORT);
	        }
	        try {
	            PrintWriter printToFile = new PrintWriter(file);
	            printToFile.print("alreadyRan=true");
	            printToFile.close();
	        } catch (IOException e) {
	            JOptionPane.showMessageDialog(null,
	                    "An error has occured and the program failed to read a file."
	                            + "\nPlease send this error to the dev so he can figure out a fix.\n"
	                            + e.toString(),
	                    "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	            System.exit(ABORT);
	        }
	    }

 

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

Update: I've found some issues, they've been fixed, but I still cannot write to a file at all. Getting java.io.IOException.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

Would I be correct to assume it's the catch (IOException e) outputting the error? 

I'm not fluent in Java, but i do know VB.NET. It seems to me, you should have something along the lines of e.message.toString() instead if e.toString to get actual info out of the error. At least that's how I remember it being in VB.

 

BTW, your error message says it failed to read a file but it seems to me it should say it failed to write...

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Naeaes said:

Would I be correct to assume it's the catch (IOException e) outputting the error? 

I'm not fluent in Java, but i do know VB.NET. It seems to me, you should have something along the lines of e.message.toString() instead if e.toString to get actual info out of the error. At least that's how I remember it being in VB.

 

BTW, your error message says it failed to read a file but it seems to me it should say it failed to write...

Their is no such thing as e.message.toString() in Java. Every class in java has a .toString() method to it and it's the default for all exceptions besides e.getMessage(). Also thanks for the problem with what I typed. I am very tired and should prob be sleeping.

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

Okay, after a ton of yawns and being drowsy, I've figured it out, I think. I think it's because Eclipse made it say

System.clearProperties("user.home")

instead of 

System.getProperties("user.home")

. Here is the new code if anyone want's to see it:

	private void checker() {
	        String dirPath = System.getProperty("user.home") + "\\AppData\\Roaming\\ClassMaker";
	        String filePath = System.getProperty("user.home") + "\\AppData\\Roaming\\ClassMaker\\settings.ini";
	        System.out.println(dirPath + "\n" + filePath);
	        if (System.getProperty("os.name").equals("Windows 10")) {
	            if (new File(dirPath).exists()) {
	                if (new File(filePath).exists()) {
	                    try {
	                        Scanner fileOpener = new Scanner(new File(filePath));
	                        String line = fileOpener.nextLine();
	                        String[] varibles = line.split("=");
	                        if (varibles[1].equals("true")) {
	                            alreadyRan = true;
	                        }
	                    } catch (FileNotFoundException e1) {
	                        JOptionPane.showMessageDialog(null,
	                                "An error has occured and the program failed to read a file."
	                                        + "\nPlease send this error to the dev so he can figure out a fix.\n"
	                                        + e1.toString(),
	                                "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	                        System.exit(ABORT);
	                    }
	                } else {
	                    createNewFile(filePath);
	                }
	            } else {
	                if (!new File(dirPath).mkdir()) {
	                    JOptionPane.showMessageDialog(null,
	                            "An error has occured and the program failed to create a directory."
	                                    + " The most likly cause of this is the program doesn't have proper" +
	                                    "read and write permission.",
	                            "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	                    System.exit(ABORT);
	                } else {
	                    createNewFile(filePath);
	                }
	            }
	        }
	    }
	    private void createNewFile(String path) {
	        File file = new File(path);
	        try {
	            file.createNewFile();
	        } catch (IOException e) {
	            JOptionPane.showMessageDialog(null,
	                    "An error has occured and the program failed to create a file."
	                            + "\nPlease send this error to the dev so he can figure out a fix.\n" +
	                            e.toString(),
	                    "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	            System.exit(ABORT);
	        }
	        try {
	            PrintWriter printToFile = new PrintWriter(file.getAbsolutePath());
	            printToFile.print("alreadyRan=true");
	            printToFile.close();
	        } catch (IOException e) {
	            JOptionPane.showMessageDialog(null,
	                    "An error has occured and the program failed to read a file."
	                            + "\nPlease send this error to the dev so he can figure out a fix.\n"
	                            + e.toString(),
	                    "An Error has occured!", JOptionPane.ERROR_MESSAGE);
	            System.exit(ABORT);
	        }
	    }

Brah, do you even Java?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×