获取在读取CSV时的While循环中的最后一行数据 – Java

huangapple go评论73阅读模式
英文:

Get last row of data in a While loop while reading CSV - Java

问题

我正在尝试在***While循环***中遍历整个文件时,读取CSV文件的最后一行数据。我可以成功读取和解析,但似乎无法弄清楚如何在最后一行停止。我不想硬编码要执行的迭代次数。

代码:

File inputF = new File("T.csv");
InputStream getStream = new FileInputStream(inputF);

boolean flag = false;

try {
    System.out.println("正在检查CSV的数值...");
	if (getStream.read() != -1) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
		String line;
		String headerLine;
		int counter = 0;
		headerLine = reader.readLine();
		
	    //将条件更改为在最后一行结束
		while ((line = reader.readLine()) != null) {
		   // 读取和解析的逻辑
					
		}
        System.out.println(line);
		reader.close();
				
	}
} catch (IOException e) {
      e.printStackTrace();
	}
输出:

null


期望输出(不准确,实际数据将是CSV输出):

CSV数据


回顾:

我希望在while循环中设置一个条件,使其在最后一行停止,以便我可以处理它。我不能硬编码它,因为行数会根据文件读取而改变。

如果能提供帮助,将不胜感激。
英文:

I am trying to read the last row of data in a CSV file when iterating through the entire file in a While loop. I can manage to read and parse fine I just cant seem to figure out how to stop in the last row. I do not want to hardcode how many iterations to take.

Code:

File inputF = new File("T.csv");
InputStream getStream = new FileInputStream(inputF);
	     
boolean flag = false;
			
			
    try {
	    System.out.println("Checking Values of CSV...");
		if (getStream.read() != -1) {
			BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
			String line;
			String headerLine;
			int counter = 0;
			headerLine = reader.readLine();
			
		    //change condition to end at last row
			while ((line = reader.readLine()) != null) {
			   // logic to read and parse
						
			}
            System.out.println(line);
			reader.close();
					
		}
	} catch (IOException e) {
	      e.printStackTrace();
		}

Output:

null

Expected Output(not accurate, actual data will be csv output):

CSV DATA 

Recap -

I want to set a condition in the while loop so it stops in the last row so I can manage it. I cannot hardcode it because amount of rows will change based on file reading..

Any help would be appriciated.

答案1

得分: 1

我认为这样做就可以了。你只需要存储你成功读取的最后一行车道(就像我用 line 做的那样),当你无法读取下一行(在 nextLine 中读取)时,就完成了!

File inputF = new File("T.csv");
InputStream getStream = new FileInputStream(inputF);
         
boolean flag = false;
            
try {
    System.out.println("Checking Values of CSV...");
    if (getStream.read() != -1) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
        String line;
        String nextLine;
        String headerLine;
        int counter = 0;
        headerLine = reader.readLine();
            
        //change condition to end at last row
        while ((nextLine = reader.readLine()) != null) {
            line = nextLine;
            // logic to read and parse
                        
        }
        System.out.println(line);
        reader.close();
                    
    }
} catch (IOException e) {
    e.printStackTrace();
}
英文:

I think something like that will do the job. You just need to store the last lane you read successfully (what i'm doing with line) and when you can't read the next one (reading in nextLine), you're done !

InputStream getStream = new FileInputStream(inputF);
         
boolean flag = false;
            
            
    try {
        System.out.println("Checking Values of CSV...");
        if (getStream.read() != -1) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(getStream));
            String line;
            String nextLine;
            String headerLine;
            int counter = 0;
            headerLine = reader.readLine();
            
            //change condition to end at last row
            while ((nextLine= reader.readLine()) != null) {
               line = nextLine;
               // logic to read and parse
                        
            }
            System.out.println(line);
            reader.close();
                    
        }
    } catch (IOException e) {
          e.printStackTrace();
        }

答案2

得分: 0

我理解您希望在循环内部停留在最后一行。可以像下面展示的方式来实现这一点。如果您只想获取最后一行,下面的代码也可以工作。

String headerLine;
String footerLine;
headerLine = reader.readLine();
do {
    String line = reader.readLine();
    if (line == null) {
        // 在此处理 footerLine
        break;
    }
    
   // 读取和解析的逻辑
   footerLine = line;
} while (true);

System.out.println(footerLine);
// 到此为止,footerLine 就是最后一行
reader.close();
英文:

I understood that you want to stop on the last line while you’re inside the loop. This can be done like shown below. If you just want to get the last line, the below code would also work.

        String headerLine;
        String footerLine;
        headerLine = reader.readLine();
        do {
            String line = reader.readLine();
            if (line == null) {
                // do something with footerLine
                break;
            }
        
           // logic to read and parse
           footerLine = line;
        } while (true);

        System.out.println(footerLine);
        // footerLine is the last line by now
        reader.close();

huangapple
  • 本文由 发表于 2020年10月9日 02:55:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64268965.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定