zoukankan
html css js c++ java
.NET中的文件IO操作实例
从TextBox控件中写入到txt文本
Code
//
从testbox中写入到txt文本
protected
void
Button5_Click(
object
sender, EventArgs e)
{
string
text
=
txtContent.Text;
if
(
!
string
.IsNullOrEmpty(text))
{
//
指定文件的完整路径
string
fileName
=
Server.MapPath(
"
~/txt/test.txt
"
);
//
判断该文件是否存在
if
(File.Exists(fileName))
{
//
如果存在,就先删掉
File.Delete(fileName);
}
else
{
//
创建一个文件操作的流
FileStream stream
=
new
FileStream(fileName, FileMode.Create);
//
创建一个写操作流
StreamWriter writer
=
new
StreamWriter(stream, Encoding.UTF8);
//
进行写操作
writer.Write(text);
//清空控件中的文字
txtContent.Text
=
string
.Empty;
//
关闭流,不然出现异常
writer.Close();
stream.Close();
}
}
else
{
Response.Write(
"
<script>alert(\
"
空的列!\
"
)</script>
"
);
}
}
然后再从生成的test.txt中读取数据,显示到TextBox控件中(方法同理)
Code
//
读取文本到textbox中显示
protected
void
Button6_Click(
object
sender, EventArgs e)
{
string
fileName
=
Server.MapPath(
"
~/txt/test.txt
"
);
if
(File.Exists(fileName))
{
FileStream stream
=
new
FileStream(fileName, FileMode.Open);
StreamReader reader
=
new
StreamReader(stream, Encoding.UTF8);
txtContent.Text
=
reader.ReadToEnd();
reader.Close();
stream.Close();
}
else
{
Response.Write(
"
<script>alert(\
"
没有test.txt文件!\
"
)</script>
"
);
}
}
查看全文
相关阅读:
python3读取chrome浏览器cookies
python3 reqeuests给OSC全站用户刷积分
批处理与python代码混合编程的实现方法
python3 使用pyperclip读写剪贴板(windows)
python3使用requests登录人人影视网站
Flask form
Flask session
Flask 目录
flask 源码剖析
单例模式
原文地址:https://www.cnblogs.com/kingfly/p/1567503.html
最新文章
计算两点经纬度之间的距离
在程序中使用自定义控件的自定义属性注意事项
sp转dp dp转px
获取字体的高度和宽度
数据库隔离级别
spring+springmvc+mybatis整合
springmvc 配置直接访问页面
spring mvc上传图片
spring mvc表单自动装入实体对象
spring mvc设置字符集过滤器
热门文章
windows 版的julia repl 启动时间已经大大优化!
学习Nim语言.rar(nim语言中文教程下载)
码农的联系方式
一个最简单的博客园文章密码暴力破解器-python3实现
python3登录极路由并读取宽带帐号帐号密码.py
python3+任务计划实现的人人影视网站自动签到
python3批量删除豆瓣分组下的好友
qpython3 读取安卓lastpass Cookies
用qpython3写一个最简单的发送短信的程序
安卓手机上的python运行环境-qpython
Copyright © 2011-2022 走看看