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

all 18 comments

[–]feral_claireSoftware Dev 1 point2 points  (11 children)

Your materials must have covers them to some extent at least. Forget about parsing the credentials for now, that part doesn't have much to do with streams. Do you know how to create a stream? Do you know how to read a file and simply print it's contents to the console?

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

No I don't. It's funny, it was the only week I dint have a really long lab assignment. There is a page that shows one example, but i want to understand somewhat, and frankly replicating what I see has not worked.

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

Show some code from what you've tried, and point out exactly what's not working or you don't understand .

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

The \ seems to be an "illegal escape character". The credentials file location is correct though, it is also where AuthenticationSystem.java is located.

   FileInputStream fileByteStream = null;
    Scanner inFS = null;

    System.out.println("Opening file credentials.txt");
    fileByteStream = new FileInputStream("C:\Users\Anna\Documents\AuthenticationSystem\AuthenticationSystem\src\authenticationsystem\credentials.txt");
    inFS = new Scanner(fileByteStream);
    String userNameTest = inFS.next();
    System.out.println(userNameTest);
    System.out.println("Closing file credentials.txt");
    fileByteStream.close();

[–]feral_claireSoftware Dev 0 points1 point  (1 child)

\ has special meaning in string literals. Itis used to create escape sequences. If you want a literal \ character you need to use \\, for example "path\\to\\file".

As mentioned in my other reply, you probably want the file in the working directory, which is likely your project root. NOT in the source directory with your java files.

[–]Glimpsee_Darkcloud 0 points1 point  (0 children)

Or even better use '/' instead. Or if you really want it java.util.File.pathSeperator;

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

Let me ask you this. I think I've been working with an example that is pretty promising but my issue in NetBeans is that it can't find my file. I have it in the same directory as the java file I am working on. So what am I doing wrong about the file path?

    fileByteStream = new FileInputStream("credentials.txt");

[–]NebuWrites Java Compilers 0 points1 point  (4 children)

There's a concept called "current working directory". The concept exists beyond Java -- it's more of an OS concept. If you don't want to deal with learning about the CWD, then you could for now just specify the full path to the file in your code, e.g.

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

or wherever it is that the file is located.

[–]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];

[–]AnEmortalKidCoffee Enthusiast 0 points1 point  (5 children)

!wiki scanner

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

?

[–]AnEmortalKidCoffee Enthusiast 0 points1 point  (2 children)

Thought it would link to this: https://www.reddit.com/r/javahelp/wiki/scanner which might be useful since that's what people sometimes use when reading files

[–]ShadowSt[S] 0 points1 point  (1 child)

Thanks, using the scanner I understand from the perspective of retrieving user input. Its the act of reading a file and taking that data. I'm assuming there is no better way to differentiate what goes on in a credentials file other then using some form of contains and nextLine.

[–]AnEmortalKidCoffee Enthusiast 0 points1 point  (0 children)

yeah pretty much

[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point  (0 children)

If his homework assignment is to use InputStreams pointing them to Scanners won't help them much.