我的代码事这样的
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace Book
{
public class Book
{
private string bookID;
private string bookName;
private string publish;
private string author; public string BookID {
get { return bookID; }
set { bookID = value; }
}
public string BookName {
get { return bookName; }
set { bookName = value; }
}
public string Publish {
get { return publish; }
set { publish = value; }
}
public string Author {
get { return author; }
set { author = value; }
}
} public class BookMethod
{
Book book = new Book();
public void into1(string a){
FileStream fs = new FileStream("e:\\aa.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(a);
sw.Close();
}
//输入
public void into(string a) {
StreamWriter writer = new StreamWriter("e:\\aa.txt",true);
writer.WriteLine(a);
writer.Close();
}//输出所有
public void outf() {
string text;
FileStream fs1 = new FileStream("e:\\aa.txt",FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs1);
text=sr.ReadToEnd();
Console.WriteLine(text);
sr.Close();
fs1.Close();
}
//根据书号查询
public void outf1(string b) {
FileInfo fi = new FileInfo("e:\\aa.txt");
StreamReader sr = fi.OpenText();
string text; do{
string[] text2=new string[4];
text = sr.ReadLine();
//?
try {
text2= text.Split(' ');
}catch(SystemException e){
Console.WriteLine(e.Message);
}
if(text2[0]==b)
Console.WriteLine(text);
}while(text!=null);
sr.Close();
}
public string outf2(string b)
{
FileInfo fi = new FileInfo("e:\\aa.txt");
StreamReader sr = fi.OpenText();
string text;
string text3;
do
{
string[] text2 = new string[4];
text = sr.ReadLine();
//?
try
{
text2 = text.Split(' ');
}
catch (SystemException e)
{
Console.WriteLine(e.Message);
} if (text2[0] == b)
{Console.WriteLine(text);
text3 = text;
return text3;
}
} while (text != null);
sr.Close();
return null;
}
//增加
public void AddBook() {
bool f=false; do{
Console.WriteLine("请输入书号!");
string num = Console.ReadLine().Trim();
Console.WriteLine("请输入书名!");
string name = Console.ReadLine();
Console.WriteLine("请输入出版社!");
string pub = Console.ReadLine();
Console.WriteLine("请输入作者!");
string auth = Console.ReadLine();
string save = num + " " + name + " " + pub + " " + auth;
into(save);
Console.WriteLine("是否继续添加?(Y/N)");
string a = Console.ReadLine();
if (a == "Y" || a == "y")
f = true;
else
f = false;
}while(f);
}
public string AddBook1() {
Console.WriteLine("请输入书号!");
string num = Console.ReadLine().Trim();
Console.WriteLine("请输入书名!");
string name = Console.ReadLine();
Console.WriteLine("请输入出版社!");
string pub = Console.ReadLine();
Console.WriteLine("请输入作者!");
string auth = Console.ReadLine();
string save = num + " " + name + " " + pub + " " + auth;
Console.WriteLine(save);
Console.WriteLine("++++++++++++++++++++");
return save;
}
//修改
public void replace(string a,string b) {
StreamReader sr1 = new StreamReader("e:\\aa.txt");
string s = sr1.ReadToEnd();
sr1.Close();
try {
s = s.Replace(a,b);
Console.WriteLine(s);
}catch(SystemException ex){
Console.WriteLine(ex.Message);
}
//try {
Console.WriteLine("haha");
into1(s);
// }catch(SystemException e){
Console.WriteLine("-------------");
// }
}
public void Del(string a) {
}
class BookTest
{
static void Main(string[] args)
{
BookMethod b = new BookMethod();
string p="";
do{
Console.WriteLine("请选择业务:输入0添加,1删除,2修改,3查看所有,4根据书号查看,5退出");
p=Console.ReadLine();
switch(p)
{
case "0":
b.AddBook();
break;
case "1":
Console.WriteLine("请输入要删除的书号!");
string s = Console.ReadLine().Trim();
string q = b.outf2(s);
Console.WriteLine(q); b.Del(q);
Console.WriteLine("删除成功!");
break;
case "2":
/*b.replace();*/
Console.WriteLine("请输入要修改的书号!");
string c = Console.ReadLine().Trim();
string e= b.outf2(c);
Console.WriteLine("修改为:");
string d= b.AddBook1();
Console.WriteLine(e);
b.replace(e,d);
Console.WriteLine("修改成功!");
break;
case "3":
b.outf();
break;
case "4":
string a;
Console.WriteLine("请输入要查询的书号!");
a = Console.ReadLine().Trim();
b.outf1(a);
break;
case "5":
return;
default:
break;
}
}while(true);
}
}
}
在实现修改功能时,修改后不能写入文件,报异常 e:\\aa.txt被另一个进程使用不能访问到
是怎么回事?谢谢
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.IO;namespace Book
{
public class Book
{
private string bookID;
private string bookName;
private string publish;
private string author; public string BookID {
get { return bookID; }
set { bookID = value; }
}
public string BookName {
get { return bookName; }
set { bookName = value; }
}
public string Publish {
get { return publish; }
set { publish = value; }
}
public string Author {
get { return author; }
set { author = value; }
}
} public class BookMethod
{
Book book = new Book();
public void into1(string a){
FileStream fs = new FileStream("e:\\aa.txt",FileMode.Create,FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(a);
sw.Close();
}
//输入
public void into(string a) {
StreamWriter writer = new StreamWriter("e:\\aa.txt",true);
writer.WriteLine(a);
writer.Close();
}//输出所有
public void outf() {
string text;
FileStream fs1 = new FileStream("e:\\aa.txt",FileMode.Open,FileAccess.Read);
StreamReader sr = new StreamReader(fs1);
text=sr.ReadToEnd();
Console.WriteLine(text);
sr.Close();
fs1.Close();
}
//根据书号查询
public void outf1(string b) {
FileInfo fi = new FileInfo("e:\\aa.txt");
StreamReader sr = fi.OpenText();
string text; do{
string[] text2=new string[4];
text = sr.ReadLine();
//?
try {
text2= text.Split(' ');
}catch(SystemException e){
Console.WriteLine(e.Message);
}
if(text2[0]==b)
Console.WriteLine(text);
}while(text!=null);
sr.Close();
}
public string outf2(string b)
{
FileInfo fi = new FileInfo("e:\\aa.txt");
StreamReader sr = fi.OpenText();
string text;
string text3;
do
{
string[] text2 = new string[4];
text = sr.ReadLine();
//?
try
{
text2 = text.Split(' ');
}
catch (SystemException e)
{
Console.WriteLine(e.Message);
} if (text2[0] == b)
{Console.WriteLine(text);
text3 = text;
return text3;
}
} while (text != null);
sr.Close();
return null;
}
//增加
public void AddBook() {
bool f=false; do{
Console.WriteLine("请输入书号!");
string num = Console.ReadLine().Trim();
Console.WriteLine("请输入书名!");
string name = Console.ReadLine();
Console.WriteLine("请输入出版社!");
string pub = Console.ReadLine();
Console.WriteLine("请输入作者!");
string auth = Console.ReadLine();
string save = num + " " + name + " " + pub + " " + auth;
into(save);
Console.WriteLine("是否继续添加?(Y/N)");
string a = Console.ReadLine();
if (a == "Y" || a == "y")
f = true;
else
f = false;
}while(f);
}
public string AddBook1() {
Console.WriteLine("请输入书号!");
string num = Console.ReadLine().Trim();
Console.WriteLine("请输入书名!");
string name = Console.ReadLine();
Console.WriteLine("请输入出版社!");
string pub = Console.ReadLine();
Console.WriteLine("请输入作者!");
string auth = Console.ReadLine();
string save = num + " " + name + " " + pub + " " + auth;
Console.WriteLine(save);
Console.WriteLine("++++++++++++++++++++");
return save;
}
//修改
public void replace(string a,string b) {
StreamReader sr1 = new StreamReader("e:\\aa.txt");
string s = sr1.ReadToEnd();
sr1.Close();
try {
s = s.Replace(a,b);
Console.WriteLine(s);
}catch(SystemException ex){
Console.WriteLine(ex.Message);
}
//try {
Console.WriteLine("haha");
into1(s);
// }catch(SystemException e){
Console.WriteLine("-------------");
// }
}
public void Del(string a) {
}
class BookTest
{
static void Main(string[] args)
{
BookMethod b = new BookMethod();
string p="";
do{
Console.WriteLine("请选择业务:输入0添加,1删除,2修改,3查看所有,4根据书号查看,5退出");
p=Console.ReadLine();
switch(p)
{
case "0":
b.AddBook();
break;
case "1":
Console.WriteLine("请输入要删除的书号!");
string s = Console.ReadLine().Trim();
string q = b.outf2(s);
Console.WriteLine(q); b.Del(q);
Console.WriteLine("删除成功!");
break;
case "2":
/*b.replace();*/
Console.WriteLine("请输入要修改的书号!");
string c = Console.ReadLine().Trim();
string e= b.outf2(c);
Console.WriteLine("修改为:");
string d= b.AddBook1();
Console.WriteLine(e);
b.replace(e,d);
Console.WriteLine("修改成功!");
break;
case "3":
b.outf();
break;
case "4":
string a;
Console.WriteLine("请输入要查询的书号!");
a = Console.ReadLine().Trim();
b.outf1(a);
break;
case "5":
return;
default:
break;
}
}while(true);
}
}
}
在实现修改功能时,修改后不能写入文件,报异常 e:\\aa.txt被另一个进程使用不能访问到
是怎么回事?谢谢
public void replace(string a,string b) {
StreamReader sr1 = new StreamReader("e:\\aa.txt");
string s = sr1.ReadToEnd();
sr1.Close(); try {
s = s.Replace(a,b);
Console.WriteLine(s);
}catch(SystemException ex){
Console.WriteLine(ex.Message);
}
//try {
Console.WriteLine("haha");
into1(s);
// }catch(SystemException e){
Console.WriteLine("-------------");
// }
}
可我后面已经关闭了呀麻烦告诉我应该在哪里关闭?
不好意思,我刚刚开始接触这个,谢谢
String path = "your path";
using (StreamReader sr = new StreamReader(path))
{
//This allows you to do one Read operation.
Console.WriteLine(sr.ReadToEnd());
}
这样子用你的StreamReader试试