在缓冲区读取器Java中添加行计数器
我的任务是使用缓冲读取器读取文件, 加上计算我的文件中的行数。 这样做之后,我应该拆分并解析它。有人能帮忙吗 在这附近的某个地方,我想应该有一个代码读取文件中的行数 最后{在缓冲区读取器Java中添加行计数器,java,bufferedreader,Java,Bufferedreader,我的任务是使用缓冲读取器读取文件, 加上计算我的文件中的行数。 这样做之后,我应该拆分并解析它。有人能帮忙吗 在这附近的某个地方,我想应该有一个代码读取文件中的行数 最后{ try { if (br != null) br.close(); if (fr != null) fr.close(); }
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
if (count != null);
这里应该是分裂的部分
String[] temp = count.split("/t");
在拆分之后,应该有一个for循环并使用数组,应该对其进行解析
}
}
}
您可以使用
BufferedReader
中的lines
方法获取行流
,拆分每行,并将其收集到列表中
List<String[]> lines;
try(BufferedReader reader = new BufferedReader(new FileReader("path"))) {
lines = reader.lines()
.map(line -> line.split("\t"))
.collect(Collectors.toList());
}
列表行;
try(BufferedReader=newbufferedreader(newfilereader(“路径”)){
lines=reader.lines()
.map(行->行分割(“\t”))
.collect(Collectors.toList());
}
阅读您的代码非常困难。请下次格式化
我创建了一个名为“random_file.txt”的文件,其内容如下:
这是一号线
这是2号线
这是另一行
还有一个
还有一个
现在,我们可以对文件执行您需要的所有操作。我们可以计算行数、打印每行或解析行数。由于您没有确切指定要解析的内容,因此我编写了一个示例方法,该方法只计算文件中的特定单词。应使用RegularExpressions(regex)进行解析。这里有一个很好的链接:
好的,JDK中已经有了LineNumberReader:你需要更好地表达你的问题。很难理解你到底想做什么。请更具体一些。另外,请尝试向你的代码示例中添加注释,就像在你的实际代码中一样,使用//符号。这样,应该在它们是可见的。而且,我不确定“计数”到底是多少应该是。它看起来像是文件路径,但最后看起来像是内容。但同时从你的问题来看,它听起来应该有一个行数。我很感谢你的帮助,实际上,我是堆栈溢出新手,请原谅我的任何错误,我会记住,并尝试下一次正确发布o问题,希望有帮助。
List<String[]> lines;
try(BufferedReader reader = new BufferedReader(new FileReader("path"))) {
lines = reader.lines()
.map(line -> line.split("\t"))
.collect(Collectors.toList());
}
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class FileParser
{
private String filepath;
public FileParser(String inputFilePath)
{
this.filepath = inputFilePath;
}
/**
* Counts the number of lines.
*
* @return Number of lines.
*
* @throws FileNotFoundException If the file doesn't exist.
* @throws IOException When an IO error occures.
*/
public int countLines() throws FileNotFoundException, IOException
{
File file = new File(filepath);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
int counter = 0;
while (br.readLine() != null)
{
counter++;
}
return counter;
}
/**
* Splits the lines of the file and returns a list.
* Each element of the list represents one line.
* Note that the line seperator is excluded.
*
* @throws FileNotFoundException If the file doesn't exist.
* @throws IOException When an IO error occures.
*/
public List<String> splitLines1() throws FileNotFoundException, IOException
{
File file = new File(filepath);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
ArrayList<String> outputList = new ArrayList<>();
while ((line = br.readLine()) != null)
{
outputList.add(line);
}
if (br != null) br.close();
return outputList;
}
/**
* Splits the lines of the file and returns a String.
* Same as before, but now we have the line seperators included.
*
* @throws FileNotFoundException If the file doesn't exist.
* @throws IOException When an IO error occures.
*/
public String splitLines2() throws FileNotFoundException, IOException
{
File file = new File(filepath);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line;
StringBuilder builder = new StringBuilder();
while ((line = br.readLine()) != null)
{
// we append every line to the builder
// note that we get one line seperator more than
// necessary (the one in the end)
builder.append(line + System.lineSeparator());
}
if (br != null) br.close();
return builder.toString();
}
/**
* An example method for parsing. In this method we count the
* number of times a word occures in given file.
*
* @param word The word we are looking for.
*
* @return Count the word occurencies.
*
* @throws FileNotFoundException If the file doesn't exist.
* @throws IOException When an IO error occures.
*/
public int countOccurencies(String word)
throws FileNotFoundException, IOException
{
List<String> fileLines = splitLines1(); // get the list, where each element represents one line
int counter = 0;
for (String line : fileLines)
{
// we split each line into words by splitting
// at the spaces
String[] words = line.split(" ");
for (int i = 0; i < words.length; i++)
{
if (words[i].equals(word)) counter++;
}
}
return counter;
}
/**
* Testing the methods.
*/
public static void main(String[] args) throws Exception
{
// Location of my file is in the project folder
String filePath = System.getProperty("user.dir") + File.separator
+ "random_file.txt";
FileParser fp = new FileParser(filePath);
System.out.println("The file has " + fp.countLines() + " lines."
+ System.lineSeparator());
System.out.println("We print a list holding each line as an element:");
System.out.println(fp.splitLines1()
.toString() + System.lineSeparator());
System.out
.println("Now we print the file contents as a single string:");
System.out.println(fp.splitLines2());
System.out
.println("Now we count the occurencies of the word \"line\":");
System.out.println(fp.countOccurencies("line"));
}
}
The file has 5 lines.
We print a list holding each line as an element:
[This is line 1 ..., And this is line number 2, This is another line ..., And one more, And another one]
Now we print the file contents as a single string:
This is line 1 ...
And this is line number 2
This is another line ...
And one more
And another one
Now we count the occurencies of the word "line":
3