java 读取文本文件并将文件中的字符串分配给变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17688760/
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
reading text file and assigning strings from file to variables
提问by gucci
I have a few problems with my code. I want to be able to read input from a text file and take the strings from each line or between spaces and assign them to variables that I will pass to an object.
我的代码有一些问题。我希望能够从文本文件中读取输入并从每一行或空格之间获取字符串并将它们分配给我将传递给对象的变量。
My first problem is that my program misreads one of my lines and omits the first letter from a variable, and the second problem is I don't know how to make my program read two strings on the same line and assign them to different variables.
我的第一个问题是我的程序误读了我的一行并省略了变量的第一个字母,第二个问题是我不知道如何让我的程序读取同一行上的两个字符串并将它们分配给不同的变量。
System.out.println("Input the file name of the text file you want to open:(remember .txt)");
keyboard.nextLine();
String filename=keyboard.nextLine();
FileReader freader=new FileReader(filename);
BufferedReader inputFile=new BufferedReader(freader);
courseName=inputFile.readLine();
while (inputFile.read()!= -1) {
fName=inputFile.readLine();
lName=inputFile.readLine();
officeNumber=inputFile.readLine();
}
System.out.println(fName);
Instructor inst=new Instructor(fName,lName,officeNumber);
System.out.println(inst);
inputFile.close();
}
I am not very good at using filereader and have tried to use the scanner keyboard method, but it lead me to even more errors :(
我不太擅长使用文件阅读器,并尝试使用扫描仪键盘方法,但这导致我犯了更多错误:(
Output: Input from a file (F) or keyboard (K): F Input the file name of the text file you want to open: (remember .txt) test.txt un bun un bun won won's office number is null
输出:从文件(F)或键盘(K)输入:F 输入要打开的文本文件的文件名:(记住.txt) test.txt un bun un bun won won's office number is null
text file: professor messor bun bun won won
文本文件:mssor bun bun won won 教授
采纳答案by nachokk
You have to read one line with readLine()
then you say that you want to split with whitespace
.
你必须阅读一行,readLine()
然后你说你想用 分割whitespace
。
So you have to do something like this
所以你必须做这样的事情
String line=null;
List<Course> courses = new ArrayList<>();
while ((line = inputFile.readLine())!= null) {
String[] arrayLine= line.split("\s+"); // here you are splitting with whitespace
courseName = arrayLine[0]
lName=arrayLine[1];
officeNumber=arrayLine[2];
list.add(new Course(courseName,lName,officeNumber));
// you sure want do something with this create an object for example
}
// in some part br.close();
Here you have an example How to read a File
这里有一个示例如何读取文件
回答by nagendra547
This is another solution using StringTokenizer
这是另一个使用 StringTokenizer 的解决方案
String line=null;
List<Course> courses = new ArrayList<>();
while ((line = inputFile.readLine())!= null) {
StringTokenizer sToken = new StringTokenizer(input, " ");
list.add(new Course(sToken.nextToken(),sToken.nextToken(),sToken.nextToken()));
}
回答by Vyassa Baratham
When you call read()
in the condition of the while
loop, the "cursor" of the BufferedReader advances one character. You don't want to do this to check if the stream can be read, you want to use the ready()
method.
当您调用循环read()
的条件时while
,BufferedReader 的“光标”前进一个字符。您不想这样做来检查是否可以读取流,而是想使用该ready()
方法。