已有一个 ArrayList 对象. 要 ArrayList 数据保存到文件中.要求:
当 ArrayList 的元素超过了100,需要按照 100元素/次 这样保存.同时文件名后面添加一个数列(文件名1,文件名2,文件名3...)附录:/// <summary>
/// 保存到文件
/// </summary>
/// <param name="path">保存路径</param>
/// <param name="arrayList">要保存的数据</param>
public SaveFile(string _path, ArrayList _ArrayListData) {
...
}谢谢了! 头脑好乱
当 ArrayList 的元素超过了100,需要按照 100元素/次 这样保存.同时文件名后面添加一个数列(文件名1,文件名2,文件名3...)附录:/// <summary>
/// 保存到文件
/// </summary>
/// <param name="path">保存路径</param>
/// <param name="arrayList">要保存的数据</param>
public SaveFile(string _path, ArrayList _ArrayListData) {
...
}谢谢了! 头脑好乱
/// 保存到文件
/// </summary>
/// <param name="path">保存路径 </param>
/// <param name="arrayList">要保存的数据 </param>
public void SaveFile(string _path, ArrayList _ArrayListData)
{
if (_ArrayListData == null || _ArrayListData.Count <= 0)
return; System.IO.TextWriter write = new System.IO.StreamWriter(_path);
for (int i = 0; i < _ArrayListData.Count; i++)
{
write.WriteLine(_ArrayListData[i].ToString());
}
write.Close();
}
void SaveData(Dictionary<int, string> data)
{
//检查
string path = "文件.txt";
FileStream fs = null;
StreamWriter sw = null;
try
{
if (File.Exists(path))
{
File.Delete(path); fs = File.Open(path, FileMode.Append, FileAccess.Write);
}
else
fs = File.Open(path, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs); foreach (var item in data.Keys)
{
sw.WriteLine(data[item]);
if (item % 10 == 0 && item != 0)
{
path = "文件" + (item / 10).ToString() + ".txt";
if (File.Exists(path))
{
File.Delete(path); fs = File.Open(path, FileMode.Append, FileAccess.Write);
}
else
{
fs = File.Open(path, FileMode.Append, FileAccess.Write);
}
sw.Flush();
Thread.Sleep(10);
sw = new StreamWriter(fs);
}
} MessageBox.Show("Test is completed!");
}
finally
{
if (fs != null)
fs.Close();
sw.Close();
}
} Dictionary<int, string> data = new Dictionary<int, string>();
for (int i = 0; i < 21; i++)
{
data.Add(i, i.ToString());
}
this.SaveData(data);//测试代码:
//完整测试:
using System;
using System.Collections.Generic;
using System.Threading;
using System.IO;
class Program
{
static void Main()
{
Dictionary<int, string> data = new Dictionary<int, string>();
for (int i = 0; i < 21; i++)
{
data.Add(i, i.ToString());
}
this.SaveData(data); }
static void SaveData(Dictionary<int, string> data)
{
string path = "文件.txt";
FileStream fs = null;
StreamWriter sw = null;
try
{
if (File.Exists(path))
{
File.Delete(path); fs = File.Open(path, FileMode.Append, FileAccess.Write);
}
else
fs = File.Open(path, FileMode.Append, FileAccess.Write); sw = new StreamWriter(fs); foreach (var item in data.Keys)
{
sw.WriteLine(data[item]);
if (item % 10 == 0 && item != 0)
{
path = "文件" + (item / 10).ToString() + ".txt";
if (File.Exists(path))
{
File.Delete(path); fs = File.Open(path, FileMode.Append, FileAccess.Write);
}
else
{
fs = File.Open(path, FileMode.Append, FileAccess.Write);
}
sw.Flush();
Thread.Sleep(10);
sw = new StreamWriter(fs);
}
} MessageBox.Show("Test is completed!");
}
finally
{
if (fs != null)
fs.Close();
sw.Close();
}
}
}
我测试了一下,大概差不多,可以跑,楼主再自己看看..
我认为ProjectDD的是应该是可以实现楼主想法的.
这个不会..汗了..看帮助也是稀里糊涂的
if (_path.Length <= 0 && _arrayListData.Count <= 0) { return; } // 循环约束,根据 每次的数据量得出.
int j = _arrayListData.Count / ValueCount; if ((_arrayListData.Count - j * ValueCount) > 0) { j++; } StreamWriter sw = null; ; try {
for (int i = 0; i < j; i++) {
Filepath = _path.Insert(_path.LastIndexOf("."), "(" + j + "--" + (i + 1).ToString() + ")"); // 处理文件名,在原文件名后添加序列号
sw = new StreamWriter(Filepath, false, System.Text.Encoding.GetEncoding("gb2312"), 512); // false 覆盖
for (int x = 0; x < ValueCount; x++) {
try {
sw.WriteLine(_arrayListData[x].ToString());
} catch (ArgumentOutOfRangeException) { }
} try {
_arrayListData.RemoveRange(0, ValueCount);
} catch (ArgumentException) {// 这个处理是没办法的.我郁闷
_arrayListData.Clear();
}
sw.Flush();
sw.Close();
}
} finally { if (sw == null) sw.Close(); } // 这个!!!!!
}