文件操作 - 过程问题



在文件操作下,我得到了几组不同的过程,用于C#中的各种不同内容。

我基本上忘记了如何调用或使用程序,因此在我弄清楚如何操作之前,它们对我来说几乎毫无用处。很抱歉听起来很愚蠢,但我已经做了尽可能多的搜索,我无法将其他来源与我的问题联系起来。

这是我得到的一个程序:

void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}

现在我完全理解了这个和所有其他程序的作用,但我忘记了如何在主要中使用它们。

这是我目前的主要内容:

string path = "C:\Users\Joe\Documents\General\College\Computer Science\Coding\TextFileWork\textFile.txt";
string readFile;
readFile = readFromTextFile(path);

现在我遇到的问题是了解如何使用该过程返回 main 以返回读取文件。字符串 readFile 是我试图将读取文本附加到的内容,但是我不知道应该如何调用该函数以附加它。一些基本的帮助就足够了,谢谢!

编辑:

这是我当前拥有的完整代码(C# 控制台应用程序)

namespace TextFileWork_03._03._18
{
class Program
{

static void Main(string[] args)
{
string path = "C:\Users\Joe\Documents\General\College\Computer 
Science\Coding\TextFileWork\textFile.txt";
string readFile;
readFromTextFile(readFile);
if (File.Exists(path) == true)
{
//Create a file to write to.
Console.WriteLine(path + " Exists");
}
else
{
Console.WriteLine(path + " File not found");
}
FileInfo fi = new FileInfo(path);
FileStream fs = fi.Create();
fs.Close();
if (File.Exists(path) == true)
{
//Create a file to write to.
Console.WriteLine(path + " Now exists");
}
else
{
Console.WriteLine(path + " File still not found");
}




}
static void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
}
}

为了使你的代码工作,你需要一个来自方法的返回值,否则你不能说:

mysomthing = readFromTextFile

因此,让我们返回一个字符串值: 有一个问题:是要返回一行还是只返回整个文件?

这是整个文件版本:

static string readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
StringBuilder sb = new StringBuilder();
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
sb.AppendLine(line);
}
//close the file
sr.Close();
return sb.ToString();
}

更新这个答案是无效的,我只保留几分钟供参考。

你应该提供更多的代码来真正使这个问题成为一个有效的问题。不过,我会尽力帮助你。

您的过程(我们在 C# 中称之为方法(与函数密切相关))存在于一个类中,我们将其称为Foo,但您可以在代码中查找它。只需向上滚动:这是您将看到的第一个蓝色class

public class Foo //this is your class
{
void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
}

若要从Main方法调用它,需要一个对象

void Main()
{
string path = "C:\Users\Joe\Documents\General\College\ComputerScience\Coding\TextFileWork\textFile.txt";
string readFile;
Foo fooObject = new Foo(); //create a new Foo
readFile = fooObject.readFromTextFile(path);
}

最新更新