java - 需要从文件中获取一行到队列中,而不是整个文件文本
问题描述
我无法一次正确地将一行文本从文件中获取到队列中,而没有将整个文件放入队列中。例如,我只想编写一个程序,将 Java 源文件作为参数读取并生成文件中所有标识符的索引。对于每个标识符,打印它出现的所有行。为简单起见,我们将考虑每个字符串仅由字母、数字和下划线组成的标识符。声明一个 Scanner 用于从源文件中读取并调用in.useDelimiter("[^A-Za-z0-9_]+")
Then 每个调用 next 返回一个标识符。
public class Main { to get added to the queue but instead the whole file text is put into the queue instead of a line at a time. Sorry if my question is unclear
// Write a program that reads a Java source file as an argument and produces an index of all
// identifiers in the file. For each identifier, print all lines in which it occurs. For simplicity,
// we will consider each string consisting only of letters, numbers, and underscores an identifier.
// Declare a Scanner in for reading from the source file and call in.useDelimiter("[^A-Za-z0-9_]+").
// Then each call to next returns an identifier.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class E_15 {
public static void main(String[] args) throws FileNotFoundException
{
// get scanner input from file
Scanner fileInput = new Scanner(new File ("C:/Users/ramir/IdeaProjects/PA_7/src/Main.java"));
Queue<String> test = new LinkedList<String>();
ArrayList<String> phrase = new ArrayList<String>();
/*
List<String> result = new ArrayList<String>();
Scanner s = new Scanner(is);
s.useDelimiter(delimiter);
*/
// Iterates till end of file
while (fileInput.hasNextLine())
{
// Here is the issue. Data will end up
// containing the whole file instead of only that line
String data = fileInput.nextLine();
Scanner in = new Scanner(data);
in.useDelimiter("[^A-Za-z0-9_]+");
// I believe around here or before is the issue that I'm having.
// It adds all the file instead of only that line
// Also trying to figure out how to display each line that it's displayed on
// What the first one should look like for example
// 0: public occurs in:
// public class Main {
// public static void main(String[] args) {
//System.out.println(data);
test.add(data);
while(in.hasNext())
{
// Getting each phrase/word into ArrayList
String token = in.next();
phrase.add(token);
}
in.close();
}
int index = 0;
// This part works fine
for(String num : phrase)
{
// printing the key
System.out.println(index + ": " + num + " occurs in:");
// printing the values
// This to print out what
for(String line : test)
{
System.out.println(line);
}
System.out.println();
++index;
}
}
}
// Just java class get file front
// This is fine
public class Main {
public static void main(String[] args) {
int a_1 = 100;
System.out.println(a_1);``
}
}
我希望它只显示System.out.println(a_1)
,因为它在See This上
。我也无法在所有出现的行中打印它。
解决方案
import java.io.*;
import java.util.Scanner;
public class ReadLineByLineExample2
{
public static void main(String args[])
{
try
{
//the file to be opened for reading
FileInputStream fis=new FileInputStream("Demo.txt");
Scanner sc=new Scanner(fis); //file to be scanned
//returns true if there is another line to read
while(sc.hasNextLine())
{
System.out.println(sc.nextLine()); //returns the line that was skipped
}
sc.close(); //closes the scanner
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
尝试研究上面的代码。我希望它会有所帮助。否则,您可能需要打开此链接以获取更多详细信息。
推荐阅读
- bash - 长路径触发错误 bash 输出,如果循环错误
- android - 如何指定播放服务位置
- php - 我创建了一个表单,我可以在其中上传图像,但我有问题。在显示它时。我已经写了下面的代码
- java - 没有为第二个功能文件打开新的浏览器窗口
- oracle - 如何在 Azure API 管理中使用 Oracle SSO SAML 进行身份验证
- jmeter - 如何在 jmeter 中关闭 JDBC 连接
- asp.net-mvc - 如何修复[缺少根元素]?
- javascript - 定期触发 React 效果
- testing - Gatling 脚本引发错误“类型不匹配”
- python - 计算不同值范围的均方误差