Johnny
Johnny

Reputation: 103

FileWriter not writing into directory properly

I have a simple program that reads in commands and performs them. Rightnow I have this code for inserting certain text into a text file:

Example command:

INSERT "John Smith" INTO college.student

My main method:

 else if(command.substring(0,6).equalsIgnoreCase("INSERT")){
        String string = command.substring(7, command.indexOf("INTO") - 1);
        String DBNameTBName = command.substring(command.indexOf("INTO") + 5);
        String tableName = DBNameTBName.substring(DBNameTBName.indexOf(".") + 1);
        String DBName = DBNameTBName.substring(0, DBNameTBName.indexOf("."));

        if(DBCommands.insert(string, DBName, tableName)){
            statfileWriter.println("Inserted " + string  + " into table " + tableName + " in " + DBName);
            statfileWriter.println("(" + command + ")");
            statfileWriter.flush();
        }
        else{
            errfileWriter.println("Error: Could not insert " + string + " into table " + tableName + " in " + DBName);
            errfileWriter.println("(" + command + ")");
            errfileWriter.flush();
        }       

And the insert method it calls:

public static boolean insert(String string, String DBName, String tableName){
    try{
        string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); //removes quotes

        File tableToWriteTo = new File(DBName  + "/" + tableName + ".txt");
        if (!tableToWriteTo.exists()){
            return false;
        }

        PrintWriter writer = new PrintWriter(new FileWriter
                  (tableToWriteTo, true));
        writer.println(string);
        writer.close();
        return true;
    }
    catch(Exception e){

        return false;
    }

}

I am getting very weird behavior with my insert method. It returns true as it always prints to my status log and not error log. I know the method to create the .txt file is working perfectly, I have tested it many times and the student.txt file is always there. With my insert command, if I change the File = new File line to this:

File tableToWriteTo = new File(tableName + ".txt");

Then it unsurprisingly creates a .txt file called "student" with my example command, but not in the "DBName" folder. If I change it to this:

File tableToWriteTo = new File(DBName  + "/" + tableName);

Then it creates a file called "student" with no type (as in, Windows asks what I want to open it with) but puts in the string I want to insert into it. I should note that if there are multiple INSERT commands then it writes all the strings as I would like it to.

I have tried declaring PrintWriter and File in my main method and passing them in, but that doesn't work either.

How can I get it to write into students.txt in the directory college?

EDIT: Oh my goodness, I'm the stupidest person on Earth. I didn't look at the full commands list I received for this assignment and I forgot there was a delete command and they were BOTH working. I would delete this question but I'll leave this up in case anyone in the future wants to see an example of FileWriter.

Upvotes: 1

Views: 206

Answers (1)

anacron
anacron

Reputation: 6721

I changed the if condition in the insert method. The file is not expected to exist. So ideally the condition should not be negated. I used the following code and it is working for me.

public class InsertToWriteTo {

    public static void main(String[] args) {
        boolean ret = insert("\"hello\"", "college", "student");
        System.out.println(ret);
    }

    public static boolean insert(String string, String DBName, String tableName) {
        try {
            string = string.substring(string.indexOf('"') + 1, string.lastIndexOf('"')); // removes quotes

            File tableToWriteTo = new File(DBName + "/" + tableName + ".txt");
            if (tableToWriteTo.exists()) { // changed condition
                System.out.println("File exists");
                return false;
            }

            PrintWriter writer = new PrintWriter(new FileWriter(tableToWriteTo, true));
            writer.println(string);
            writer.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}

Hope this helps!

Upvotes: 1

Related Questions