Java - 解析文本文件

发布于 2024-11-04 05:10:28 字数 543 浏览 5 评论 0原文

我有一个这种格式的输入文本文件:

<target1> : <dep1> <dep2> ...
<target2> : <dep1> <dep2> ...
...

以及一个采用两个参数的方法

function(target, dep);

,我需要进行此解析以使用每个目标和 dep 调用我的方法,例如:

function(target1, dep1);
function(target1, dep2);
function(target1, ...);
function(target2, dep1);
function(target2, dep2);
function(target2, ...);

调用 function(target, dep) 在文本文件的每一行?我尝试使用扫描仪和 string.split 进行操作,但没有成功。我很困惑。

谢谢。

I have an input text file in this format:

<target1> : <dep1> <dep2> ...
<target2> : <dep1> <dep2> ...
...

And a method that takes two parameters

function(target, dep);

I need to get this parsing to call my method with each target and dep eg:

function(target1, dep1);
function(target1, dep2);
function(target1, ...);
function(target2, dep1);
function(target2, dep2);
function(target2, ...);

What would be the most efficient way to call function(target,dep) on each line of a text file? I tried fooling around with the scanner and string.split but was unsuccessful. I'm stumped.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱贱哒 2024-11-11 05:10:28
  • 将行读入 String myLine
  • : 上的 myLine 拆分为 String[] array1
  • 拆分 array1[1] on ' ' into String[] array2
  • 迭代 array2 并调用 function(array1[0], array2[我])

所以...

FileReader input = new FileReader("myFile");
BufferedReader bufRead = new BufferedReader(input);
String myLine = null;

while ( (myLine = bufRead.readLine()) != null)
{    
    String[] array1 = myLine.split(":");
    // check to make sure you have valid data
    String[] array2 = array1[1].split(" ");
    for (int i = 0; i < array2.length; i++)
        function(array1[0], array2[i]);
}
  • Read line into String myLine
  • split myLine on : into String[] array1
  • split array1[1] on ' ' into String[] array2
  • Iterate through array2 and call function(array1[0], array2[i])

So ...

FileReader input = new FileReader("myFile");
BufferedReader bufRead = new BufferedReader(input);
String myLine = null;

while ( (myLine = bufRead.readLine()) != null)
{    
    String[] array1 = myLine.split(":");
    // check to make sure you have valid data
    String[] array2 = array1[1].split(" ");
    for (int i = 0; i < array2.length; i++)
        function(array1[0], array2[i]);
}
无边思念无边月 2024-11-11 05:10:28

首先,您必须从文件中读取行,然后在此分割读取行之后,因此您的代码应如下所示:

FileInputStream fstream = new FileInputStream("your file name");
// or using Scaner
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
  // split string and call your function
}

The firstly you have to read line from file and after this split read line, so your code should be like:

FileInputStream fstream = new FileInputStream("your file name");
// or using Scaner
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)   {
  // split string and call your function
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文