麻烦大家帮忙啊,知道一个也麻烦发下程序。
谢谢了!1.一个文本文件,文件名为c:\f9.txt,包含有3行,每行有3个int型数据,行中两个数据之间用“,”分隔。使用StreamReader对象从文件中读取数据,并放入一个3×3的二维数组中,请编写代码。2.一个3×3的二维int型数组,现需要将其元素依次写到一个文本文件中,并且每3个一行,行内元素间用“,”分隔。3.一个二进制文件,文件名为c:\f9.data,包含9个int型数据。使用FileStream,Stream,BinaryReader类对象方法从文件中读取数据,并放入一个3×3的二维数组中,请编写代码。4.一个3×3的二维int型数组,现需要将其元素依次写到一个二进制文件中。5.在Graphics绘图面中心绘制一个1/4大小的蓝色矩形,在矩形内中心位置填充一个适当大小的红色椭圆,在椭圆内中心位置写上蓝色文字“我的绘图画”,并在矩形的4个角各绘制一个蓝色小圆。6.使用SqlConnection,SqlCommand,SqlDataReader和SqlParameter(如果需要的话),写一个方法从数据库中读数据,并将记录生成文本行,所有文本行放入到一个TextBox控件。
谢谢了!1.一个文本文件,文件名为c:\f9.txt,包含有3行,每行有3个int型数据,行中两个数据之间用“,”分隔。使用StreamReader对象从文件中读取数据,并放入一个3×3的二维数组中,请编写代码。2.一个3×3的二维int型数组,现需要将其元素依次写到一个文本文件中,并且每3个一行,行内元素间用“,”分隔。3.一个二进制文件,文件名为c:\f9.data,包含9个int型数据。使用FileStream,Stream,BinaryReader类对象方法从文件中读取数据,并放入一个3×3的二维数组中,请编写代码。4.一个3×3的二维int型数组,现需要将其元素依次写到一个二进制文件中。5.在Graphics绘图面中心绘制一个1/4大小的蓝色矩形,在矩形内中心位置填充一个适当大小的红色椭圆,在椭圆内中心位置写上蓝色文字“我的绘图画”,并在矩形的4个角各绘制一个蓝色小圆。6.使用SqlConnection,SqlCommand,SqlDataReader和SqlParameter(如果需要的话),写一个方法从数据库中读数据,并将记录生成文本行,所有文本行放入到一个TextBox控件。
楼主的第一个问题,我已经解决了,这是代码你自己看看,剩下你就可以自己解决了。
static void Main(string[] args)
{
//问题:1.一个文本文件,文件名为c:\f9.txt,包含有3行,每行有3个int型数据,行中两个数据之间用“,”分隔。使用
//StreamReader对象从文件中读取数据,并放入一个3×3的二维数组中,请编写代码。
//文本文件的内容
//23,24,25
//26,27,28
//29,30,31 string Path = @"c:\f9.txt";
int[,] testArray = new int[3, 3];
string strTemp = String.Empty;
string[] strArray = new string[3];
int x1, x2, x3;
int i = 0;
try
{
if (!File.Exists(Path))
{
File.CreateText(Path);
} using (StreamReader sr = new StreamReader(Path))
{
while (sr.Peek() >= 0 && i<3)
{
//读取每一行,然后存储起来。
strTemp=sr.ReadLine();
strArray = strTemp.Split(',');
x1 = int.Parse(strArray[0]);
x2 = int.Parse(strArray[1]);
x3 = int.Parse(strArray[2]);
testArray[i, 0] = x1;
testArray[i, 1] = x2;
testArray[i, 2] = x3;
i++;
}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
2.一个3×3的二维int型数组,现需要将其元素依次写到一个文本文件中,并且每3个一行,行内元素间用“,”分隔。
[code=C#]using System.IO;int[,] array = new int[3,3]{{1,2,3},{4,5,6},{7,8,9}};
string Path = "d:\\1.txt";
string strMsg = string.Empty;
StreamWriter sw = new StreamWriter(Path,true); for(int i=0; i<array.Length;i++)
{
strMsg += i.ToString();
if(i%3 == 2)
{
sw.WriteLine(strMsg);
sw.Flush();
strMsg = string.Empty;
}
else
{
strMsg += ",";
}
}
sw.Close();[code]
string Path = "d:\\1.txt";
string strMsg = string.Empty;
StreamWriter sw = new StreamWriter(Path,true); for(int i=0; i <array.Length;i++)
{
strMsg += i.ToString();
if(i%3 == 2)
{
sw.WriteLine(strMsg);
sw.Flush();
strMsg = string.Empty;
}
else
{
strMsg += ",";
}
}
sw.Close();
{
int[,] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; FileStream fs = new FileStream(@"c:\a.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs); for (int i = 0; i < arr.GetLength(0); i++)
{
for (int j = 0; j < arr.GetLength(1); j++)
{
bw.Write(arr[i, j]);
//bw.Write("asdf"+i.ToString());
//bw.Write("asdf" + j.ToString());
}
}
fs.Close();
bw.Close();
} public void Read()
{
FileStream fs = new FileStream(@"c:\a.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryReader br = new BinaryReader(fs);
while (br.PeekChar() != -1)
{
Console.WriteLine(br.ReadInt32());
}
fs.Close();
br.Close();
}