简体   繁体   English

读取文件并将每一行保存到变量中

[英]Reading a file and saving each line into a variable

For a project of mine, I'm trying to read a file of Integers and save each line into a file.对于我的一个项目,我正在尝试读取一个整数文件并将每一行保存到一个文件中。 Each of the files I'm reading have a different amount of lines.我正在阅读的每个文件都有不同数量的行。

The file would look like该文件看起来像

17 17
72 72
61 61
11 11
63 63
95 95
100 100

Is there a way I can use a loop and save the value in a different variable for each line?有没有办法可以使用循环并将每行的值保存在不同的变量中?

Assuming you just want to take a line and copy it into another file, here you go: Reading the file as long as there are lines and just change System.out.println(line);假设您只想取一行并将其复制到另一个文件中,这里是 go: 只要有行就读取文件,只需更改System.out.println(line); (line 16) to create and write to the file (and there just add an incrementing index (like index++ executed at the end of "while") to the filename and pass "line" to the write function). (第 16 行)创建和写入文件(并且只需在文件名中添加一个递增索引(如在“while”末尾执行的index++ )并将“line”传递给 write 函数)。

The only thing that is important: close the BufferedReader and the FileWriter!!唯一重要的是:关闭 BufferedReader 和 FileWriter!!

To prevent getting deleted by a bot the code from the links:为了防止被机器人删除,链接中的代码:

阅读 创作与写作

Create a Collection to capture your values.创建一个集合来捕捉您的价值观。 We can use a List so we maintain the input order.我们可以使用 List 来维护输入顺序。

List<Integer> list = new ArrayList<>();

Create a mechanism for reading from the file.创建从文件中读取的机制。 A common option is a Scanner.一个常见的选项是扫描仪。 The Scanner is Autoclosable so we'll put it in a try-with-resources block so we don't have to close it ourselves. Scanner 是可自动关闭的,所以我们将它放在一个 try-with-resources 块中,这样我们就不必自己关闭它。

try (Scanner scanner = new Scanner(new File(fileName))) {
  ... do stuff ...
}

While the Scanner has values, read a value and add it to the List.当 Scanner 有值时,读取一个值并将其添加到列表中。

while (scanner.hasNextInt()) {
  list.add(scanner.nextInt());
}

In sum...总共...

List<Integer> list = new ArrayList<>();
try (Scanner scanner = new Scanner(yourFileName)) {
  while (scanner.hasNextInt()) {
    list.add(scanner.nextInt());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java读取文本文件并将每一行另存为新的字符串数组 - Java Reading Text File And Saving Each Line As a New String Array 逐行读取文件,然后修改一行并仅保存该行 - Reading a file line by line then modifying a line and saving only that line Java读取数据文件中的文本,并将其保存为变量 - Java reading text in a data file, and saving it as a variable 我坚持创建一个循环来一次读取 6 行文件,将每一行保存到一个变量中,以便稍后用于计算 - I am stuck on creating a loop to read 6 lines of a file at a time, saving each line to a variable to be used later for calculations 从txt文件中每隔一行读取一次并保存到数组中 - Reading every other line from a txt file and saving into an array 读取多行文本文件,每行中包含不同的数据 - Reading a Multi-line text file with different data in each line 从文件中读取一行,并从每一行操纵单个单词? - Reading line from a file, and manipulating individual words from each line? 从我的桌面读取文件并从中打印每一行 - Reading a file from my desktop and printing each line from it Java:仅从每行读取第一个单词即可读取.csv文件 - Java: Reading .csv file with only the first word from each line 读取文本文件的每一行并将其与用户输入进行比较 - Reading each line of a text file and compare it to user input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM