| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 18595 人关注过本帖
标题:Read()和ReadLine()的区别
只看楼主 加入收藏
yigedaizi
Rank: 1
等 级:新手上路
帖 子:123
专家分:0
注 册:2006-11-24
收藏
得分:0 


layman on C#
2006-12-09 00:02
缘来
Rank: 1
等 级:新手上路
帖 子:135
专家分:0
注 册:2006-11-18
收藏
得分:0 
哈哈,讨论激烈啊,Console.Read()方法用于得到任意用户自定义值的ASCII值。

博客:http://hi.baidu.com/xiaotown欢迎大家一起交流!
2006-12-09 19:50
舞雪
Rank: 1
等 级:新手上路
威 望:1
帖 子:1786
专家分:0
注 册:2006-10-11
收藏
得分:0 

Read:从标准输入流读取下一个字符。
返回值:输入流中的下一个字符;或者如果没有更多的可用字符,则为负一 (-1)。

备注
此方法直到读取操作终止(例如,用户按下 Enter 键)后才会返回。如果数据可用,则输入流包含用户输入的内容,末尾附加依赖于环境的换行符。

示例
[Visual Basic]
Dim i As Integer
Dim c As Char
While True
i = Console.Read()
If i = - 1 Then
Exit While
End If
c = Microsoft.VisualBasic.Chr(i)
Console.WriteLine("Echo: {0}", c)
End While
Console.WriteLine("Done")
Return 0
[C#]
int i;
char c;
while (true)
{
i = Console.Read ();
if (i == -1) break;
c = (char) i;
Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;
[JScript]
var i : int;
var c : char;
while (true)
{
i = Console.Read ();
if (i == -1) break;
c = char(i);
Console.WriteLine ("Echo: {0}", c);
}
Console.WriteLine ("Done");
return 0;

--------------------------------------------------------------------------
ReadLine:从标准输入流读取下一行字符。
返回值:输入流中的下一行;或者如果没有更多的可用字符,则为空引用(Visual Basic 中为 Nothing)。

备注
行被定义为后跟回车符(十六进制 0x000d)、换行符(十六进制 0x000a)或 Environment.NewLine 的字符序列。返回的字符串不包含终止字符。

如果此方法引发 OutOfMemoryException,则读取器在基础 Stream 的位置会前移若干字符,前移的字符数就是该方法能够读取的字符数,但是会放弃已经读入内部 ReadLine 缓冲区的字符。因为无法更改读取器在流中的位置,所以已经读取的字符是无法恢复的,只能通过重新初始化 TextReader 来访问这些字符。如果流内的初始位置未知,或者流不支持查找,则也需要重新初始化基础 Stream。

若要避免出现这种情况并生成可靠的代码,则应当使用 Read 方法,并将所读取字符存储在预先分配的缓冲区内。

[Visual Basic, C#, JScript] 下面的代码示例说明 ReadLine 的用法:
[Visual Basic]
Public Class InsertTabs
Private Const tabSize As Integer = 4
Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"

'Entry point which delegates to C-style main Private Function
Public Overloads Shared Sub Main()
System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
End Sub

Overloads Public Shared Function Main(args() As String) As Integer
Dim writer As StreamWriter = Nothing

If args.Length < 3 Then
Console.WriteLine(usageText)
Return 1
End If

Try
writer = New StreamWriter(args(2))
Console.SetOut(writer)
Console.SetIn(New StreamReader(args(1)))
Catch e As IOException
Dim errorWriter As TextWriter = Console.Error
errorWriter.WriteLine(e.Message)
errorWriter.WriteLine(usageText)
Return 1
End Try
Dim line As String
line = Console.ReadLine()
While Not line Is Nothing
Dim newLine As String = line.Replace("".PadRight(tabSize, " "c), ControlChars.Tab)
Console.WriteLine(newLine)
line = Console.ReadLine()
End While
writer.Close()
' Recover the standard output stream so that a
' completion message can be displayed.
Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
standardOutput.AutoFlush = True
Console.SetOut(standardOutput)
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args(0))
Return 0
End Function 'Main
End Class 'InsertTabs
[C#]
public class InsertTabs {
private const int tabSize = 4;
private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
public static int Main(string[] args) {
StreamWriter writer = null;

if (args.Length < 2) {
Console.WriteLine(usageText);
return 1;
}

try {
writer = new StreamWriter(args[1]);
Console.SetOut(writer);
Console.SetIn(new StreamReader(args[0]));
}
catch(IOException e) {
TextWriter errorWriter = Console.Error;
errorWriter.WriteLine(e.Message);
errorWriter.WriteLine(usageText);
return 1;
}
string line;
while ((line = Console.ReadLine()) != null) {
string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a
// completion message can be displayed.
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);
return 0;
}
}
[JScript]
const tabSize = 4;
const usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";

var writer : StreamWriter = null;
var args = Environment.GetCommandLineArgs();

if (args.Length != 3) {
Console.WriteLine(usageText);
Environment.Exit(1);
}

try {
writer = new StreamWriter(args[2]);
Console.SetOut(writer);
Console.SetIn(new StreamReader(args[1]));
}
catch(e : IOException) {
var errorWriter = Console.Error;
errorWriter.WriteLine(e.Message);
errorWriter.WriteLine(usageText);
Environment.Exit(1);
}
var line;
while ((line = Console.ReadLine()) != null) {
var newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);



呵呵,不愧是BC论坛
2006-12-09 21:05
舞雪
Rank: 1
等 级:新手上路
威 望:1
帖 子:1786
专家分:0
注 册:2006-10-11
收藏
得分:0 
这样的回答不知道楼主满意不

呵呵,不愧是BC论坛
2006-12-09 22:27
daidai120
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2013-11-10
收藏
得分:0 
南京白癜风http://user.qzone.
南京白癜风医院http://user.qzone.
南京白癜风专科医院http://user.qzone.
南京治疗白癜风最好医院http://user.qzone.




徐州白癜风http://user.qzone.
徐州白癜风医院http://user.qzone.
徐州白癜风专科医院http://user.qzone.
徐州治疗白癜风最好的医院http://user.qzone.

2013-11-10 14:12
qindongqing
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2016-4-29
收藏
得分:0 
2016-04-29 20:27
快速回复:Read()和ReadLine()的区别
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019956 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved