L.redir
L.redir

Reputation: 65

Store contents of a file into a string variable java

I'm unsure how to store the contents of a file into a string after its contents are read.

I've already managed to read in the contents of the .txt file and print its contents but I am unsure how to store those contents as they are into a String variable in java.

example of .txt contents: RANDOMSTRING

Code snip I have that reads in the contents of the text file but doesn't store it into variable "key":

{
    FileReader file = new FileReader("C:/Users/John/Documents/key.txt");
    BufferedReader reader = new BufferedReader(file);

    String key = "";
    String line = reader.readLine();

    while (line != null) {
        key += line;
        line = reader.readLine();
    }

    System.out.println(key); //this prints contents of .txt file
}

//  String key = " "; //should be able to reference the key and message here
//  String message = "THIS IS A SECRET MESSAGE!"; // another string that is stored

//encrypt is a method call that uses the stored strings of "message" and "key"
String encryptedMsg = encrypt(message, key);

Upvotes: 2

Views: 18318

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

It stores the data fine into the key variable (if your File reading code is working correctly -- and this is easy to test). No your real problem is one of variable scope. The key variable is declared in the curly brace-enclosed block at the top of your code block, and is not visible where you're trying to use it. Try declaring the key variable before the block of code that you've shown, or use it as a field in your class.

// here is some block of code:
{
    FileReader file = new FileReader("C:/Users/John/Documents/key.txt");
    BufferedReader reader = new BufferedReader(file);

    // **** key is declared here in this block of code
    String key = "";
    String line = reader.readLine();

    while (line != null) {
        key += line;
        line = reader.readLine();
    }
    System.out.println(key); // so key works
}

// but here, the key variable is not visible as it is **out of scope**
String encryptedMsg = encrypt(message, key); 

Solution:

// declare key *** here***
String key = "";


{
    FileReader file = new FileReader("C:/Users/John/Documents/key.txt");
    BufferedReader reader = new BufferedReader(file);

    // don't declare it here
    // String key = "";
    String line = reader.readLine();

    while (line != null) {
        key += line;
        line = reader.readLine();
    }
    System.out.println(key); // so key works
}

// but here, the key variable is in fact visible as it is now **within scope**
String encryptedMsg = encrypt(message, key); 

Your other problem is that your code formatting is terrible, and this is partly responsible for the problem above. If you format your code well including using consistent indentation style so that the code appears uniform and consistent, you would see exactly what block the key variable has been declared in, and it becomes obvious that it will not be visible where you need it. I usually avoid using tabs for indenting (forum software often doesn't play well with tabs) and indent each code block 4 spaces. Code that is in the same block should be indented the same.

Upvotes: 3

Related Questions