java中读取文本文件
在这里,我试图读取一个每行仅包含整数的文本文件。例如:
1
2
3
1
我编写了以下代码来读取文本文件。代码如下所示。
package fileread;
import java.io.*;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
try{
FileInputStream fstream=new FileInputStream("C:/Users/kiran/Desktop/text.txt");
DataInputStream in=new DataInputStream (fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
in.close();
}
catch(Exception e){
System.err.println(e);
}
}
}
现在我只想检索那些重复的整数并将其显示给用户。 在这种情况下我想显示“1”。
我怎样才能在Java中实现这个?
Here I'm trying to read a text file which contains only integers.in every line.For eg:
1
2
3
1
I wrote the following code to read the text file. Code as shown below.
package fileread;
import java.io.*;
public class Main {
public static void main(String[] args) {
// TODO code application logic here
try{
FileInputStream fstream=new FileInputStream("C:/Users/kiran/Desktop/text.txt");
DataInputStream in=new DataInputStream (fstream);
BufferedReader br=new BufferedReader(new InputStreamReader(in));
String str;
while((str=br.readLine())!=null){
System.out.println(str);
}
in.close();
}
catch(Exception e){
System.err.println(e);
}
}
}
Now I want to retrieve only those integers which repeated and display it to the user.
In this case i want to display "1".
How can I implement this in Java??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
注意:确保您的输入每行都没有尾随空格。另请注意,当列表变长时,此实现对内存不是特别友好。
Note: Make sure your input doesn't have trailing whitespace on each line. Also, note that when the list gets long, this implementation is not particularly memory friendly.
您需要读取数组中的值,然后查找该数组中的重复条目。
You need to read the value in an array and then find duplicate entries in that array.
完整读取文件,将行保存到您选择的数据结构中(map(key=line, value=count),如果只是整数,则为数组),枚举数据结构并打印这些值大于 1 的数据结构(如果值代表计数)。
或即时:读取文件,将条目添加到集合/列表/数组中(如果未包含在集合/列表/数组中),否则打印出行。
Read the file entirely, save the lines to a data structure of your choice (map(key=line, value=count), array if only integers), enumerate the data structure and print these whose value is greater than 1 (if the value represents the count).
or on the fly: read the file, add entry to a set/list/array, if not contained in set/list/array, else print out line.
那么,您可以使用具有 10 个槽的数组,该数组映射到 0 到 9 之间的数字。对于每一行,您检查该数字是什么并相应地增加数组中的值。它会是这样的:
Well, you can use an array with 10 slots which maps to a number from 0 to 9. For every line, you check what that number is and increment the value in the array accordingly. It would be something like this:
除了给定的答案之外,请确保将字符串转换为整数(数字)并捕获异常,以防文件中的任何内容不是数字。在这种情况下,我认为您可以安全地忽略异常,因为它不相关,但检查输入数据是一个很好的做法。
In addition to the given answers, make sure you are converting your strings to integers (numbers) and catch the exception in case whatever comes from the file isn't a number. In this case I think you can safely ignore the exception because it's not relevant, but it's a good practice to check your input data.
首先,我将定义 1 个列表和 1 组整数,如下所示:
然后,我将检查重复项并将它们添加到相应的列表中,如下所示:
First off, I would define 1 list and 1 set of Integers, as below:
And then, I would check for duplicates and add 'em to respective lists, as below:
像这样的东西
Something like this
我会使用两组方法;
或者,您也可以使用单个 HashMap,其中 HashMap.Key 作为 Integer,HashMap.Value 作为该 Integer 的计数。
然后,如果您稍后需要重构以查找单次出现的所有实例,那么您可以轻松地做到这一点。
请小心 NumberFormatExceptions,根据您的情况,您可以在循环内部或循环外部处理它们。
I would use the two set approach;
Alternately, you could also use a single HashMap with the HashMap.Key as the Integer and the HashMap.Value as count for that Integer.
Then if you later need to refactor to find all instances with a single occurrence then you can easily do that.
Be careful of NumberFormatExceptions, and depending on your situation, you can either handle them inside the loop, or outside the loop.