java 将文件的内容存储到字符串变量java中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32729253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Store contents of a file into a string variable java
提问by L.redir
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.
我已经设法读入.txt
文件的内容并打印其内容,但我不确定如何将这些内容按原样存储到 java 中的 String 变量中。
example of .txt
contents: RANDOMSTRING
.txt
内容示例:RANDOMSTRING
Code snip I have that reads in the contents of the text file but doesn't store it into variable "key":
我读入文本文件的内容但不将其存储到变量“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);
回答by Hovercraft Full Of Eels
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 beforethe block of code that you've shown, or use it as a field in your class.
它将数据很好地存储到关键变量中(如果您的文件读取代码工作正常——这很容易测试)。不,您真正的问题是可变范围之一。key 变量在代码块顶部的大括号括起来的块中声明,并且在您尝试使用它的地方不可见。尝试在您显示的代码块之前声明键变量,或将其用作类中的字段。
// 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.
你的另一个问题是你的代码格式很糟糕,这是造成上述问题的部分原因。如果您很好地格式化您的代码,包括使用一致的缩进样式,以便代码看起来统一和一致,您将准确地看到关键变量已在哪个块中声明,并且很明显它在您需要的地方不可见。我通常避免使用制表符进行缩进(论坛软件通常不能很好地使用制表符)并将每个代码块缩进 4 个空格。同一块中的代码应该缩进相同。