检查空文本文件 [英] Check for empty text file

查看:88
本文介绍了检查空文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文本文件的内容读入变量。如果文本文件不存在,或者它确实存在并包含4行数据,则一切正常。但我需要考虑行为空或空。我怎么检查这个?

I am reading the contents of a text file into variables. All works well if the text file does not exist, or it does exist and contains 4 rows of data. But I need to account for rows being empty or null. How can I check this?

展开 | 选择 | Wrap | 行号

推荐答案

您可能希望查看ReadLine已经为你换行的方法。

我建议你读入一个临时数组,然后在将它们分配给最终变量之前对每一行进行限定。正如您所见,您可以认为事情正如您所期望的那样。用户有惊人的能力搞砸了。

You might want to look at ReadLine method that already makes the line breaks for you.
I would recommend you read into a temporary array, then qualify each of your lines before assigning them to their final variables. As you have seen you can assume that things are as you expect. Users have an amazing capacity to screw things up.

展开 | 选择 | Wrap | 行号


这行是什么......


List< string> Temp = new List< string>();


抛出错误。
What is this line...

List<string> Temp = new List<string>();

It is throwing errors.


使用System.Collections添加使用语句


;

使用System.Collections.Generic;


A List< T>是一种高级类型的数组,添加了一些方法,比如

.RemoveAt(5)删除List的第6个元素。

如果你使用数组,你必须处理向下移动所有其他元素。添加更有问题,因为你必须使用array.Resize(newsize)这个东西。
add the using statements

using System.Collections;
using System.Collections.Generic;

A List<T> is an advanced type of array with added methods for things like
.RemoveAt(5) to remove the 6th element of the List.
If you use an array you have to handle the moving of all the other elements downward. Adding is more problematic since you have to array.Resize(newsize) the thing.


这篇关于检查空文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆