Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是否单独检测文本文件的第一行?_Java - Fatal编程技术网

Java 是否单独检测文本文件的第一行?

Java 是否单独检测文本文件的第一行?,java,Java,我正在设计一个程序,将文本文件加载到不同的媒体文件类(媒体>音频>mp3,媒体>视频>Avi等)。 现在,我的文本文件的第一行是总共有多少个文件,如中所示 3 exmaple.mp3,fawg,gseges test.gif,wfwa,rgeg ayylmao.avi,awf,gesg 这就是我的文本文件中的内容,我想先分别获取第一行,然后循环遍历其余的文件。 现在我明白了,我可以简单地通过使用一个随着循环而增长的int来计算文件的数量,但我希望在文件中也能清

我正在设计一个程序,将文本文件加载到不同的媒体文件类(媒体>音频>mp3,媒体>视频>Avi等)。 现在,我的文本文件的第一行是总共有多少个文件,如中所示

    3
    exmaple.mp3,fawg,gseges
    test.gif,wfwa,rgeg 
    ayylmao.avi,awf,gesg
这就是我的文本文件中的内容,我想先分别获取第一行,然后循环遍历其余的文件。 现在我明白了,我可以简单地通过使用一个随着循环而增长的int来计算文件的数量,但我希望在文件中也能清楚地显示出来,我不知道该怎么做

static public Media[] importMedia(String fileName)
    {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line = reader.readLine();
            while(line != null)
            {
                //Get the first line of the text file seperatly? (Then maybe remove it? idk)
                //Split string, create a temp media file and add it to a list for the rest of the lines
            }
            //String[] split = s.next().split(",");
        } catch (Exception ex) { System.out.println(ex.getMessage()); }
        return null;
    }

我希望我的问题是清楚的,如果它TL;DR我想分别获取文本文件的第一行,然后其余的Id要循环。

您不需要
循环读取到文件末尾。读取第一行并将其转换为
int
而不是循环

static public Media[] importMedia(String fileName)
{
    try {

        BufferedReader reader = new BufferedReader(new FileReader(fileName));

        // Get and process first line:
        int lineNo=Integer.parseInt(reader.readLine());

        // Now read upto lineNo            
        for(int i=0; i < lineNo; i++){

            //Do what you need with other lines. 
            String[] values = reader.readLine().split(",");
        }

    } catch (Exception e) {
      //Your exception handling goes here
    }
}
静态公共媒体[]导入媒体(字符串文件名)
{
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
//获取并处理第一行:
int lineNo=Integer.parseInt(reader.readLine());
//现在读到第1行
对于(int i=0;i
您不需要在
循环读取文件末尾时执行
。读取第一行并将其转换为
int
而不是循环

static public Media[] importMedia(String fileName)
{
    try {

        BufferedReader reader = new BufferedReader(new FileReader(fileName));

        // Get and process first line:
        int lineNo=Integer.parseInt(reader.readLine());

        // Now read upto lineNo            
        for(int i=0; i < lineNo; i++){

            //Do what you need with other lines. 
            String[] values = reader.readLine().split(",");
        }

    } catch (Exception e) {
      //Your exception handling goes here
    }
}
静态公共媒体[]导入媒体(字符串文件名)
{
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
//获取并处理第一行:
int lineNo=Integer.parseInt(reader.readLine());
//现在读到第1行
对于(int i=0;i
我不建议在这里使用for循环,因为该文件可能包含额外的行(例如注释或空行),以使其更具可读性。通过检查每一行的内容,您可以使您的处理对这类事情更加健壮

static public Media[] importMedia(String fileName)
{
    try {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        // Get and process first line:
        String line = reader.readLine(); // <-- Get the first line. You could consider reader as a queue (sort-of), where readLine() dequeues the first element in the reader queue.
        int numberOfItems = Integer.valueOf(line); // <-- Create an int of that line.
        // Do the rest:
        while((line = reader.readLine()) != null) // <-- Each call to reader.readLine() will get the next line in the buffer, so the first time around this will give you the second line, etc. until there are no lines left to read.
        {
             // You will not get the header here, only the rest.
             if(!line.isEmpty() || line.startsWith("#") {
                 // If the line is not empty and doesn't start with a comment character (I chose # here).
                 String[] split = line.split(",");
                 String fileName = split[0];
                 // etc...
             }
        }
    } catch (Exception ex) { System.out.println(ex.getMessage()); }
    return null;
}
静态公共媒体[]导入媒体(字符串文件名)
{
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
//获取并处理第一行:

String line=reader.readLine();//我不建议在这里使用for循环,因为该文件可能包含额外的行(例如注释或空行)以使其更易于阅读。通过检查每行的内容,您可以使您的处理对这类事情更加健壮

static public Media[] importMedia(String fileName)
{
    try {
        BufferedReader reader = new BufferedReader(new FileReader(fileName));
        // Get and process first line:
        String line = reader.readLine(); // <-- Get the first line. You could consider reader as a queue (sort-of), where readLine() dequeues the first element in the reader queue.
        int numberOfItems = Integer.valueOf(line); // <-- Create an int of that line.
        // Do the rest:
        while((line = reader.readLine()) != null) // <-- Each call to reader.readLine() will get the next line in the buffer, so the first time around this will give you the second line, etc. until there are no lines left to read.
        {
             // You will not get the header here, only the rest.
             if(!line.isEmpty() || line.startsWith("#") {
                 // If the line is not empty and doesn't start with a comment character (I chose # here).
                 String[] split = line.split(",");
                 String fileName = split[0];
                 // etc...
             }
        }
    } catch (Exception ex) { System.out.println(ex.getMessage()); }
    return null;
}
静态公共媒体[]导入媒体(字符串文件名)
{
试一试{
BufferedReader reader=新的BufferedReader(新文件读取器(文件名));
//获取并处理第一行:

String line=reader.readLine();//很简单,读取第一行并将其保存到某个变量,然后分别为下一行启动循环。很简单,读取第一行并将其保存到某个变量,然后分别为下一行启动循环。由于某些原因,这只会覆盖第一行…?我将添加一些附加注释。@Cacoon不要复制粘贴代码,请更改cwhile循环中的条件使它为您工作。不,我理解,我不是盲目复制粘贴。while循环中的拆分器有问题,我已经修复了它,感谢您的帮助用split更新了行。它仍然使用问题代码中的旧变量…呜呜。这只是出于某种原因越过第一行…?我将添加一些附加注释。@Cacoon不要复制粘贴原样的代码。更改while循环中的条件以使其适用于您。不,我理解,我不是盲目复制粘贴。while循环中的拆分器有问题,我已修复该问题,谢谢您的帮助他同意斯普利特的观点。它仍然在使用问题代码中的一个旧变量…呜呜。