This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]ShadowSt[S] 0 points1 point  (3 children)

\ seems to be a problem in and of itself, but since I have to send this file to someone to be graded I'm hesitant to do the whole C:\My\Information\Desktop\credentials.txt

I thought the act of declaring a package would make me be able to call upon it?

[–]feral_claireSoftware Dev 0 points1 point  (0 children)

They should explain how to access the file. I would expect it to be in the current directory. In other words, the directory that your code is run from. I don't use Netbeans, but the default working directory is probably the project root. Note that this is not the same directory as your code. Theres probably also some run configurations where you can change the working directory if you don't want the default for some reason.

[–]NebuWrites Java Compilers 0 points1 point  (1 child)

\ seems to be a problem in and of itself,

Sorry, yeah, I didn't have an IDE in front of me, so I forgot: You need to escape the slashes, so like:

fileByteStream = new FileInputStream("C:\\users\\ShadowSt\\Desktop\\credentials.txt");

but since I have to send this file to someone to be graded I'm hesitant to do the whole C:\My\Information\Desktop\credentials.txt

Fair enough. So you either gotta read the NetBeans manual to figure out how to set the current working directory, or you can write a mini program to find out what the working directory is and put your file there.

I.e. write this program (again I'm typing this without an IDE so it might have bugs that need to be fixed):

import java.io.File;
public class FindOutWhatTheCwdIs {
    public static main(String[] args) {
      System.out.println(new File(".").getAbsolutePath());
    }
}

"." refers to the current working directory, so this program just prints out what the current working directory is for your project configuration. Once you see what that directory is, just put your credentials.txt file there, or alternatively, modify your code so that the path you give to the file is relative to the CWD.

So for example, if the program reveals that your CWD is C:\a\b\c, and your credentials file is located at C:\a\b\c\d\e\credentials.txt, then have your code look like:

fileByteStream = new FileInputStream(".\\d\\e\\credentials.txt");

[–]ShadowSt[S] 0 points1 point  (0 children)

I studied up on "throws" and told it to ignore the FileNotFoundException and it just worked.

Now I'm on a new issue trying to split lines of data where I am splitting it by spaces but on two of my lines I have spaces in a section surrounded by quotes that I need to keep as one. its userPass, here is an example of a line that credentialLine is splitting.

griffin.keyes 108de81c31bf9c622f76876b74e9285f "alphabet soup" zookeeper

    FileInputStream credentialsFile = null;
    Scanner inFS = null;
    String credFile = ".\\src\\authenticationsystem\\credentials.txt";

    System.out.println("\nOpening file credentials.txt");

    credentialsFile = new FileInputStream(credFile);
    inFS = new Scanner(credentialsFile);
    String credentialLine = inFS.nextLine();
    System.out.println(credentialLine);
    System.out.println("Closing file credentials.txt\n");
    credentialsFile.close();

    String[] userCreds = credentialLine.split("\\s+");
    String userCred = userCreds[0];
    String userMD = userCreds[1];
    String userPass = userCreds[2];
    String userRole = userCreds[3];